将txt文档中内容导入到数据库,并且显示一个对话框,提示用户选择文件的位置

OpenFileDialog opfd = new OpenFileDialog();
            opfd.ShowDialog();
            string lujing = opfd.FileName;
            if (lujing != "")
            {
                StreamReader sr = new StreamReader(new FileStream(lujing, FileMode.Open, FileAccess.Read), System.Text.Encoding.Default);
                string sLine = "";
                ArrayList al = new ArrayList();


                string connstr = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(connstr))
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        while (sLine != null)
                        {


                            sLine = sr.ReadLine();
                            if (sLine != null)
                                al.Add(sLine);
                        }
                        sr.Close();
                        foreach (string sOutput in al)
                        {
                            string[] strs = sOutput.Split('|');
                            string name = "'" + strs[0] + "'";
                            string phone1 = "'" + strs[1] + "'";
                            string phone2 = "'" + strs[2] + "'";
                            string mail = "'" + strs[3] + "'";
                            string mobile1 = "'" + strs[4] + "'";
                            string mobile2 = "'" + strs[5] + "'";
                            string address = "'" + strs[6] + "'";
                            string youbian = "'" + strs[7] + "'";
                            string carcard = "'" + strs[8] + "'";
                            string chejiahao = "'" + strs[9] + "'";
                            string chexing = "'" + strs[10] + "'";
                            string mendian = "'" + strs[11] + "'";
                            string buydate = "'" + strs[12] + "'";
                            string suggestion = "'" + strs[13] + "'";
                            string beizhu = "'" + strs[14] + "'";
                            string huifang = "'" + strs[15] + "'";
                            cmd.CommandText = "insert into T_Customer(CustomName,PhoneOne,PhoneTwo,Mail,MobileOne,MobileTwo,Address,Youbian,CarCard,ChejiaNum,chexing,Mendian,BuyDate,Suggestion,Beizhu,IsVisit)values(" + name + "," + phone1 + "," + phone2 + "," + mail + "," + mobile1 + "," + mobile2 + "," + address + "," + youbian + "," + carcard + "," + chejiahao + "," + chexing + "," + mendian + "," + buydate + "," + suggestion + "," + beizhu + "," + huifang + ")";
                            cmd.ExecuteNonQuery();
                        }
                    }
                }
                MessageBox.Show("数据导入成功!");
            }
            else
            {
                return;
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值