找 C#高手解决问题 在线等

来源:百度知道 编辑:UC知道 时间:2024/05/11 04:07:38
public DataSet ExecuteQuery(string sqlText, CommandType commandtype, string tableName,SqlParameter[] parm)
{
SqlConnection conn = new SqlConnection(
ConfigurationManager.ConnectionStrings["NorthwindConnStr"].ConnectionString);

SqlDataAdapter da = new SqlDataAdapter(sqlText, conn);
da.SelectCommand.CommandType = commandtype;
if (parm != null)
{
foreach (SqlParameter p in parm)
{
da.SelectCommand.Parameters.Add(p);
}
}
DataSet ds = new DataSet();
try
{
da.Fill(ds, tableName);
}
catch (SqlException sqlEx)
{
throw sqlEx;
}
catch (Exception ex)
{

public DataSet ExecuteQuery(string sqlText, CommandType commandtype, string tableName,SqlParameter[] parm)
{
SqlConnection conn = new SqlConnection(
ConfigurationManager.ConnectionStrings["NorthwindConnStr"].ConnectionString); //设置连接字符串,NorthwindConnStr是已定义字符串

SqlDataAdapter da = new SqlDataAdapter(sqlText, conn); //定义数据适配器
da.SelectCommand.CommandType = commandtype;
if (parm != null) //判断之前定义的变量数组是否为空
{
foreach (SqlParameter p in parm) //foreach循环,对于PARM数组中的每个变量P
{
da.SelectCommand.Parameters.Add(p); //都把它添加为SQL命令的外部变量
}
}
DataSet ds = new DataSet(); //定义dataset
try
{
da.Fill(ds, tableName); //填充dataset
}
catch (SqlException sqlEx) //捕获数据库异常
{
throw sqlEx; //抛出数据库异常
}
catch (Exception ex) //捕获程序异常
{
throw ex; //抛出程序异常
}
return ds; //返回dataset的值

}