Using datasets to implement operations on the database
Last Update:2017-02-28
Source: Internet
Author: User
Data | database///<summary>
Note: This class mainly implements the operation of the database (query | SP)
Established by: Huang Zongban
Establishment Time: 2004-12-4
</summary>
public class DB
{
<summary>
Querying data from a database
</summary>
<param name= "Columns" > Query column name </param>
<param name= "target" > Query target </param>
<param name= "Pkcolumn" > Primary key Column name </param>
<param name= "Pkvalue" > Primary key value </param>
<param name= "Q" > whether to add quotes </param>
<param name= "Strex" > Exception information </param>
<returns> return Query Results </returns>
public static DataView Getdbdata (String Columns, String Target, String pkcolumn, String pkvalue, bool Q, ref string Strex )
{
String strcondition = "[" + Pkcolumn + "] =";
if (Q)
{
Strcondition + = "'" + Pkvalue + "'";
}
Else
{
Strcondition + = Pkvalue;
}
DataSet ds = Getdbdata (Columns, Target, strcondition, ref Strex);
if (Strex!= string. Empty)
{
return null;
}
Return DS. Tables[0]. DefaultView;
}
<summary>
Querying data from a database
</summary>
<param name= "Columns" > Query column name </param>
<param name= "target" > Query target </param>
<param name= "Condition" > Query conditions </param>
<param name= "Strex" > Exception information </param>
<returns> Query Results </returns>
public static DataSet Getdbdata (String Columns, String Target, String Condition, ref string Strex)
{
Strex = string. Empty;
String strconnect = configurationsettings.appsettings["ConnectString"];
SqlConnection SqlConnection = new SqlConnection (StrConnect);
Try
{
String strquery = "Select" + Columns + "from [" + Target + "]";
if (Condition.trim ()!= string. Empty)
{
Strquery + = "WHERE [" + Condition;
}
SqlDataAdapter SqlDataAdapter = New SqlDataAdapter (Strquery, SqlConnection);
DataSet ds = new DataSet ();
Sqldataadapter.fill (DS);
Sqlconnection.close ();
return DS;
}
catch (SqlException ex)
{
Sqlconnection.close ();
Strex = ex. message;
return null;
}
}
<summary>
Execute a stored procedure
</summary>
<param name= "SP" > Stored procedure name </param>
<param name= "xsd" > DataSet object </param>
<param name= "Strex" > Exception information </param>
<returns> Implementation Results </returns>
public static void ExecSP (string SP, DataSet xsd, ref string Strex)
{
String strconnect = configurationsettings.appsettings["ConnectString"];
SqlConnection SqlConnection = new SqlConnection (StrConnect);
Try
{
SqlDataAdapter SqlDataAdapter = New SqlDataAdapter (SP, SqlConnection);
SqlDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < xsd. Tables["in"]. Columns.count; i++)
{
String strcolumnname = xsd. Tables["in"]. Columns[i]. ColumnName;
SQLDATAADAPTER.SELECTCOMMAND.PARAMETERS.ADD ("@" + strcolumnname, (XSD). Tables["in"]. Rows[0]) [i]);
}
for (int j = 0; J < xsd.) tables["Out". Columns.count; J + +)
{
String strcolumnname = xsd. tables["Out". COLUMNS[J]. ColumnName;
SQLDATAADAPTER.SELECTCOMMAND.PARAMETERS.ADD ("@" + Strcolumnname, xsd.) tables["Out". COLUMNS[J]. DataType);
sqldataadapter.selectcommand.parameters["@" + strcolumnname]. Direction = ParameterDirection.Output;
}
Sqldataadapter.fill (XSD, "out");
}
catch (SqlException ex)
{
Strex = ex. message;
}
Finally
{
Sqlconnection.close ();
}
}
}