码迷,mamicode.com
首页 > 数据库 > 详细

c#插入数据库

时间:2015-06-07 15:43:33      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

 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()+"条数据");
        }

 

c#插入数据库

标签:

原文地址:http://www.cnblogs.com/lushixiong/p/4558466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!