I will try to be as Concise as I can be, please.. bear with me, as it should be very simple …
-
the goal:
trying to universalize a specific section of a project, that is dealing with the
SQL databse transactions.
a side note
to assist you with your answer, I’ve pasted the folowing (just for Reference)
a Usage-Code : GetTestOfTablTime() returns a DataTable
class : SQLDBInteraction this is another Class – responsible for the final(SQL transaction) stage
in this code below I am constructing what I Call: “Stored Procedure’s Meta Data”
that class is the one that holds all of the SQL Db SPs :
HTSPs (HT is the company’s aliases)
this class is holding each SP (requierd) parameters
HTSPs class contains another sub Class, for all SPs Names, it only has const strings For Each SP name
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();
}
}
so the issue is: how do I initialize the constractor ?
what is the Proper way to have your simple customised StoredProcedure “MetaData“
Iv’e currently Completed the implementation of the Method below (apart from that issue…)
USAGE
this is a method that returns DataTable while using the 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);
}
UPDATE
the last lines of the code above is trying to implement the parmeters assignment ,
thus it will no longer be required to use :
SQLDBInteraction.SqlParamList.SP_Params (which is List<SqlParameter>)
and instead i would really like to be able to use
SQLDBInteraction.SqlParamList.SqlCmd.Parameters
that way as it is already used for most of the requierd steps to interact with the Database,
so this is how i will drop some unnecessery usage of extra variables
while in same time i wanted to assign SqlCmd ( parmsTime.SqlCmd.Parameters.Add(......))
with the Struct – SPTime Real SqlParameters
… instead of using the strings that reperesnts their name as it is now
E.g parameter.name – (SPTime.ParNameMonth, someValue)
final stage- sql trasaction
the SQLDBInteraction Class that does the transaction
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;
}
I will really appreciate it if someone will find what am I doing wrong with the part of the stored procedure’s Parameters Assigned to the SQL Command
so at last … this is my own answer to the ‘problem’.
as there will always be opportunities to fine-tune …and polish it even more,
though I did find, a way to implement the solution .
for starters :
Summary
connecting between a 3 stages proccess – the data extraction, as follows:
1’st stage : assigning the name and id’s for the table and its stored procedure
within the struct
2’nd stage : constructing the sql parameter collection /
List <SqlParameter>for the final sql command .
the 3’rd stage : by using these
structs to format / construct the sql command,will in return, get a
System.DataDataTableas requested, via the sql instructionsin the stored procedure… naturally.
prepering a background – for code ReUse
these first 2 blocks of code are sotred in two separated files
the first file is for holding the sql databse schema
table names , tables (custom) IDs , Columns names , and stored procedures names and parameters.
the Second file is a general usage Helper
nameapace, that stores all Helper classes,and one of the classes (next block of code) , is the one that hodls the
structsone for the table and another for the stored procedure,
these two, will be assinged, later in the application’s
code behind.so both of the codes above are in two separated files …
meaning codes above could be used in every application i need to create,
so it’ll be very easy to implement after this initial background work done.
implementation in current project.
the next codes are used in the "current project" code behind
aspx.csthe implementation of struct for the
System.DataDataTableand its
stored procedure, that will be used / assigned to theSqlCommandthe final two steps that could have been in one , though what i want to see is
that the project would have the smallest footprint of extra codes needed to get the data
so i guess the second part that is doing the actual interaction with database
should be soon after tests are complete moved to the Helpers section and out of the
code behindsection of every application .so next are codes that will gather all preperations and data setup into action
the final stage: sql database interaction
this is only a test, yet it works very fast
and very easy to implemet , it will fit perfectly as it is for personal or small buisness use
comments are very welcome …
thanks