我附加了基於服務的數據庫到我的Windows應用程序,我使用下面的代碼來保存數據,它工作正常,但是當我關閉應用程序並再次打開時,我保存的數據被自動清除。如何將數據作爲基於服務的數據庫連接時將數據保存到數據庫?
如何永久保存數據...
string c = Application.StartupPath + "\\Stock.mdf";
SqlConnection con = new SqlConnection(@"AttachDbFilename='"+c+"';Integrated Security=True;Connect Timeout=30;User Instance=True");
con.Open();
SqlCommand cmd = new SqlCommand("Insert into Codedetails values('TF','" + txt_productcode.Text + "','" + txt_productname.Text + "','" + txt_brandcode.Text + "','" + txt_brandname.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
附註:請不要使用string.Concat/+來構造SQL查詢 - http://stackoverflow.com/questions/332365/xkcd-sql-injection-please-explain –
嗨阿列克謝列文科夫,我不明白yu提到這個鏈接 –