我在类(DataBase)中有这段代码,而我正在
并非所有路径都返回值。
任何帮助都将非常感激。
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);
}
}
答案 0 :(得分:0)
您的方法应该有return value
,因为您已声明它返回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";
}
}
}
}
答案 1 :(得分:0)
你已宣布你的方法返回DataSet
......但你什么也没有返回。所以......您需要返回DataSet
的实例或更改您的方法签名。
答案 2 :(得分:0)
您已声明该方法应返回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);
}
}
}
}
答案 3 :(得分: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);
}
}
}
}