• Net学习日记_ADO.Net_2_练习(导入练习)


    导入练习

    主代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace InputFile
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnOpenFile_Click(object sender, EventArgs e)
            {
                OpenFileDialog of = new OpenFileDialog();
                if (of.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    tbFile.Text = of.FileName;
                }
            }
    
            private void btnInput_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(tbFile.Text))
                {
                    MessageBox.Show("请选择文件!!");
                }
    
                string[] allLines = File.ReadAllLines(tbFile.Text,Encoding.Default);
    
                foreach (string item in allLines)
                {
                    string[] napd = item.Split('|');
                    SqlHelper.Insert(napd[0],napd[1]);
                }
                MessageBox.Show("导入完毕!!!");
    
            }
    } }

     辅助

    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace InputFile
    {
        class SqlHelper
        {
    
            public static bool Insert(string name,string pwd)
            {
                using (SqlConnection conn = new SqlConnection("server=PC201609230944\SQL2005;database=HeiMaBlog;user=sa;pwd=123456"))
                {
                    string sql = "insert into UserInfo values(@name,@pwd,default,default)";
                    using (SqlCommand cmd = new SqlCommand(sql,conn))
                    {
                        SqlParameter sp1 = new SqlParameter("@name",name);
                        SqlParameter sp2 = new SqlParameter("@pwd",pwd);
                        cmd.Parameters.Add(sp1);
                        cmd.Parameters.Add(sp2);
    
                        conn.Open();
                        return cmd.ExecuteNonQuery()>0;
                    }
                }          
            }
        }
    }

    数据库

  • 相关阅读:
    又是七月的尾巴
    在Macbook Air上安装Archlinux
    又是一年毕业季
    orgpage a static site generator for emacs and org mode
    看完了《三体》三部曲
    暴强的命令行git提交历史记录查询
    xaml的window的AllowsTransparency属性在winxp下好像有bug,不知还有谁遇到过
    Swift 计算字符串展示的区域
    SwiftUI Stack中的View被压缩的效果
    如果git pull拉取分支出错,如何返回
  • 原文地址:https://www.cnblogs.com/lisong-home/p/7747901.html
Copyright © 2020-2023  润新知