Using datasets to implement operations on the database

Source: Internet
Author: User
Tags count empty implement
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 ();
}
}
}




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.