![](/img/trans.png)
[英]C# program is looking for sql server compact 3.5, but I'm using 4.0
[英]Struggling to insert record into “SQL Server Compact 4.0 Database” from my C# WinForm Application. (I'm using VS 2010))
在我的C#Winform應用程序中,我編寫了代碼以將記錄插入“ SQL Server Compact 4.0數據庫”。 我也逐行調試了代碼,一切都工作正常(沒有任何錯誤),但是當我檢查數據庫時插入功能之后,發現記錄未插入數據庫中,我很奇怪為什么會發生..!
但我認為這是因為“在嘗試在項目中添加數據庫時出現此錯誤 ”, 這是我將代碼插入數據庫的代碼,
// Retrieve the connection string from the settings file.
string conString = Properties.Settings.Default.snda_dbConnectionString;
//string conString = "Data Source=|DataDirectory|\\db_snda.sdf";
try
{
conString = conString +";Password = test@1;";
// Open the connection using the connection string.
using (SqlCeConnection con = new SqlCeConnection(conString))
{
con.Open();
// Read in all values in the table.
using (SqlCeCommand cmd = new SqlCeCommand("INSERT INTO tbl_user_master" + "(user_id, password, user_type, user_title, first_name, middle_name, last_name, gender, dob, mobile_no, email_id, plant_code, div_code, region_code, reporting_to, releaving_date, created_date)" + " VALUES(@user_id, @password, @user_type, @user_title, @first_name, @middle_name, @last_name, @gender, @dob, @mobile_no, @email_id, @plant_code, @div_code, @region_code, @reporting_to, @releaving_date, @created_date)", con))
{
cmd.Parameters.AddWithValue("@user_title", strTitle);
cmd.Parameters.AddWithValue("@first_name", strFirstName);
cmd.Parameters.AddWithValue("@middle_name", strMiddleName);
cmd.Parameters.AddWithValue("@last_name", strLastName);
cmd.Parameters.AddWithValue("@gender", strGender);
cmd.Parameters.AddWithValue("@user_type", strUserType);
cmd.Parameters.AddWithValue("@plant_code", strPlantCode);
cmd.Parameters.AddWithValue("@div_code", strDivCode);
cmd.Parameters.AddWithValue("@region_code", strRegionCode);
cmd.Parameters.AddWithValue("@reporting_to", strReportingTo);
cmd.Parameters.AddWithValue("@user_id", strUserName);
cmd.Parameters.AddWithValue("@password", Encrypt(strPassword)); //Encrypt(strPassword)
cmd.Parameters.AddWithValue("@email_id", strEmailId);
cmd.Parameters.AddWithValue("@mobile_no", strMobileNo);
cmd.Parameters.AddWithValue("@dob", strDOB);
cmd.Parameters.AddWithValue("@created_date", strCreatedDate);
cmd.Parameters.AddWithValue("@releaving_date", strReleavingDate);
cmd.ExecuteNonQuery();
}
con.Close();
XtraMessageBox.Show("User Created Successfully.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
ResetAfterSubmit();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
謝謝...!
之所以會令人發笑,是因為您混淆了數據庫-您檢查的數據庫不是在插入程序期間使用的數據庫,因為當您開始調試程序時,將在輸出文件夾中創建一個副本-然后在停止程序時將其丟棄。
這並不是一個罕見的問題,並且很好地表明了有人沒有使用搜索功能來找到解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.