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

asp.net读取txt并导入数据库

时间:2017-11-01 12:09:05      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:with   nta   cti   cut   导入数据库   event   insert   object   txt   

protected void Button1_Click(object sender, EventArgs e)
    {
        
        System.IO.StreamReader my = new System.IO.StreamReader("C:/Users/Administrator/Desktop/2.txt", System.Text.Encoding.Default);
        string line;
        while ((line = my.ReadLine()) != null)
        {
            Response.Write(line + "<br />");
      string[] mm = line.Split(‘|‘);
            Insert(mm);
        }  
            
        my.Close();


    }

    private void Insert(string[] mm)
    {
        string name = mm[0];
        string dianhua = mm[1];
        string dizhi = mm[2];
        string nianling = mm[3];
        string xingbie = mm[4];
        string strcon = ConfigurationManager.ConnectionStrings["strcn"].ConnectionString;
        SqlConnection sqlcnn = new SqlConnection(strcon);
        SqlCommand sqlcmm = new SqlCommand();
        sqlcmm.Connection = sqlcnn;
        sqlcnn.Open();


            sqlcmm.CommandText = "insert into users(name,hao,age,sex,dress) values(@name,@hao,@age,@sex,@dress)";
          
            sqlcmm.Parameters.AddWithValue("@hao",dianhua);
            sqlcmm.Parameters.AddWithValue("@name", name);
            sqlcmm.Parameters.AddWithValue("@dress",dizhi);
            sqlcmm.Parameters.AddWithValue("@age", nianling);
            sqlcmm.Parameters.AddWithValue("@sex", xingbie);
            int rest = sqlcmm.ExecuteNonQuery();
            if (rest > 0)
            {
                this.Response.Write("保存成功!");
            }
        
        else
        {
            this.Response.Write("错误!");
        }
            sqlcnn.Close();
        
    }

  导包

using System.Data.SqlClient;
using System.Configuration;

  

asp.net读取txt并导入数据库

标签:with   nta   cti   cut   导入数据库   event   insert   object   txt   

原文地址:http://www.cnblogs.com/njy888888/p/7765570.html

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