ASP. NET reads txt and imports the database

Source: Internet
Author: User

protected void Button1_Click (object sender, EventArgs e) {System.IO.StreamReader my = new System.IO.Str        Eamreader ("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 ("Saved successfully!            "); } else {this. Response.Write ("Error!        "); } sqlcnn.            Close (); }

Guide Package

Using system.data.sqlclient;using system.configuration;

  

ASP. NET reads txt and imports the database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.