|
|||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionSuppose, you have an application with a SQL Server backend and have to deal with all the different SQL connection classes like The class encapsulates all these functions. COperationsSQL does the following:
How to use the classYou need to add the .cs file available in the download to your project. Here are the constructorspublic COperationsSQL(string Server, string Database,
string LoginName, string Password)
Specify a public COperationsSQL(string ConnectionStringCoded)
The connection string can also be passed as a single string. For Example: "server=MyServer;database=MyDatabase;uid=johndoe;pwd=1234"
Parsing will be done on this to extract the public COperationsSQL(COperationsSQL rhs)
Gets the How to instantiate the classHere is an example of how we can instantiate the class: COperationsSQL connection = new COperationsSQL("MyServer",
"MyDataBase", "JohnDoe", "1234"); //Simple Enough
Note: an error will be thrown back if it's unable to connect to the database. If it's able to connect, the uID and RoleInfo for the person logging in will be stored in a structure that is available for use. Also note that logging in as SA is not permitted (you can change that if you want). Once the class is instantiated, it is ready for use. MethodsFollowing is a description of some of the methods that are available in the class and their use:
ExamplesFirst way (this way uses regular variable, the variables that get passed into the stored procedure must be in the exact order specified by the SP): //This is where the data/table will be
//stored for the returned person
System.Data.DataSet myDataSet = new DataSet();
//the name of the stored procedure to run
string saveSp = "savePerson";
//the name of the stored procedure to run
string getSp = "getPerson";
string firstName = "John"; //parameters
string lastName = "Doe"; //parameters
System.DateTime DOB = "01/01/1980";
int currentAge = 25;
bool isAlive = true;
//This is where the person id will be stored
int personID;
try
{
//Instantiation of the class
CUtilities.COperationsSQL myconn =
new COperationsSQL("MyServer",
"MyDataBase", "RobertFrost", "1234");
//After the class has been instantiated,
//we will then save a person into the database
//NOTE: personID will also be set to the returned
//value for the stored procedure
//NOTE: the class willl automatically put ticks
// around whatever needs to have ticks
// and will change c# boolean to sql boolean.
// An error will be thrown if a SQL type
// has not been handled for.
//There are two ways you can add the parameters to
//the sp. Either as an ArrayList or an Array
//of objects.
object []param = new object[5];
param[0] = firstName;
param[1] = lastName;
param[2] = DOB;
param[3] = currentAge;
param[4] = isAlive;
//NOTE: when you use this method, the
//PARAMETERS MUST BE PASSED IN CORRECT ORDER
//We will now execute the storedprocedure
//and set the personID in one step.
personID = (int)myconn.ExcecuteStoredProcedure(saveSp,
param).Tables[0].Rows[0][0];
//We will now execute the getPerson stored
//procedure but with the ArrayList approach.
System.Collections.ArrayList parameters =
new System.Collections.ArrayList();
parameters.Add(personID);
//Lets say that the Stored procedure returns Columns
//with column names: ID, FirstName, LastName, DOB,
//CurrentAge,isAlive
myDataSet =
myconn.ExcecuteStoredProcedure(getSp, parameters);
MessageBox.Show("Hello! My name is " +
myDataSet.Tables[0].Rows[0]["FirstName"] +
" " +
myDataSet.Tables[0].Rows[0]["LastName"] +
". And my ID is : " +
myDataSet.Tables[0].Rows[0]["ID"]);
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
Second way (this way uses the structure available in the class so that the parameters can be passed in any order): //This is where the data/table will
//be stored for the returned person
System.Data.DataSet myDataSet = new DataSet();
//the name of the stored procedure to run
string saveSp = "savePerson";
//the name of the stored procedure to run
string getSp = "getPerson";
string firstName = "John"; //parameters
string lastName = "Doe"; //parameters
System.DateTime DOB = "01/01/1980";
int currentAge = 25;
bool isAlive = true;
//This is where the person id will be stored
int personID;
try
{
//Instantiation of the class
CUtilities.COperationsSQL myconn =
new COperationsSQL("MyServer",
"MyDataBase", "RobertFrost", "1234");
//After the class has been instantiated, we
//will then save a person into the database
//NOTE: personID will also be set to the
//returned value for the stored procedure
//NOTE: the class willl automatically put
// ticks around whatever needs to have ticks
// and will change c# boolean to sql boolean.
// An error will be thrown if a SQL type
// has not been handled for.
//passing in the parameters out of order using
//the sIdentifierItem Structure in the class
CUtilities.COperationsSQL.sIdentifierItem []param =
new CUtilities.COperationsSQL.sIdentifierItem[5];
param[0] =
new CUtilities.COperationsSQL.sIdentifierItem("@lastName",
lastName, "varchar");
param[1] =
new CUtilities.COperationsSQL.sIdentifierItem("@firstName",
firstName, "varchar");
param[2] =
new CUtilities.COperationsSQL.sIdentifierItem("@isAlive",
isAlive, "bit");
param[3] =
new CUtilities.COperationsSQL.sIdentifierItem("@dob",
DOB, "datetime");
param[4] =
new CUtilities.COperationsSQL.sIdentifierItem("@currentage",
currentAge, "int");
//We will now execute the storedprocedure
//and set the personID in one step.
personID = (int)myconn.ExcecuteStoredProcedure(saveSp,
param).Tables[0].Rows[0][0];
//We will now execute the getPerson stored
//procedure but with the ArrayList approach.
System.Collections.ArrayList parameters =
new System.Collections.ArrayList();
parameters.Add(personID);
//Lets say that the Stored procedure returns Columns
//with column names: ID, FirstName, LastName, DOB,
//CurrentAge,isAlive
myDataSet = myconn.ExcecuteStoredProcedure(getSp, parameters);
MessageBox.Show("Hello! My name is " +
myDataSet.Tables[0].Rows[0]["FirstName"] +
" " +
myDataSet.Tables[0].Rows[0]["LastName"] +
". And my ID is : " +
myDataSet.Tables[0].Rows[0]["ID"]);
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
This is basically what the class is intended for, an easy interface between the application and the SQL Server backend. The best way to use this class is to make it a part of your own objects (for example a person object) and then do the interactions required to get data from the database and save data to the database. The entire class is available at the top as a link. Add the .cs file to your project and enjoy.
|
||||||||||||||||||||||||||||||||||||||||||||