Copy codeThe Code is as follows: private void button#click (object sender, EventArgs e)
{
If (openFileDialog1.ShowDialog () = DialogResult. OK)
{
Using (FileStream fs = File. OpenRead (openFileDialog1.FileName ))
{
Using (StreamReader sr = new StreamReader (fs, System. Text. Encoding. GetEncoding ("GB2312 ")))
{
// <Span style = "color: # 3333ff;"> you must set the character Encoding System. Text. Encoding. GetEncoding ("GB2312 "),
Otherwise, the name in string name = arr [0] is garbled </span> using (SqlConnection conn = new SqlConnection (@ "Data Source =. \ SQLEXPRESS; AttachDbFilename = '| DataDirectory | \ dd. mdf ';
Integrated Security = True; User Instance = True "))
{
// <Span style = "color: # 3333ff;"> DataDirectory indicates the absolute path of the database, and the Program in winForm. code must be added to cs; otherwise, yes. NET is found in the database is a problem, really do not understand can go to the blog Park to look at why yourself </span>
Conn. Open ();
Using (SqlCommand cmd = conn. CreateCommand ())
{
Cmd. CommandText = "insert into T_Persons values (@ Name, @ Age )";
String line = "";
While (line = sr. ReadLine ())! = Null)
{
String [] arr = line. Split ('| ');
String name = arr [0];
Int age = Convert. ToInt32 (arr [1]);
Cmd. Parameters. Clear (); // don't forget
Cmd. Parameters. Add (new SqlParameter ("Name", name ));
Cmd. Parameters. Add (new SqlParameter ("Age", age ));
Cmd. ExecuteNonQuery ();
}
}
}
}
}
MessageBox. Show ("txt imported to the database! ");
}
}
Private void button2_Click (object sender, EventArgs e)
{
If (saveFileDialog1.ShowDialog () = DialogResult. OK)
{
Using (FileStream fs = File. OpenWrite (saveFileDialog1.FileName ))
{
Using (StreamWriter sw = new StreamWriter (fs, System. Text. Encoding. GetEncoding ("GB2312 ")))
{
Using (SqlConnection conn = new SqlConnection (@ "Data Source =. \ SQLEXPRESS; AttachDbFilename = '| DataDirectory | \ dd. mdf '; Integrated Security = True; User Instance = True "))
{
Conn. Open ();
Using (SqlCommand cmd = conn. CreateCommand ())
{
Cmd. CommandText = "select * from T_Persons ";
Using (SqlDataReader sdr = cmd. ExecuteReader ())
{
While (sdr. Read ())
{
String name = sdr. GetString (sdr. GetOrdinal ("Name "));
Int age = sdr. GetInt32 (sdr. GetOrdinal ("Age "));
String line = name + "|" + age;
Sw. WriteLine (line );
Sw. Flush ();
}
}
}
}
}
}
MessageBox. Show ("data exported to txt is successful! ");
}
}
</Span>
This is the code to be added to the Program. cs file. It is only valid for winForm and win console:
Copy codeThe Code is as follows: static void Main ()
{
String dataDir = AppDomain. CurrentDomain. BaseDirectory;
If (dataDir. EndsWith (@ "\ bin \ Debug \") | dataDir. EndsWith (@ "\ bin \ Release \"))
{
DataDir = System. IO. Directory. GetParent (dataDir). Parent. Parent. FullName;
AppDomain. CurrentDomain. SetData ("DataDirectory", dataDir );
}
Application. EnableVisualStyles ();
Application. SetCompatibleTextRenderingDefault (false );
Application. Run (new Form1 ());
}