Common class libraries for SQL Server data operations
Last Update:2017-02-28
Source: Internet
Author: User
Server|sqlserver| data based on their usual experience common class libraries for SQL Server operations
Using System;
Using System.Data;
Using System.Data.SqlClient;
Using System.Web;
Namespace WHGW_LC
{
<summary>
Summary description of Data.
</summary>
<summary>
Database related Operations class
Author: Darkangel
Time: 2004-9-24
</summary>
public class Data
{
<summary>
Connecting Database strings
</summary>
Protected static string con= "Data source=10.68.19.203;initial catalog=whgw;persist security Info=false;user Id=sa; Password=sc2000;workstation id=darkangel;packet size=4096 ";
<summary>
Database Connection objects
</summary>
Protected System.Data. Sqlclient.sqlconnection DataConnection;
<summary>
SqlDataAdapter objects
</summary>
protected System.Data.SqlClient.SqlDataAdapter DataAdapter;
<summary>
SqlCommand objects
</summary>
protected System.Data.SqlClient.SqlCommand DataCommand;
<summary>
SqlCommandBuilder objects
</summary>
protected System.Data.SqlClient.SqlCommandBuilder Datacombuilder;
<summary>
DataSet object
</summary>
protected System.Data.DataSet DataSet;
<summary>
DataRow objects
</summary>
protected System.Data.DataRow DataRow;
<summary>
SqlDataReader objects
</summary>
protected System.Data.SqlClient.SqlDataReader DataReader;
protected System.Web.UI.Page mypage;
<summary>
Constructors
</summary>
Public Data ()
{
//
TODO: Connecting Database parameters
//
con=
Dataconnection=dataconn ();
}
<summary>
Database Connection Parameter Properties
</summary>
public string Con
{
Get{return con;
set{con= value;}
}
<summary>
Establishing a database connection
</summary>
<returns> Return database connection objects </returns>
Public SqlConnection DataConn ()
{
SqlConnection dataconnection=new SqlConnection (Con);
return dataconnection;
}
<summary>
Query results, returning
</summary>
<param name= "MySQL" > Query statements </param>
<returns> return to Datareader</returns>
Public SqlDataReader dataquery (string Sql)
{
Dataconnection=dataconn ();
Dataconnection.open ();
Datareader=null;
Datacommand=new SqlCommand (sql,dataconnection);
Datareader=datacommand.executereader ();
return DataReader;
}
<summary>
Query results, returning dataset
</summary>
<param name= "MySQL" > Query statements </param>
<param name= "tabname" > table name </param>
<returns>DataSet</returns>
Public virtual DataSet datasetquery (String sql,string tablename)
{
Dataconnection=dataconn ();
Dataconnection.open ();
Dataadapter=new SqlDataAdapter (Sql,dataconnection);
Dataset=new DataSet ();
DataAdapter.Fill (Dataset,tablename);
return DataSet;
}
Public DataSet Datasetquery (DataSet myds,string tablename,string Sql)
{
Dataconnection=dataconn ();
Dataconnection.open ();
Dataadapter=new SqlDataAdapter (Sql,dataconnection);
DataAdapter.Fill (Myds,tablename);
return DataSet;
}
<summary>
Query result, return dataset, with start and end
</summary>
<param name= "MySQL" > Query statements </param>
<param name= "tabname" > table name </param>
<param name= "Starindex" > Start line </param>
<param name= "Count" > Stop line </param>
<returns> return to Dataset</returns>
Public DataSet datasetquery (string sql,string tablename,int starindex,int Count)
{
Dataconnection=dataconn ();
Dataconnection.open ();
Dataadapter=new SqlDataAdapter (Sql,dataconnection);
Dataset=new DataSet ();
DataAdapter.Fill (Dataset,starindex,count,tablename);
return DataSet;
}
<summary>
Modify Database Contents
</summary>
<param name= "MySQL" > Query statements </param>
<returns> If execution returns true, no person returns false </returns>
public bool EditData (string Sql)
{
Dataconnection=dataconn ();
Dataconnection.open ();
Datacommand=new SqlCommand (sql,dataconnection);
Try
{
Datacommand.executenonquery ();
return true;
}
catch (Exception ex)
{
System.Console.Write (ex. Message.tostring ());
return false;
}
Dataconnection.close ();
}
<summary>
Modify the contents of the dataset and update the database
</summary>
<param name= "DataSet" >dataset object </param>
<param name= "Myda" >sqldataadapter container </param>
<param name= "tbname" > table name </param>
<returns> return to Dataset</returns>
Public DataSet EditData (DataSet Dataset,sqldataadapter myda,string tablename)
{
Datacombuilder=new SqlCommandBuilder (Myda);
Myda. Updatecommand=datacombuilder.getupdatecommand ();
Myda. Deletecommand=datacombuilder.getdeletecommand ();
Myda. Insertcommand=datacombuilder.getinsertcommand ();
Myda. Update (Dataset,tablename);
Dataset.acceptchanges ();
return DataSet;
}
<summary>
Execution of parameter-free stored procedures
</summary>
<param name= "proname" > Stored procedure name </param>
<returns> whether to execute success </returns>
public bool Dataprocedure (string procedurename)
{
Dataconnection=dataconn ();
Dataconnection.open ();
Datacommand=new SqlCommand ();
Datacommand.commandtext=procedurename;
Datacommand.connection=dataconnection;
Datacommand.commandtype=system.data.commandtype.storedprocedure;
Try
{
Datacommand.executenonquery ();
return true;
}
catch (Exception ex)
{
System.Console.Write (ex. Message.tostring ());
return false;
}
}
<summary>
To close a database connection
</summary>
public void CloseConnection ()
{
Dataconnection.close ();
}
}
}