当前位置:Gxlcms > 数据库问题 > asp.net读取txt并导入数据库

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

时间:2021-07-01 10:21:17 帮助过:20人阅读

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   

人气教程排行