我正在寫一個方法插入一個學生到一個包含有關學生信息表中的本地SQL數據庫,將數據插入SQL數據庫:無法使用C#
public void AddStudent(string name, string teachName, string pass)
{
string dbfile = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\Logo.sdf";
SqlCeConnection connection = new SqlCeConnection("Data Source=" + dbfile + "; Password = 'dbpass2011!'");
connection.Open();
SqlCeTransaction transaction = connection.BeginTransaction();
SqlCeCommand command = connection.CreateCommand();
command.Transaction = transaction;
command.CommandText = "INSERT INTO Students VALUES ('@name', '@id', '@pass', '@tname')";
command.Parameters.Add("@name", name);
command.Parameters.Add("@id", this.ID);
command.Parameters.Add("@pass", MD5Encrypt.MD5(pass));
command.Parameters.Add("@tname", teachName);
command.Prepare();
command.ExecuteNonQuery();
transaction.Commit();
connection.Dispose();
connection.Close();
}
每當我用這個,它永遠不會當我查看數據庫中的學生表的內容時,將數據插入到表中。最初我有這個返回一個int,所以我可以看到它影響了多少行,它總是返回1,所以我知道它正在工作。
我在尋找這個問題的答案,對類似問題的回答是那個人問的是看錯了.sdf文件。我確定我正在查看正確的文件。
任何反饋將不勝感激!
解決了我的問題。顯然,MSVS每次構建解決方案時都會將頂級sdf文件複製到bin \ Debug文件夾中。因此,如果我希望我的程序運行時所做的更改得到保存,則必須在重建解決方案之前將頂級sdf設置爲bin \ Debug文件夾中的一個。 – user1057068