"ADO" 3, import data from TXT to database

Source: Internet
Author: User

Database fields and Types
ID int,name nvarchar (), age int

TXT text content is

Small High-20
Zhang San-18
John Doe-19

Private voidBtninput_click (Objectsender, EventArgs e) {    if(Opfile.showdialog ()! = DialogResult.OK)//Determine whether the user clicks OK or cancel, without a certain point, returns the program     {           return; }     using(FileStream fstream = File.openread (opfile.filename))//open a file for reading     {        //define character encoding to GB2312        using(StreamReader Stremreader =NewStreamReader (Fstream,encoding.getencoding ("GB2312")) {using(SqlConnection conn =NewSqlConnection (@"server=.; Database=mytest;uid=sa;pwd=gao") {Conn.             Open (); using(SqlCommand cmd =Conn. CreateCommand ()) {Cmd.commandtext="INSERT INTO T_age (name,age) VALUES (@n,@a)"; stringline =NULL;  while(line = Stremreader.readline ())! =NULL)//reads a row of data in the text, when it is not empty               {                 string[] STRs = line. Split ('-');//split string by "-" character                 stringName = strs[0]; intAge = Convert.ToInt32 (strs[1]); Cmd. Parameters.clear (); //parameters cannot be added repeatedly, this SqlCommand object is always used in the whileCmd. Parameters.Add (NewSqlParameter ("@N", name)); Cmd. Parameters.Add (NewSqlParameter ("@A", age)); Cmd.                 ExecuteNonQuery (); } }}} MessageBox.Show ("Import Successful! "); }  }

"ADO" 3, import data from TXT to database

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.