Source code record
Add
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); }
Code in the import button
Using system; using system. windows. forms; using system. data. sqlclient; using system. io; log on to the bathroom using namespace. Hi {public partial class form1: FORM {public form1 () {initializecomponent ();} private void button#click (Object sender, eventargs e) {If (ofdimport. showdialog () = dialogresult. OK) {using (filestream = file. openread (ofdimport. filename) {using (streamreader = new streamr Eader (filestream) {string line = NULL; while (line = streamreader. Readline ())! = NULL) {string [] STRs = line. split ('|'); string name = STRs [0]; int age = convert. toint32 (STRs [1]); Using (sqlconnection conn = new sqlconnection (@ "Data Source =. \ sqlexpress; attachdbfilename = | datadirectory | \ db1.mdf; Integrated Security = true; user instance = true ") {Conn. open (); Using (sqlcommand cmd = Conn. createcommand () {cmd. commandtext = "insert into t_persons (name, age) values (@ name, @ age)"; cmd. parameters. add (New sqlparameter ("name", name); cmd. parameters. add (New sqlparameter ("Age", age); cmd. executenonquery ();}}}}}}}}}