1

我在一个类(数据库)中有这段代码,我得到了

并非所有路径都返回值。

任何帮助将不胜感激。

public static DataSet DELETE_PDT(String rowid)
{
    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand("sp_DELETE_PDT", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@rowid", SqlDbType.Int).Value = rowid;

    con.ConnectionString = ConfigurationManager.ConnectionStrings["WMS"].ConnectionString;

    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        con.Close();
    }
    catch (Exception ex)
    {
        throw new Exception("Error while deleting record. Please contact your System Administrator", ex);
    }
}
4

4 回答 4

0

您的方法应该有 areturn value因为您已将其声明为 return Dataset

例子,

return new Dataset();

如果您不想看到错误消息,请更改

public static DataSet DELETE_PDT(String rowid)
{
   // statements
}

public static void DELETE_PDT(String rowid)
{
   // statements
}

更新 1

根据您的评论,我将对其进行修改以再次返回字符串值。

public static string DELETE_PDT(String rowid)
{
    using (SqlConnection con = new SqlConnection())
    {
        con.ConnectionString = ConfigurationManager.ConnectionStrings["WMS"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand("sp_DELETE_PDT", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@rowid", SqlDbType.Int).Value = rowid;
            try
            {
                if (con.State == ConnectionState.Open)
                con.Close();
                con.Open();
                cmd.ExecuteNonQuery();
                return "Record has been deleted";
            }
            catch (Exception ex)
            {
                return "Error while deleting record. Please contact your System Administrator";
            }
        }
    }
}
于 2012-09-24T02:38:48.623 回答
0

您已经声明了返回 a DataSet... 的方法,但您什么也没返回。所以......您需要返回一个实例DataSet或更改您的方法签名。

于 2012-09-24T02:39:07.430 回答
0

您已声明该方法应返回 a DataSet,但它不返回任何内容(这对于该方法的作用是合乎逻辑的)。将签名更改为:

public static void DELETE_PDT(String rowid)

旁注:为避免出现异常离开未关闭的连接,您可以使用using. 它确保无论发生什么,命令和连接总是被处理:

public static void DELETE_PDT(String rowid) {
  using (SqlConnection con = new SqlConnection()) {
    con.ConnectionString = ConfigurationManager.ConnectionStrings["WMS"].ConnectionString;
    using (SqlCommand cmd = new SqlCommand("sp_DELETE_PDT", con)) {
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Parameters.Add("@rowid", SqlDbType.Int).Value = rowid;
      try {
        con.Open();
        cmd.ExecuteNonQuery();
      } catch (Exception ex) {
        throw new Exception("Error while deleting record. Please contact your System Administrator", ex);
      }
    }
  }
}
于 2012-09-24T02:40:18.340 回答
0

我已经重构了你的代码;试试这个:

public static void DELETE_PDT(String rowid) {
    using(SqlConnection con = new SqlConnection()) {
        con.ConnectionString = ConfigurationManager.ConnectionStrings["WMS"].ConnectionString;
        using(SqlCommand cmd = new SqlCommand("sp_DELETE_PDT", con)) {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@rowid", SqlDbType.Int).Value = rowid;

            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while deleting record. Please contact your System Administrator", ex);
            }
        }
    }
}
于 2012-09-24T02:42:17.690 回答