繁体   English   中英

努力将记录从我的C#WinForm应用程序插入“ SQL Server Compact 4.0数据库”。 (我正在使用VS 2010)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM