double Get_Page_Nummber(string tableName)
{
//获取搜索结果总共有多少页
int rows;
string connectionString = PubConstant.ConnectionString; //获取连接字符串
string filter_strsql = Creat_filter_strsql();
string SQLString = "SELECT count(*) From " + tableName + " " + filter_strsql;
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
rows = (int)cmd.ExecuteScalar();
}
catch (System.Data.SqlClient.SqlException e)
//catch
{
connection.Close();
throw e;
}
return Math.Ceiling(Convert.ToDouble(rows / int.Parse(DropDownList1.SelectedValue)));
}
}
//Response.Write(strsql + " " + roms_number);
}
double Get_Page_Nummber(string tableName)
{
//获取搜索结果总共有多少页
int rows;
string connectionString = PubConstant.ConnectionString; //获取连接字符串
string filter_strsql = Creat_filter_strsql();
string SQLString = "SELECT count(*) From " + tableName + " " + filter_strsql;
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
rows = (int)cmd.ExecuteScalar();
}
//catch (System.Data.SqlClient.SqlException e)
catch
{
connection.Close();
//throw e;
}
return Math.Ceiling(Convert.ToDouble(rows / int.Parse(DropDownList1.SelectedValue)));
}
}
//Response.Write(strsql + " " + roms_number);
}
上面的写法是正确的,下面的写法就会提示你使用了未赋值的变量rows。同样是引发异常,一个往外抛出,一个没抛出,从结果来说rows都没被赋值,为啥写法一就是正确的?