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();
       
    }

posted on 2013-07-25 16:42  努力实现目标  阅读(2777)  评论(0编辑  收藏  举报