自定义了一个方法,始终报错,提示 ————并非所有的代码路径都返回值??分析了很久,也没有找到症结,求高手指点??
//自定义DtReslut()方法从某个位置取出一条信息放到DataTable中
private DataSet DtReslut(int i)
{
//连接Sql server 数据库
SqlConnection con = new SqlConnection("Data Source=PC2012030219NBS;Initial Catalog=Stud;Integrated Security=True");
//打开数据库
con.Open();
//得到记录数(Count的值)
try
{
SqlCommand command = new SqlCommand("select count(*) from T_classInfo", con);
Count = (int)command.ExecuteNonQuery();
//获取信息
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select * from T_classInfo", con);
DataSet ds = new DataSet();
//da.Fill(ds, i, i + 1, "T_classInfo");
da.Fill(ds);
//return ds; //返回Dataset对象
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();//关闭数据库连接
}
}
//自定义DtReslut()方法从某个位置取出一条信息放到DataTable中
private DataSet DtReslut(int i)
{
//连接Sql server 数据库
SqlConnection con = new SqlConnection("Data Source=PC2012030219NBS;Initial Catalog=Stud;Integrated Security=True");
//打开数据库
con.Open();
//得到记录数(Count的值)
try
{
SqlCommand command = new SqlCommand("select count(*) from T_classInfo", con);
Count = (int)command.ExecuteNonQuery();
//获取信息
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select * from T_classInfo", con);
DataSet ds = new DataSet();
//da.Fill(ds, i, i + 1, "T_classInfo");
da.Fill(ds);
//return ds; //返回Dataset对象
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();//关闭数据库连接
}
}
不好意思啊,原文中return ds;没有注释
DataSet ds = new DataSet();
//连接Sql server 数据库
SqlConnection con = new SqlConnection("Data Source=PC2012030219NBS;Initial Catalog=Stud;Integrated Security=True");
//打开数据库
con.Open();
//得到记录数(Count的值)
try
{
SqlCommand command = new SqlCommand("select count(*) from T_classInfo", con);
Count = (int)command.ExecuteNonQuery();
//获取信息
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select * from T_classInfo", con); //da.Fill(ds, i, i + 1, "T_classInfo");
da.Fill(ds);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();//关闭数据库连接
}
}return ds; //返回Dataset对象
{
MessageBox.Show(ex.Message);
return null;
}
代码错误直接捕捉异常,到Catch里了,
所以你的代码,有可能没有return 值。
像“孟子”写的就行了。
不过这样的ADO操作一般写在数据层,所以Catch里直接Throw就行了,“MessageBox.Show”在画面层调用的时候再弹出。
{
MessageBox.Show(ex.Message);return null;
}
保证每个地方都有return就好了~~
VS都提示了其实还是挺好找的
try
{}
catct
{
return null;
}