protected static DataSet 解释一下代码。。

来源:百度知道 编辑:UC知道 时间:2024/05/14 15:07:15
protected static DataSet ST_ExecuteSql4Ds(string ST_strSQL)
{
SqlConnection ST_myCn = new SqlConnection(ST_strConn);
try
{
ST_myCn.Open();
SqlDataAdapter sda = new SqlDataAdapter(ST_strSQL,ST_myCn);
DataSet ds = new DataSet("ds");
sda.Fill(ds);
return ds;
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
ST_myCn.Close();
}
}

//整个方法就是返回从数据库里查询的一个数据集
protected static DataSet ST_ExecuteSql4Ds(string ST_strSQL) //这个是方法名就不多说了
{
SqlConnection ST_myCn = new SqlConnection(ST_strConn); //声明一个数据库连接对象
try //异常处理
{
ST_myCn.Open(); //打开数据连接对象
SqlDataAdapter sda = new SqlDataAdapter(ST_strSQL,ST_myCn); //声明一个适配器
DataSet ds = new DataSet("ds"); //声明一个容器用于放查询得到的数据集
sda.Fill(ds); //把数据填充到ds容器中
return ds; //返回数据集合
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message); //抛出异常信息
}
finally
{
ST_myCn.Close(); //关闭数据连接 如果用DataSet的话可以不关
}
}

整段代码楼上说的很明白了
另外说下protected static DataSet ST_ExecuteSql4Ds(string ST_strSQL)

这行是声明了一个名字为ST_ExecuteSql4Ds的函数,这个函数接收一个string类型的参数

protected说明这个方法是受保护的 只能在该类内部或者子孙类中调用
static 说明这是一个静态方法,不需要实例化即可通过 类名.方法名 进行调用