sql-server - 收到错误(System.InvalidOperationException:'ExecuteNonQuery:连接属性尚未初始化。')
问题描述
这是我的代码:
private void btnRegister_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=register;Integrated Security=True"); //datasource
SqlCommand cmd = new SqlCommand(@"INSERT INTO [dbo].[register]
([firstname], [lastname], [address], [gender], [email], [phone], [username], [password])
VALUES ('" + txtFname.Text + "', '" + txtLname.Text + "', '" + txtAdd.Text + "', '" + cmbGender.SelectedItem.ToString() + "', '" + txtEmail.Text + "', '" + txtPhone.Text + "', '" + txtUser.Text + "', '" + txtPass.Text + "')");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Registered successfully"); //end of code
}
此代码应该从 Windows 表单将值添加到数据库表中,但它给出了这个错误:
System.InvalidOperationException:“ExecuteNonQuery:连接属性尚未初始化。”
解决方案
推荐阅读
- c# - 通过 C# 从 Access 中提取 VBA 代码
- opc - 与 Milo OPC Client 连接时 PLC 拒绝连接
- python - 在 Python 中使用 ctypes 实现 NtCreateNamedPipeFile
- python - @api.depends 非存储字段不起作用
- sapui5 - 绑定northwind odata服务到ui5?
- ios - 将 Swift 自定义框架与 Cordova 插件集成
- php - 用php编辑/更新行不起作用/未定义的变量
- layout - 如何在 vaadin 水平布局上重叠两个组件。
- vba - 在连接单元格的情况下按字母顺序对列进行排序
- java - 有没有办法像 Java 中的模拟时钟指针一样旋转 Rectangles2D?