ArticleDirectory
- Execute SQL statements
- Tableframework implements simple data insertion and update
This article mainly introduces the features and usage of the new version of JSM sqlhelper2.0!
New Features of JSM sqlhelper2.0
Inherit the static method mode of the original sqlhelper and optimize it.
Enhance web. config configuration support to facilitate routine website maintenance.
Added the facial object class. You can use the sqlhelper object to easily implement complexProgramLogic.
Added support for access, Oracle, and MySQL databases.
Added the tableframework class to implement simple insert and update statements, automatically generate parameters and SQL statements, and reduceCodeQuantity.
JSM sqlhelper configuration method
Open the Web. config file and configure the Add item in configurationsettings under the configuration node. The name indicates the default read link address of the program.
Read from web. config. You can overwrite and use the method to pass in the Link string. The Web. config configuration instance code is as follows:
<? XML version = "1.0" ?>
< Configuration >
< Connectionstrings >
< Add Connectionstring = "Server =.; uid = sa; Pwd = ***; database = dbname" Name = "Sqlserverhelper" />
< Add Connectionstring = "Data Source = orcl; user id = system; Password = ***; Integrated Security = No" Name = "Oraclehelper" />
< Add Connectionstring = "Server = localhost; uid = root; Pwd = ***; database = mysql_dbname" Name = "Mysqlhelper" />
</ Connectionstrings >
< System. Web >
< Compilation Debug = "True" />
</ System. Web >
</ Configuration >
Static Implementation of JSM sqlhelper
/// <Summary>
/// Read Student Information
/// </Summary>
Datatable readstudent ( Long Stid)
{
Return Sqlserverhelper. readtable (" Select * from [Students] Where stid = @ stid " ,
Sqlserverhelper. createinputparameter ( " @ Stid " , Sqldbtype. bigint, stid ));
}
/// <Summary>
/// Delete Student Information
/// </Summary>
Int Deletestudent ( Long Stid)
{
Return Sqlserverhelper. executenonquery ( " Delete from [Students] Where stid = @ stid " ,
Sqlserverhelper. createinputparameter ( " @ Stid " , Sqldbtype. bigint, stid ));
}
JSM sqlhelper face image object Implementation Method
In complex database operations, the face object method can execute multiple SQL code at a time by connecting to the database, thus improving the program execution efficiency.
Execute SQL statements
/// <Summary>
/// Read Student Information
/// </Summary>
Datatable readstudent ( Long Stid)
{
Using (Sqlserverhelper helper =New Sqlserverhelper ())
{
Helper. Command. commandtext = " Select * from [Students] Where stid = @ stid " ;
Helper. addparameter ( " @ Stid " , Sqldbtype. bigint, stid );
Helper. open ();
Return Helper. readtable ();
}
}
/// <Summary>
/// Delete student information to implement transactions and execute the delete SQL statement
/// </Summary>
Int Deletestudent ( Long Stid)
{
Using (Sqlserverhelper helper = New Sqlserverhelper ())
{
Helper. open ();
// Start transaction
Sqltransaction TRAN = helper. Connection. begintransaction ();
Helper. Command. Transaction = Tran;
Try
{
Helper. Command. commandtext = " Delete from [Students] Where stid = @ stid " ;
Helper. addparameter ( " @ Stid " , Sqldbtype. bigint, stid );
Int R = helper. executenonequery ();
Tran. Commit ();
Return R;
}
Catch {
Tran. rollback ();
Throw ;
}
}
}
Tableframework implements simple data insertion and update
Using tableframework, you can use tameframework to add column and data value information. Using inserttable or updatetable, you can easily generate SQL statements and parameters, which are accurate and simple and easy to maintain in the future, add only one column value. The sample code is as follows:
/// <Summary>
/// Add Student Information
/// </Summary>
Bool Insertstudent ( String Studentname, String Classname)
{
Tableframework TF = New Tableframework ( " Students " );
TF. Add ( " Student_name " , Studentname );
TF. Add ( " Class " , Classname );
Using (Sqlserverhelper helper =New Sqlserverhelper ())
{
Helper. open ();
// Insert a new record
Return Helper. inserttable (TF );
}
}
/// <Summary>
/// Update Student Information
/// </Summary>
Bool Updatestudent ( Long Stid, String Studentname, String Classname)
{
Tableframework TF = New Tableframework ( " Students " );
TF. Add ( " Student_name " , Studentname );
TF. Add ( " Class " , Classname );
Using (Sqlserverhelper helper = New Sqlserverhelper ())
{
Helper. addparameter ( " @ Stid " , Sqldbtype. bigint, stid );
Helper. open ();
Return Helper. updatetable (TF, " Where stid = @ stid " , False );
}
}
Download JSM sqlhelper 2.0 source code