【问题标题】:database Table is not updating c#数据库表没有更新c#
【发布时间】:2014-04-13 01:54:05
【问题描述】:

我已将表单和 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);
        }

【问题讨论】:

  • tyr 将 ExecuteNonQuery 分配给一个 int 变量,看看你得到了什么数字。像 int i= exsql.ExecuteNonQuery();
  • 我写道: int x = exsql.ExecuteNonQuery(); MessageBox.Show(x.ToString());它显示错误消息@Sas
  • 你不检查调用的结果,所以它当然总是说它有效。
  • ... 错误信息是?
  • *1。 *您的连接字符串是否指向正确的数据库 *2。 *正在运行的交易。 3. 当您捕获查询时,SQL 分析器会说什么? 4. 你有什么触发器?

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


【解决方案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();
    }

【讨论】:

  • 嗯,是的,他应该是,但他的实际问题并不明显。我还不太精通 C#/db 接口,但看起来你可能已经删除了“归化”/国际安全字符串(某些前面的 'N' 字符)字符串) - 或者参数化是否自动处理?哦,你仍然不应该笼统地捕捉异常。
【解决方案2】:

您的数据库正在更新,但 Visual Studio 并未将您的数据库从“Solution”目录复制到“Debug”或“Bin”目录。如果你去你的“调试”目录并从那里打开你的数据库文件,你会看到那里的变化

【讨论】:

  • 在“解决方案资源管理器”中右键单击您的数据库并单击复制,然后右键单击“调试”文件夹并单击粘贴。您可能必须在解决方案资源管理器中显示隐藏文件夹。
【解决方案3】:

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

【讨论】:

  • 此解决方案是否指的是您从其他答案之一中学到的东西?如果是这样,请将最能回答您的问题的答案标记为已接受,并对您认为有用的任何答案进行投票。如果此解决方案不是基于任何答案,请更具体地解释并附上代码,然后在 48 小时等待期后将您的答案标记为已接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多