SQL异常未处理 - System.Data.dll中出现 System.Data.SqlClient.SqlException 类型的未处理异常
public DataSet getCustomers()
{
string Connectionstring = ConfigurationManager.ConnectionStrings["DbConn"].ToString();
SqlConnection objConnection = new SqlConnection(Connectionstring);
objConnection.Open();
SqlCommand objCommand = new SqlCommand("Select * from Customer '" ,
objConnection);
DataSet objDataSet = new DataSet();
SqlDataAdapter objAdapter = new SqlDataAdapter(objCommand);
objAdapter.Fill(objDataSet);
objConnection.Close();
return objDataSet;
}
答案 0 :(得分:2)
在客户' "
之后看起来像是一个额外的撇号:
SqlCommand objCommand = new SqlCommand("Select * from Customer '", objConnection);
答案 1 :(得分:0)
删除'来自查询命令:
SqlCommand objCommand = new SqlCommand("Select * from Customer" , objConnection);
答案 2 :(得分:0)
将代码包含在Try Catch中:
public DataSet getCustomers()
{
try
{
string Connectionstring = ConfigurationManager.ConnectionStrings["DbConn"].ToString();
SqlConnection objConnection = new SqlConnection(Connectionstring);
objConnection.Open();
SqlCommand objCommand = new SqlCommand("Select * from Customer '",
objConnection);
DataSet objDataSet = new DataSet();
SqlDataAdapter objAdapter = new SqlDataAdapter(objCommand);
objAdapter.Fill(objDataSet);
objConnection.Close();
return objDataSet;
}
catch (Exception ex)
{
Trace.Write(ex.Message);
return null;
}
}
ex.Message将是:字符串'后的未闭合引号。
只需删除客户
后的'即可