error: not all code path returns a value,
错误:并非所有代码路径都返回一个值,
i'm trying to return a dataset from function but getting this error,
我试图从函数返回一个数据集,但收到此错误,
code:
public partial class crystalReport_manual : System.Web.UI.Page
{
String conStr = WebConfigurationManager.ConnectionStrings["LoginDatabaseConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
DataSet1 ds = Dataset_load("Select * from login");
ReportDocument rd = new ReportDocument();
rd.Load(Server.MapPath("CrystalReport.rpt"));
rd.SetDataSource(ds);
CrystalReportViewer1.ReportSource = rd;
}
public DataSet1 Dataset_load(String query)
{
SqlConnection sqlcon = new SqlConnection(conStr);
SqlCommand sqlCom = new SqlCommand("select * from Login", sqlcon);
SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCom);
// DataSet ds = new DataSet("CRDataSet");
try
{
sqlcon.Open();
//sqlCom.ExecuteNonQuery();
//sqlDA.Fill(ds,"Login");
DataSet1 ds = new DataSet1();
DataTable dt = new DataTable("DT_CR");
sqlDA.Fill(dt);
ds.Tables[0].Merge(dt);
return ds;
}
catch (Exception exc)
{
Response.Write(exc.Message);
}
finally
{
sqlcon.Close();
}
}
2 个解决方案
#1
1
public DataSet1 Dataset_load(String query)
{
DataSet1 ds = new DataSet1();
using(SqlConnection sqlcon = new SqlConnection(conStr))
using(SqlCommand sqlCom = new SqlCommand("select * from Login", sqlcon))
using (SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCom))
{
try
{
//sqlCom.ExecuteNonQuery();
//sqlDA.Fill(ds,"Login");
DataTable dt = new DataTable("DT_CR");
sqlDA.Fill(dt);
ds.Tables[0].Merge(dt);
//return ds;
}
catch (SqlException se)
{
Response.Write(se.Message);
//return null;
}
catch (Exception exc)
{
Response.Write(exc.Message);
//return null;
}
}
return ds;
}
Building off the previous user's response, if an empty DataSet is returned at the end of your using statement, you can always check ds.Tables.Count
to see if anything was returned. Otherwise you can use the logic above and null
check
建立前一个用户的响应,如果在using语句结束时返回一个空的DataSet,则可以随时检查ds.Tables.Count以查看是否返回了任何内容。否则,您可以使用上面的逻辑和null检查
#2
4
What if an exception occur, your catch
or finally
block should return some value.
如果发生异常,您的catch或finally块应该返回一些值。
Since you are only closing the connection in finally
you can use using
block like:
由于您最终只是关闭了连接,因此可以使用如下块:
public DataSet1 Dataset_load(String query)
{
DataSet1 ds = new DataSet1();
using(SqlConnection sqlcon = new SqlConnection(conStr))
using(SqlCommand sqlCom = new SqlCommand("select * from Login", sqlcon))
using (SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCom))
{
try
{
//sqlCom.ExecuteNonQuery();
//sqlDA.Fill(ds,"Login");
DataTable dt = new DataTable("DT_CR");
sqlDA.Fill(dt);
ds.Tables[0].Merge(dt);
return ds;
}
catch (SqlException se)
{
Response.Write(se.Message);
return null;
}
catch (Exception exc)
{
Response.Write(exc.Message);
return null;
}
}
}
using
internally translates to try-finally
block, and works with those which implements IDisposable
, in finally
block it calls the Dispose
method, since SqlConnection
, SqlCommand
, and SqlDataAdapter
, all implements IDisposable
with using
statement it will call Dispose
at the end which would close the connection.
使用内部转换为try-finally块,并与实现IDisposable的那些一起工作,在finally块中它调用Dispose方法,因为SqlConnection,SqlCommand和SqlDataAdapter都使用using语句实现IDisposable,它将在结束时调用Dispose连接。
#1
1
public DataSet1 Dataset_load(String query)
{
DataSet1 ds = new DataSet1();
using(SqlConnection sqlcon = new SqlConnection(conStr))
using(SqlCommand sqlCom = new SqlCommand("select * from Login", sqlcon))
using (SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCom))
{
try
{
//sqlCom.ExecuteNonQuery();
//sqlDA.Fill(ds,"Login");
DataTable dt = new DataTable("DT_CR");
sqlDA.Fill(dt);
ds.Tables[0].Merge(dt);
//return ds;
}
catch (SqlException se)
{
Response.Write(se.Message);
//return null;
}
catch (Exception exc)
{
Response.Write(exc.Message);
//return null;
}
}
return ds;
}
Building off the previous user's response, if an empty DataSet is returned at the end of your using statement, you can always check ds.Tables.Count
to see if anything was returned. Otherwise you can use the logic above and null
check
建立前一个用户的响应,如果在using语句结束时返回一个空的DataSet,则可以随时检查ds.Tables.Count以查看是否返回了任何内容。否则,您可以使用上面的逻辑和null检查
#2
4
What if an exception occur, your catch
or finally
block should return some value.
如果发生异常,您的catch或finally块应该返回一些值。
Since you are only closing the connection in finally
you can use using
block like:
由于您最终只是关闭了连接,因此可以使用如下块:
public DataSet1 Dataset_load(String query)
{
DataSet1 ds = new DataSet1();
using(SqlConnection sqlcon = new SqlConnection(conStr))
using(SqlCommand sqlCom = new SqlCommand("select * from Login", sqlcon))
using (SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCom))
{
try
{
//sqlCom.ExecuteNonQuery();
//sqlDA.Fill(ds,"Login");
DataTable dt = new DataTable("DT_CR");
sqlDA.Fill(dt);
ds.Tables[0].Merge(dt);
return ds;
}
catch (SqlException se)
{
Response.Write(se.Message);
return null;
}
catch (Exception exc)
{
Response.Write(exc.Message);
return null;
}
}
}
using
internally translates to try-finally
block, and works with those which implements IDisposable
, in finally
block it calls the Dispose
method, since SqlConnection
, SqlCommand
, and SqlDataAdapter
, all implements IDisposable
with using
statement it will call Dispose
at the end which would close the connection.
使用内部转换为try-finally块,并与实现IDisposable的那些一起工作,在finally块中它调用Dispose方法,因为SqlConnection,SqlCommand和SqlDataAdapter都使用using语句实现IDisposable,它将在结束时调用Dispose连接。