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

时间:2017-11-10 00:55:18

标签: c# asp.net sqlcommand sqlexception sqldataadapter

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;

    }

3 个答案:

答案 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将是:字符串'后的未闭合引号。

只需删除客户

后的'即可