发生了一个'System.Data.SqlClient.SqlException'类型的异常,在System.Data.dll中。

9
当我执行下面的代码时,出现了以下错误信息:
“System.Data.SqlClient.SqlException”类型的异常发生在“System.Data.dll”中,但未在用户代码中处理
附加信息:附近的语法不正确。
下面是代码:
string position;

SqlConnection con = new SqlConnection("server=free-pc\\FATMAH; Integrated Security=True; database=Workflow; ");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT EmpName FROM Employee WHERE EmpID=" + id.Text, con);

SqlDataReader Read = cmd.ExecuteReader();

if (Read.Read()==true)
{
    position = Read[0].ToString();
    Response.Write("User Registration successful");
}
else
{
    Console.WriteLine("No Employee found.");
}

Read.Close(); 

什么导致了这个问题,我该如何解决它?

TextBox 的 ID 提供的文本是什么? - nstosic
5
不要将输入连接到 SQL 中非常重要,应该使用参数。否则,可能会偶尔失败(希望您的公司没有姓 O'Neil 的人),或者成为安全漏洞(参见:“Bobby Tables”)。 - Marc Gravell
6个回答

14

你的代码存在一些问题。首先,我建议使用参数化查询以避免 SQL 注入攻击,并且参数类型可以由框架自动检测:

var cmd = new SqlCommand("SELECT EmpName FROM Employee WHERE EmpID = @id", con);
cmd.Parameters.AddWithValue("@id", id.Text);

其次,如果您只对查询返回的一个值感兴趣,最好使用ExecuteScalar

var name = cmd.ExecuteScalar();

if (name != null)
{
   position = name.ToString();
   Response.Write("User Registration successful");
}
else
{
    Console.WriteLine("No Employee found.");
}

最后一步是将 SqlConnectionSqlCommand 放入 using 中,这样任何被它们使用的资源都将被释放:

string position;

using (SqlConnection con = new SqlConnection("server=free-pc\\FATMAH; Integrated Security=True; database=Workflow; "))
{
  con.Open();

  using (var cmd = new SqlCommand("SELECT EmpName FROM Employee WHERE EmpID = @id", con))
  {
    cmd.Parameters.AddWithValue("@id", id.Text);
  
    var name = cmd.ExecuteScalar();
  
    if (name != null)
    {
       position = name.ToString();
       Response.Write("User Registration successful");
    }
    else
    {
        Console.WriteLine("No Employee found.");
    }
  }
}

8
我认为你的EmpID列是字符串类型,并且你忘记在值中使用' '
因为当你写EmpID=" + id.Text时,你的命令看起来像EmpID = 12345而不是EmpID = '12345' 请将你的SqlCommand更改为:
SqlCommand cmd = new SqlCommand("SELECT EmpName FROM Employee WHERE EmpID='" + id.Text +"'", con);

或者更好的方法是,您可以(也应该)始终使用{{link1:参数化查询}}。这种字符串连接容易受到{{link2:SQL注入}}攻击。

SqlCommand cmd = new SqlCommand("SELECT EmpName FROM Employee WHERE EmpID = @id", con);
cmd.Parameters.AddWithValue("@id", id.Text);

我认为你的EmpID列保存了员工的ID,因此它的类型应该是一些数字类型而不是字符。

3

试试这个

SqlCommand cmd = new SqlCommand("SELECT EmpName FROM Employee WHERE EmpID=@id", con);
cmd.Parameters.AddWithValue("id", id.Text);

1
使用try-catch语句来查看您遇到的真正错误。
 try
{
  //Your insert code here
}
catch (System.Data.SqlClient.SqlException sqlException)
{
  System.Windows.Forms.MessageBox.Show(sqlException.Message);
}

0

在 System.Data.dll 中发生了未处理的类型为 'System.Data.SqlClient.SqlException' 的异常

    private const string strconneciton = "Data Source=.;Initial Catalog=Employees;Integrated Security=True";
    SqlConnection con = new SqlConnection(strconneciton);

    private void button1_Click(object sender, EventArgs e)
    {

        con.Open();
        SqlCommand cmd = new SqlCommand("insert into EmployeeData (Name,S/O,Address,Phone,CellNo,CNICNO,LicenseNo,LicenseDistrict,LicenseValidPhoto,ReferenceName,ReferenceContactNo) values ( '" + textName.Text + "','" + textSO.Text + "','" + textAddress.Text + "','" + textPhone.Text + "','" + textCell.Text + "','" + textCNIC.Text + "','" + textLicenseNo.Text + "','" + textLicenseDistrict.Text + "','" + textLicensePhoto.Text + "','" + textReferenceName.Text + "','" + textContact.Text + "' )", con);
        cmd.ExecuteNonQuery();
        con.Close();
    }

-2
using (var cmd = new SqlCommand("SELECT EmpName FROM [Employee] WHERE EmpID = @id", con))

在表名周围加上[] ;)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接