C#应用程序不会将数据保存到SQL数据库中

时间:2016-05-01 11:32:55

标签: c# sql-server database

使用C#windows窗体应用程序开发员工/学生管理系统 两个开发人员正在研究这个项目,并使用两个版本的visual studio和SQL服务器。 (一台PC使用VS 2013 Pro和MS SQL express 14,其他PC使用VS 2015企业和SQL 14企业)。两台PC都在通过团队基础服务器工作。

C#应用程序与SQL数据库连接,并使用登录界面验证数据检索。用户名和密码存储在表中,登录过程正常。因此,它确保数据库连接没有错误。

问题在于,当我们尝试将数据插入SQL表时,C#在try/catch exceptions中没有显示错误,但数据未存储在数据库中。也使用了command.ExecuteNonQuery()

不确定连接字符串是否存在问题。

这是connectionManager类。

class ConnectionManager
{
    public static SqlConnection connection()
    {
        string connectionString = 
            @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\tdsdb.mdf;Integrated Security=True";
        SqlConnection con = new SqlConnection(connectionString);
        con.Open();
        return con;
    }
}

这是处理SQL插入操作的类。

class Student
{
    public void addStudent(string studid, string fullname, string nameinit, string gend, string dob, int age, string nic, string nicdate, string hotp, string mobtp, string odlno, string odldt, string odlcls, string reqdlcls, int trtds, int active)
    {
        string sql= "INSERT INTO tblStudent VALUES ('"+studid+"','"+fullname+"','"+nameinit+"','"+gend+"','"+dob+"','"+age+"','"+nic+"','"+nicdate+"','"+hotp+"','"+mobtp+"','"+odlno+"','"+odldt+"','"+odlcls+"','"+reqdlcls+"','"+trtds+"','"+active+"')";
        SqlCommand com = new SqlCommand(sql, ConnectionManager.connection());
        com.ExecuteNonQuery();
    }
}

这是处理提交按钮点击操作的类

private void button6_Click(object sender, EventArgs e)
{
    string radtext = "";
    bool isChecked = radioButton1.Checked;
    if(isChecked)
        radtext = radioButton1.Text;
    else
        radtext = radioButton2.Text;

    string hometp = "+94"+textBox8.Text;
    string mobiletp = "+94"+textBox9.Text;

    int activeint = 1;

    string trtdschk = comboBox3.Text;
    int trtds = 0;
    if (trtdschk == "Yes")
        trtds = 1;
    else
        trtds = 0;

    try
    {
        std.addStudent(textBox2.Text, textBox38.Text, textBox4.Text, radtext, dateTimePicker2.Text, int.Parse(textBox6.Text), textBox7.Text, dateTimePicker3.Text, hometp, mobiletp, textBox12.Text, dateTimePicker4.Text, comboBox1.Text, comboBox2.Text, trtds, activeint);
        MessageBox.Show("Personal details has been successfully added to the database!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }            
}

1 个答案:

答案 0 :(得分:0)

AFAIK参数化查询更好。请参阅此通用样本。

 using(SqlConnection openCon=new SqlConnection("your_connection_String"))
    {
      string saveStaff = "INSERT into tbl_staff (staffName,userID,idDepartment) VALUES (@staffName,@userID,@idDepartment)";

      using(SqlCommand querySaveStaff = new SqlCommand(saveStaff))
       {
         querySaveStaff.Connection=openCon;
         querySaveStaff.Parameters.Add("@staffName",SqlDbType.VarChar,30).Value=name;
         .....
         openCon.Open();

         openCon.Close();
       }
     }

另外,请查看此链接以获取有关连接字符串的详细信息。

https://www.connectionstrings.com/sql-server/

相关问题