TXT import data to SQL

Source: Internet
Author: User
Tags readline

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data.SqlClient;usingSystem.IO;namespace_03 importing Data {classProgram {Static voidMain (string[] args) {            stringstr ="Data source=xy-pc;initial catalog=myitcast;integrated security=true"; using(StreamReader reader=NewStreamReader ("333.txt"))            {                stringLine= Reader. ReadLine ();//The first column is finished .                using(SqlConnection con=NewSqlConnection (str)) {con.                    Open (); stringsql ="INSERT into userlogin values (@UserName, @UserPwd)"; Sqlparameter[] PS= {                                            //tell the database that the values stored in my parameters are stored in the table in the nvarchar type                                          NewSqlParameter ("@UserName", System.Data.SqlDbType.NVarChar),NewSqlParameter ("@UserPwd", System.Data.SqlDbType.VarChar)}; using(SqlCommand cmd=NewSqlCommand (Sql,con)) {cmd. Parameters.addrange (PS);//because the first row is a column name and is read only once, it is not placed in the while loop                         while(line = reader.) ReadLine ())! =NULL)                        {                            string[] txts = line. Split (New Char[] {','}, Stringsplitoptions.removeemptyentries); //Replace the parameter with what valueps[0]. Value = txts[1];//name, fill in the statement string sql = "INSERT into userlogin values (@UserName, @UserPwd)"; Txts[0] is the ID, which is the logical primary key identifier, discardedps[1]. Value = txts[2]; Cmd. ExecuteNonQuery ();//loop execution SQL statement String sql = "INSERT into userlogin values (@UserName, @UserPwd)"; }}}} Console . WriteLine ("Learn excavator control computer become Kapaski");        Console.readkey (); }    }}

TXT import data to SQL

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.