ado.net中,数据的批量处理

//btBigImport按钮点击事件

private void btBigImport_Click(object sender, RoutedEventArgs e)
        {

//连接字符串
            string connStr = ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString;

//文件选取
            OpenFileDialog ofd = new OpenFileDialog();

//文件选取类型
            ofd.Filter = "文本文件|*.txt";

ofd.ShowDialog();
            string[] lines = File.ReadAllLines(ofd.FileName, Encoding.Default);

DataTable table = new DataTable();
            table.Columns.Add("startNum");
            table.Columns.Add("area");
            table.Columns.Add("numType");
            table.Columns.Add("areaNum");

foreach (string line in lines)
            {
                string[] str = line.Split('\t');
                string startNum = str[0];
                string area = str[1];
                string numType = str[2];
                string areaNum = str[3];

DataRow row = table.NewRow();
                row["startNum"] = startNum;
                row["area"] = area;
                row["numType"] = numType;
                row["areaNum"] = areaNum;
                table.Rows.Add(row);
            }

using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connStr))
            {
                bulkCopy.DestinationTableName = "T_telArea";

bulkCopy.ColumnMappings.Add("startNum", "startNum");
                bulkCopy.ColumnMappings.Add("area", "area");
                bulkCopy.ColumnMappings.Add("numType", "numType");
                bulkCopy.ColumnMappings.Add("areaNum", "areaNum");
                bulkCopy.WriteToServer(table);
            }
            MessageBox.Show("成功插入!!");
        }

上一篇:【转载】Asp.net Mvc 入门视频教程


下一篇:Mifare系列7-安全性(转)