c#插入数据库
private void button1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "文本文件|*.txt"; if (ofd.ShowDialog()!=DialogResult.OK) { return; } string filename = ofd.FileName; //IEnumerable<string> lines = File.ReadLines(filename,Encoding.Default); string[] lines = File.ReadLines(filename,Encoding.Default).ToArray(); DateTime starttime = DateTime.Now; DataTable table = new DataTable(); table.Columns.Add("start"); table.Columns.Add("telarea"); table.Columns.Add("teltype"); table.Columns.Add("areanum"); for (int i = 1; i < lines.Length; i++) { //string line = lines.ElementAt(i); string line=lines[i]; string[] segs = line.Split(‘\t‘); string StartTelNum=segs[0]; string TelArea = segs[1]; TelArea = TelArea.Trim(‘"‘); string Teltype = segs[2]; Teltype = Teltype.Trim(‘"‘); string AreaNum = segs[3]; AreaNum = AreaNum.Trim(‘"‘); DataRow row = table.NewRow(); row["start"]=StartTelNum; row["telarea"]=TelArea; row["teltype"]=Teltype; row["areanum"] = AreaNum; table.Rows.Add(row); } using(SqlBulkCopy bulkcopy=new SqlBulkCopy(connStr)) { bulkcopy.DestinationTableName = "TelNum"; bulkcopy.ColumnMappings.Add("start","StartTelNum"); bulkcopy.ColumnMappings.Add("telarea","TelArea"); bulkcopy.ColumnMappings.Add("teltype","TelType"); bulkcopy.ColumnMappings.Add("areanum","AreaNum"); bulkcopy.WriteToServer(table); } TimeSpan time = DateTime.Now - starttime; MessageBox.Show(time.ToString()); //using (SqlConnection conn = new SqlConnection(connStr)) //{ // for (int i = 1; i < lines.Count(); i++) // { // string line = lines.ElementAt(i); // //string line=lines[i]; // string[] segs = line.Split(‘\t‘); // string StartTelNum = segs[0]; // string TelArea = segs[1]; // TelArea = TelArea.Trim(‘"‘); // string Teltype = segs[2]; // Teltype = Teltype.Trim(‘"‘); // string AreaNum = segs[3]; // AreaNum = AreaNum.Trim(‘"‘); // using (SqlCommand cmd=conn.CreateCommand()) // { // cmd.CommandText = "insert into TelNum(StartTelNum,TelArea,TelType,AreaNum) values(@s,@a,@t,@u)"; // cmd.Parameters.Add(new SqlParameter("@s", StartTelNum)); // cmd.Parameters.Add(new SqlParameter("@a",TelArea)); // cmd.Parameters.Add(new SqlParameter("@t", Teltype)); // cmd.Parameters.Add(new SqlParameter("@u",AreaNum)); // cmd.ExecuteNonQuery(); // } // //SqlHelper.ExecuteNonQuery("insert into TelNum(StartTelNum,TelArea,TelType,AreaNum) values(@s,@a,@t,@u)", new SqlParameter("@s", StartTelNum), new SqlParameter("@a", TelArea), new SqlParameter("@t", Teltype), new SqlParameter("@u", AreaNum)); // } //} //foreach (string line in lines) //{ // string[] segs = line.Split(‘\t‘); // string StartTelNum=segs[0]; // string TelArea=segs[1]; // string Teltype=segs[2]; // string AreaNum=segs[3]; // SqlHelper.ExecuteNonQuery("insert into TelNum(StartTelNum,TelArea,TelType,AreaNum) values(@s,@a,@t,@u)",new SqlParameter("@s",StartTelNum),new SqlParameter("@a",TelArea),new SqlParameter("@t",Teltype),new SqlParameter("@u",AreaNum)); //} //MessageBox.Show("一共插入了"+lines.Count()+"条数据"); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。