[英]Struggling to insert record into “SQL Server Compact 4.0 Database” from my C# WinForm Application. (I'm using VS 2010))
In my C# Winform Application I have written a code to insert record into the "SQL Server Compact 4.0 Database". 在我的C#Winform应用程序中,我编写了代码以将记录插入“ SQL Server Compact 4.0数据库”。 Also I had debug code line-by-line everything is working fine (without any error) but after insert functionality when I checked my database I found that record is not inserted into the database, I'm strange why it is happening..!
我也逐行调试了代码,一切都工作正常(没有任何错误),但是当我检查数据库时插入功能之后,发现记录未插入数据库中,我很奇怪为什么会发生..!
But I think it is happening because, "when I tried to add database in my project I got this error " Following is my code to insert record into the database-- 但我认为这是因为“在尝试在项目中添加数据库时出现此错误 ”, 这是我将代码插入数据库的代码,
// 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());
}
Thank you...! 谢谢...!
It is hapening because you mix up databases - the one you check is not the one that is used during insert as likely when you start the debugging of the program a copys created in the output folder - and then discarded when you stop the program. 之所以会令人发笑,是因为您混淆了数据库-您检查的数据库不是在插入程序期间使用的数据库,因为当您开始调试程序时,将在输出文件夹中创建一个副本-然后在停止程序时将其丢弃。
This is not exactly a rare problem and a good indication of someone not using a search function to find a solution. 这并不是一个罕见的问题,并且很好地表明了有人没有使用搜索功能来找到解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.