如何在 DAL 中将存储过程名称作为参数传递
How to pass stored procedure name as parameter in DAL
我有不止一个函数可以简单地从数据库中获取数据。函数之间的区别是存储过程名称(uspLoadStudents,uspLoadMarks)
。要优化,将其作为一个函数并通过 SP。
public DataSet LoadSubjects()
{
string SqlDBConnection = Utils.GetConnectionString();
DataSet ds = new DataSet();
SqlConnection sqlConn = new SqlConnection(SqlDBConnection);
SqlCommand sqlCmd = new SqlCommand("uspLoadSubjects", sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlConn.Open();
DataTable dt = new DataTable();
dt.Load(sqlCmd.ExecuteReader());
ds.Tables.Add(dt);
sqlConn.Close();
return ds;
}
这个?
public DataSet ExecProc(string procName)
{
string SqlDBConnection = Utils.GetConnectionString();
DataSet ds = new DataSet();
SqlConnection sqlConn = new SqlConnection(SqlDBConnection);
SqlCommand sqlCmd = new SqlCommand(procName, sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlConn.Open();
DataTable dt = new DataTable();
dt.Load(sqlCmd.ExecuteReader());
ds.Tables.Add(dt);
sqlConn.Close();
return ds;
}
试试这个
public static DataSet getDataSet(string sp_name, string[] param_names, object[] param_values)
{
SqlDataAdapter sqlda = new SqlDataAdapter();
SqlCommand sqlcmd = new SqlCommand();
DataSet set = new DataSet();
try
{
sqlcmd.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = sp_name;
sqlda.SelectCommand = sqlcmd;
sqlda.Fill(set);
}
catch (Exception ex)
{
}
finally
{
if (sqlcmd.Connection.State == ConnectionState.Open)
sqlcmd.Connection.Close();
}
return set;
}
像 sql 命令、存储过程名称这样的信息应该是您 Data Access Layer
的一部分,而不是数据访问层中的 helper class。试试这个:
public static class DALHelper
{
public static DataSet ExecuteProcedure(string procedureName)
{
string sqlDBConnection = Utils.GetConnectionString();
DataSet ds = new DataSet();
using (SqlConnection sqlConn = new SqlConnection(sqlDBConnection))
{
using(SqlCommand sqlCmd = new SqlCommand(procedureName, sqlConn))
{
sqlCmd.CommandType = CommandType.StoredProcedure;
try
{
sqlConn.Open();
using (var adapter = new SqlDataAdpter(sqlCmd))
{
adapter.Fill(ds);
}
}
catch
{
throw;
}
finally
{
sqlConn.Close();
}
}
}
return ds;
}
}
实现一个方法来使用这个助手,例如:
public DataSet LoadSubjects()
{
return DALHelper.ExecuteProcedure("uspLoadStudents");
}
我有不止一个函数可以简单地从数据库中获取数据。函数之间的区别是存储过程名称(uspLoadStudents,uspLoadMarks)
。要优化,将其作为一个函数并通过 SP。
public DataSet LoadSubjects()
{
string SqlDBConnection = Utils.GetConnectionString();
DataSet ds = new DataSet();
SqlConnection sqlConn = new SqlConnection(SqlDBConnection);
SqlCommand sqlCmd = new SqlCommand("uspLoadSubjects", sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlConn.Open();
DataTable dt = new DataTable();
dt.Load(sqlCmd.ExecuteReader());
ds.Tables.Add(dt);
sqlConn.Close();
return ds;
}
这个?
public DataSet ExecProc(string procName)
{
string SqlDBConnection = Utils.GetConnectionString();
DataSet ds = new DataSet();
SqlConnection sqlConn = new SqlConnection(SqlDBConnection);
SqlCommand sqlCmd = new SqlCommand(procName, sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlConn.Open();
DataTable dt = new DataTable();
dt.Load(sqlCmd.ExecuteReader());
ds.Tables.Add(dt);
sqlConn.Close();
return ds;
}
试试这个
public static DataSet getDataSet(string sp_name, string[] param_names, object[] param_values)
{
SqlDataAdapter sqlda = new SqlDataAdapter();
SqlCommand sqlcmd = new SqlCommand();
DataSet set = new DataSet();
try
{
sqlcmd.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = sp_name;
sqlda.SelectCommand = sqlcmd;
sqlda.Fill(set);
}
catch (Exception ex)
{
}
finally
{
if (sqlcmd.Connection.State == ConnectionState.Open)
sqlcmd.Connection.Close();
}
return set;
}
像 sql 命令、存储过程名称这样的信息应该是您 Data Access Layer
的一部分,而不是数据访问层中的 helper class。试试这个:
public static class DALHelper
{
public static DataSet ExecuteProcedure(string procedureName)
{
string sqlDBConnection = Utils.GetConnectionString();
DataSet ds = new DataSet();
using (SqlConnection sqlConn = new SqlConnection(sqlDBConnection))
{
using(SqlCommand sqlCmd = new SqlCommand(procedureName, sqlConn))
{
sqlCmd.CommandType = CommandType.StoredProcedure;
try
{
sqlConn.Open();
using (var adapter = new SqlDataAdpter(sqlCmd))
{
adapter.Fill(ds);
}
}
catch
{
throw;
}
finally
{
sqlConn.Close();
}
}
}
return ds;
}
}
实现一个方法来使用这个助手,例如:
public DataSet LoadSubjects()
{
return DALHelper.ExecuteProcedure("uspLoadStudents");
}