C# 读取TXT文本数据 添加到数据库

   protected void Button1_Click(object sender, EventArgs e)
        {
            //使用FileStream读取文件  
            FileStream fileStream = File.OpenRead(FileUpload1.PostedFile.FileName);
            StreamReader reader = new StreamReader(fileStream);
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString); 
            conn.Open();
            //向数据库插入数据    
            SqlCommand command = conn.CreateCommand();
            command.CommandText = insertsql;  
            string line = null;
            while ((line = reader.ReadLine()) != null)
            {
                string[] str = line.Split('|');
                if (str.Length<13)//跳过第一行数据
                {
                    continue;
                }
                string RefParma = str[0];
                string requestid = str[1];
                string agency_name = str[2];
                string channel_coding = str[3];
                string phone = str[4];
                string province_code = str[5];
                string cities_code = str[6];
                string TrafficSts = (str[7] == "20101") ? "1" : "-1";
                string product_type_code = str[8];
                string package_name = str[9];
                string processsing_date = str[10];
                string processsing_time = str[11];
                string money = str[12];
                //RefParma, requestid, agency_name, channel_coding, phone, province_code, cities_code, TrafficSts, product_type_code, package_name, processsing_date, processsing_time, money
                command.Parameters.Clear(); //每次插入都要清除参数  
                command.Parameters.Add(new SqlParameter("RefParma", RefParma));
                command.Parameters.Add(new SqlParameter("requestid", requestid));
                command.Parameters.Add(new SqlParameter("agency_name", agency_name));
                command.Parameters.Add(new SqlParameter("channel_coding", channel_coding));
                command.Parameters.Add(new SqlParameter("phone", phone));
                command.Parameters.Add(new SqlParameter("province_code", province_code));
                command.Parameters.Add(new SqlParameter("cities_code", cities_code));
                command.Parameters.Add(new SqlParameter("TrafficSts", TrafficSts));
                command.Parameters.Add(new SqlParameter("product_type_code", product_type_code));
                command.Parameters.Add(new SqlParameter("package_name", package_name));
                command.Parameters.Add(new SqlParameter("processsing_date", processsing_date));
                command.Parameters.Add(new SqlParameter("processsing_time", processsing_time));
                command.Parameters.Add(new SqlParameter("money", money));
                int tem=command.ExecuteNonQuery();  
            }
            Response.Write("<script>alert('数据导入完成');</script>");
            fileStream.Close();
            reader.Close();
            conn.Dispose();  
        }
        public string insertsql = "insert into Order_Table (RefParma,requestid,agency_name,channel_coding,phone,province_code,cities_code,TrafficSts,product_type_code,package_name,processsing_date,processsing_time,money) values (@RefParma, @requestid, @agency_name, @channel_coding, @phone, @province_code, @cities_code, @TrafficSts, @product_type_code, @package_name, @processsing_date, @processsing_time, @money)";
    }

  

转载于:https://www.cnblogs.com/caipz/p/6773938.html

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值