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