数据库表没有更新c#

时间:2014-04-13 01:54:06

标签: c# sql sql-server database sql-insert

我有form和dataGridView连接到数据库中的表, 当我添加记录时它显示在dataGridView中,但是当我停止运行时,表返回空!

这是代码:

        string employeeName =  "person";
        string employeeUserName ="person1";
        string emplpyeeNotes = "empty";
        string employeePassword = "123";
        int employeeSalary = 4000;
        try
        {

           SqlConnection cn = new SqlConnection(Properties.Settings.Default.StudentManagementDBConnectionString);
           cn.Open();

           string sql = "INSERT INTO Employee (Name,Salary,UserName,Password,Notes) VALUES(@name, @salary, @userName, @password, @notes)";
           SqlCommand exsql = new SqlCommand(sql, cn);
           exsql.Parameters.AddWithValue("@name", employeeName);
           exsql.Parameters.AddWithValue("@salary", employeeSalary);
           exsql.Parameters.AddWithValue("@userName", employeeUserName);
           exsql.Parameters.AddWithValue("@password", employeePassword);
           exsql.Parameters.AddWithValue("@notes", emplpyeeNotes);
           exsql.ExecuteNonQuery();

           MessageBox.Show("employee added Successfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
           this.employeeTableAdapter.Fill(this.studentManagementDBDataSet.Employee);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

3 个答案:

答案 0 :(得分:1)

尝试使用像这样的参数化查询:

string employeeName = textBoxName.Text;
string employeeUserName = textBoxUserName.Text;
string emplpyeeNotes = richTextBoxNotes.Text;
string employeePassword = textBoxPassword.Text;

SqlConnection cn = new SqlConnection(global::StudentManagementProject.Properties.Settings.Default.StudentManagementDBConnectionString);

    try
    {
        string sql = "INSERT INTO Employee (Name,Salary,UserName,Password,Notes) VALUES(@name, @salary, @userName, @password, @notes)";
        SqlCommand exsql = new SqlCommand(sql, cn);
        cn.Open();
        exsql.Parameters.AddWithValue("@name", employeeName);
        exsql.Parameters.AddWithValue("@salary", 2000);
        exsql.Parameters.AddWithValue("@userName", employeeUserName);
        exsql.Parameters.AddWithValue("@password", employeePassword);
        exsql.Parameters.AddWithValue("@notes", emplpyeeNotes);
        int result=exsql.ExecuteNonQuery();
        if (result > 0 )
        {
        MessageBox.Show("employee added Successfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }else
        { 
          MessageBox.Show("employee insertion failed", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        cn.Close();
    }

答案 1 :(得分:0)

您的数据库正在更新,但Visual Studio不会复制您的数据库"解决方案"目录到"调试"或" Bin"目录。如果你去你的" Debug"目录并打开你的数据库文件,你会看到那里的变化

答案 2 :(得分:0)

解决方法是更改​​连接字符串并将其设置为mdf文件的路径,方法是从数据集的属性中复制它。