【发布时间】:2015-08-31 21:58:45
【问题描述】:
如果返回值为 null 或为空,如何使以下代码返回零。另外我怎样才能只返回数据表上的第一条记录
private DataTable GetData(SqlCommand cmd)
{
gridoutofstock.DataBind();
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["AhlhaGowConnString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
【问题讨论】:
-
返回
0?你的函数被定义为返回一个DataTable- 你不能返回0。您可能应该添加更多详细信息或澄清您的问题。 -
这是我的 sql 命令,它只返回一个值 select sum(Quantity) from [dbo].Orderdetails 那么如何使用 insted 的数据表?
-
请用详细信息编辑您的问题,不要将它们添加为 cmets - 这些可能会被读者忽略。
-
听起来您在问“为什么我在需要
int时使用DataTable作为返回类型” - 这有点难以解释...也许您正在寻找 @987654321 @ ?
标签: c# sqldataadapter