gpt4 book ai didi

c# - 返回数据集值给出错误

转载 作者:行者123 更新时间:2023-11-30 13:16:58 27 4
gpt4 key购买 nike

我定义了以下函数,它将从表中返回 3 列。

public DataSet GetFunc()
{
int iRet = 0;
DataSet ds = new DataSet();
SqlConnection sqlConnection = new SqlConnection();
try
{
iRet = connect(ref sqlConnection);
if (DB_SUCCESS_CONNECT == iRet)
{
SqlCommand sqlCommand = new SqlCommand("", sqlConnection);
String strQuery = "Select ID, Did, FirstName from Users";
sqlCommand.CommandText = strQuery;

SqlDataAdapter adaptor = new SqlDataAdapter(sqlCommand);
adaptor.Fill(ds);
sqlConnection.Close();
return ds;
}

}
catch (Exception e)
{
disconnect(ref sqlConnection);
}
}

但是当我尝试构建它时,出现错误:

Error 172 'GetFunc()': not all code paths return a value

我对我哪里出错了感到困惑。有人可以指导我吗?

最佳答案

在 try block 中你已经给出了返回类型,而在 catch block 中没有返回类型。当编译器没有找到合适的返回值时,通常会发生此错误。尝试在 catch 中返回 ds

但是请确保在您的逻辑中进一步检查 ds 是否进行空检查

关于c# - 返回数据集值给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18224303/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com