F 5*_*505 1 c# sql sql-server visual-studio-2012
我是c#的新开发人员,我创建了ac #project,我需要访问SQL数据库来执行SELECT语句
我在这个图中得到了这个错误
我的连接声明是正确的,所以它有什么问题?!
我尝试了提到的解决方案,我得到了这个错误

有谁知道如何处理它?
错误告诉您该怎么做,您的连接尚未打开.打开就像:
con.Open();
Run Code Online (Sandbox Code Playgroud)
在执行命令之前.
为您的代码做一些事情,使用参数化查询,这将使您免于SQL注入,也使用using将确保处理连接对象的语句.
using (SqlConnection con = new SqlConnection("connection string"))
using(SqlCommand cmd = new SqlCommand("SELECT EmpName FROM Employee WHERE EmpID=@EmpID", con))
{
cmd.Parameters.AddWithValue("@EmpID", id.Text);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
//..... your rest of the code
}
Run Code Online (Sandbox Code Playgroud)
编辑:
对于您编辑过的部分问题,您的SQL Server问题不允许远程连接.你必须启用它.
| 归档时间: |
|
| 查看次数: |
70 次 |
| 最近记录: |