【发布时间】: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