My Linked database classes

Source: Internet
Author: User
Tags table name
Data | database using System;
Using System.Data;
Using System.Data.SqlClient;
Using System.Configuration;

Namespace Admin.commonclass
{
<summary>
The class--opendb that links the database
</summary>
public class Opendb
{
To get a string of linked databases
private string Strjet = configurationsettings.appsettings["strconn"];

Public Opendb ()
{
//
TODO: Add constructor logic here
//
}

#region Get DataSet
<summary>
Get a DataSet, format: GetDataSet (query field, query table, condition field, conditional symbol, condition value)
</summary>
<param name= "Strzdname" ></param>
<param name= "strTableName" ></param>
<param name= "Strwherevalue" ></param>
<returns></returns>

Public DataSet GetDataSet (string strzdname,string strtablename,string strwherevalue)
{

To create an object that links a database
SqlConnection myconn = new SqlConnection (Strjet);

Creating adapter Objects
SqlDataAdapter myadapter = new SqlDataAdapter ("Sp_getdataset", myconn);

Indicate as stored procedure
MyAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;

Assigns a value to a stored procedure's STRZD variable, which indicates the query field.
MYADAPTER.SELECTCOMMAND.PARAMETERS.ADD ("@ZDName", SqlDbType.Char). Value = Strzdname;

Assigns a value to a stored procedure's Fromwhere variable, which indicates the query table.
MYADAPTER.SELECTCOMMAND.PARAMETERS.ADD ("@TableName", SqlDbType.Char). Value = strTableName;

Assigns a value to a stored procedure's Wherevalue variable, which indicates the query condition.
MYADAPTER.SELECTCOMMAND.PARAMETERS.ADD ("@WhereValue", SqlDbType.Char). Value = Strwherevalue;


Creating a DataSet Object
DataSet ds = new DataSet ();

Populating data
Myadapter.fill (ds, "Strfromwhere");

Returns a DataSet
return DS;
}
#endregion

Inserting Data #region
<summary>
Insert data, Format: insertdata (table name, field name, field value)
</summary>
<param name= "tablename" ></param>
<param name= "Zdname" ></param>
<param name= "Zdvalue" ></param>
public void InsertData (string strtablename,string strzdname,string strzdvalue)
{
To create an object that links a database
SqlConnection myconn = new SqlConnection (Strjet);

Open myconn
MyConn. Open ();

Creating SqlCommand objects
SqlCommand mycmd = new SqlCommand ("InsertData", myconn);

Indicates that the stored procedure
Mycmd.commandtype = CommandType.StoredProcedure;

Add stored procedure Parameters
myCMD. Parameters.Add ("@TableName", SqlDbType.Char);
myCMD. Parameters.Add ("@ZDName", SqlDbType.Char);
myCMD. Parameters.Add ("@ZDValue", SqlDbType.Char);

Assigning values to stored procedure variables
myCMD. parameters["@TableName"]. Value = strTableName;
myCMD. parameters["@ZDName"]. Value = Strzdname;
myCMD. parameters["@ZDValue"]. Value = Strzdvalue;

To perform a command operation
myCMD. ExecuteNonQuery ();
}
#endregion

#region Modify Data
<summary>
Modify data, Format: UpdateData (table name, field assignment, conditional statement)
</summary>
<param name= "strTableName" ></param>
<param name= "Strzdname" ></param>
<param name= "Strzdvalue" ></param>
public void UpdateData (string strtablename,string strzdname,string strwherevalue)
{
To create an object that links a database
SqlConnection myconn = new SqlConnection (Strjet);

Open myconn
MyConn. Open ();

Creating SqlCommand objects
SqlCommand mycmd = new SqlCommand ("UpdateData", myconn);

Indicates that the stored procedure
Mycmd.commandtype = CommandType.StoredProcedure;

Add stored procedure Parameters
myCMD. Parameters.Add ("@TableName", SqlDbType.Char);
myCMD. Parameters.Add ("@ZDName", SqlDbType.Char);
myCMD. Parameters.Add ("@WhereValue", SqlDbType.Char);

Assigning values to stored procedure variables
myCMD. parameters["@TableName"]. Value = strTableName;
myCMD. parameters["@ZDName"]. Value = Strzdname;
myCMD. parameters["@WhereValue"]. Value = Strwherevalue;

To perform a command operation
myCMD. ExecuteNonQuery ();
}
#endregion

#region Delete Data
<summary>
Delete data, Format: deletedata (table name, delete condition)
</summary>
<param name= "strTableName" &



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.