我会尽量简洁,请..请耐心等待,因为它应该非常简单......
目标:
试图通用化项目的特定部分,即处理
SQL databse transactions
.
旁注
为了帮助您回答,我粘贴了以下内容(仅供参考)
一个使用代码:GetTestOfTablTime()
返回一个DataTable
类:SQLDBInteraction
这是另一个类 - 负责最终(SQL 事务)阶段
在下面的这段代码中,我正在构建我所说的:“存储过程的元数据”
该类是包含所有 SQL Db SP 的类:
HTSPs
(HT是公司的别名)
这个类正在持有每个 SP
(必需的) parameter
s
HTSPs
class 包含另一个子 Class,对于所有SP
的 s 名称,它只有const string
s 的 For EachSP
名称
public sealed class HTSPs
{
//so for example this is one of the members of this class - a stored procedure
//its mission: get evnents with specified id OF specified userId in spec' month, year..
public sealed class GetTimesWithCustomerNames
{
//if I DO need Constructor for its parameters how do I properly format the constructor?
public GetTimesWithCustomerNames()
{
Userid.ParameterName = ParNameUserid;
Month.ParameterName = ParNameMonth;
Year.ParameterName = ParNameYear;
EventId.ParameterName = ParNameReasonid;
}
const string ParNameUserId = "@userId",
ParNameMonth = "@month",
ParNameYear = "@year",
ParNameEventId = "@eventId";
public static SqlParameter Userid = new SqlParameter();
public static SqlParameter Month = new SqlParameter();
public static SqlParameter Year = new SqlParameter();
public static SqlParameter EventId = new SqlParameter();
}
}
所以问题是: 我如何初始化构造函数?
让您的简单定制StoredProcedure
“元数据”的正确方法是什么
我目前已完成以下方法的实施(除了那个问题......)
用法
这是一个DataTable
在使用HTSPs
class
/时返回的方法constuctor
using SPTime = HT_DBSchema.HTSPs.GetTimesWithCustomerNames;
private DataTable GetTestOfTablTime()
{
SQLDBInteraction.DataContainer DC_Time = new SQLDBInteraction.DataContainer();
SQLDBInteraction.SqlParamList parmsTime = new SQLDBInteraction.SqlParamList();
Dictionary<SqlParameter, int> SqlCmdParDict = new Dictionary<SqlParameter, int>();
parmsTime.SqlCmd.CommandType = CommandType.StoredProcedure;
parmsTime.SqlCmd.CommandText = AppDb.MetaSqlSProc.Time.Name;
parmsTime.SP_Name = AppDb.MetaSqlSProc.Time.Name;
parmsTime.TableName = AppDb.MetaSqlTable.Time.Name;
//While folowing implementation Does Work I comented it out to try using the SP Struct
//ParmsTTime.SP_Params.Add(new SqlParameter(SPTime.ParNameMonth, 9));
//ParmsTTime.SP_Params.Add(new SqlParameter(SPTime.ParNameReasonid, 1));
//ParmsTTime.SP_Params.Add(new SqlParameter(SPTime.ParNameYear, 2012));
//ParmsTTime.SP_Params.Add(new SqlParameter(SPTime.ParNameUserid, 3571));
//here's where I'm currently stuck, in section below. trying to assign values for the SqlCommand
parmsTime.SqlCmd.Parameters.AddWithValue(SPTime.ParNameMonth, 9);
parmsTime.SqlCmd.Parameters.AddWithValue(SPTime.ParNameYear, 2012);
parmsTime.SqlCmd.Parameters.AddWithValue(SPTime.ParNameReasonid, 1);
SPTime.Userid.Direction = ParameterDirection.Input;
SPTime.Userid.SqlValue = 3571;
return DC_Time.LocalTbl_V3(ParmsTime);
}
更新
上面代码的最后几行试图实现参数分配,
因此不再需要使用:
SQLDBInteraction.SqlParamList.SP_Params
(即List<SqlParameter>
)
相反,我真的很想能够使用
SQLDBInteraction.SqlParamList.SqlCmd.Parameters
这样,因为它已经用于与数据库交互的大多数必需步骤,
所以这就是我将放弃一些不必要的额外变量使用的方法
同时我想分配 SqlCmd ( parmsTime.SqlCmd.Parameters.Add(......)
)
使用结构 -SPTime
真实 SqlParameters
...而不是像现在一样使用代表其名称的字符串
例如 parameter.name
- ( SPTime.ParNameMonth, someValue
)
最后阶段-sql事务
SQLDBInteraction
进行交易的类
public class SQLDBInteraction
{
public class SqlParamList
{
public SqlCommand SqlCmd = new SqlCommand();
public List<SqlParameter> SP_Params = new List<SqlParameter>();
public String SP_Name;
public string TableName;
public string SelectCommand;
///public SqlCommandType SelectedCmdType;
}
public class DataContainer
{
public DataTable LocalTbl_V3(SqlParamList Params)
{
SqlConnection sqlConnection;
DataTable Usrs = new DataTable(Params.TableName);
SqlDataAdapter sqlDataAdapter;
using (sqlConnection = new SqlConnection(WebConfigurationManager.ConnectionStrings["HTConn"].ConnectionString))
{
sqlConnection.Open();
using (Params.SqlCmd.Connection = sqlConnection)
{
using (sqlDataAdapter = new SqlDataAdapter(Params.SqlCmd.CommandText, sqlConnection))
{
if (sqlDataAdapter.SelectCommand.Parameters.Count > 0 == false)
{
sqlDataAdapter.SelectCommand = Params.SqlCmd;
sqlDataAdapter.Fill(Usrs);
}
}
}
}
return Usrs;
}
如果有人发现我在分配给SQL Command