Encapsulating the database Profile app configuration file

Source: Internet
Author: User
Tags connectionstrings

<connectionStrings>
<add name= "Strcon" connectionstring= "Data source=.;i Nitial Catalog=datamanager; User Id=sa; Passwoed=lifatshit "/>
</connectionStrings>

Package table

public static DataTable executedatetable (String sql, params sqlparameter[] param)
{
DataTable dt = null;
using (SqlConnection con=new SqlConnection (Strcon))
{
SqlCommand cmd = new SqlCommand (sql, con);
Cmd. Parameters.addrange (param);
Using (SqlDataAdapter adapter=new SqlDataAdapter (CMD))
{
DT = new DataTable ();
Adapter. Fill (DT);
}
}
return DT;
}

Perform additions and deletions to return the number of rows affected

public static int ExecuteNonQuery (String sql, params sqlparameter[] param)
{
int n =-1;
using (SqlConnection con = new SqlConnection (Strcon))
{
using (SqlCommand cmd = new SqlCommand (sql, con))
{
Con. Open ();
Cmd. Parameters.addrange (param);
n = cmd. ExecuteNonQuery ();
}
}
return n;

}

Execute query is responsible for returning first row column

public static Object Executedcalar (String sql, params sqlparameter[] param)
{
Object o = null;
using (SqlConnection con=new SqlConnection (Strcon))
{
using (SqlCommand cmd=new SqlCommand (Sql,con))
{
Con. Open ();
Cmd. Parameters.addrange (param);
o = cmd. ExecuteScalar ();
}
}
return o;

}

Read database

public static SqlDataReader ExecuteReader (string sql,params sqlparameter[] param)
{
SqlDataReader reader = null;
using (SqlConnection con = new SqlConnection (Strcon))
{
using (SqlCommand cmd = new SqlCommand (sql, con))
{
Con. Open ();
Cmd. Parameters.addrange (param);
reader = cmd. ExecuteReader ();
}
}
return reader;
}

Encapsulating the database Profile app configuration file

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.