C # Data Common operation functions

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Data;
Using System.Data.SqlClient;
Using System.Configuration;

Namespace DBTest
{
public class Opactiondata
{
<summary>
Get the database connection address and receive it via the App. config file
</summary>
<returns> returns a connection string information </returns>
public static string Getconn ()
{
String conn = configurationmanager.appsettings["ConnectionString"];

/*

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<appSettings>
<!--The connection string is encrypted--
<add key= "Constringencrypt" value= "false"/>
<add key= "ConnectionString" value= "server=mis01;database=db;uid=uid;pwd=test123"/>
<!--<add key= "ConnectionString" value= "Server=192.168.1.254;database=db;uid=uid;pwd=ittest"/>-->
</appSettings>
</configuration>

*/

Return conn;
}
<summary>
Executes an SQL statement that returns a data set
</summary>
<param name= "SQL" >sql statements </param>
<returns> Return Data Set </returns>
public static DataSet GetDataSet (String sql)
{
Try
{
SqlConnection con = new SqlConnection (Getconn ());
SqlDataAdapter ad = new SqlDataAdapter (sql, con);
DataSet ds = new DataSet ();
Ad. Fill (DS);
return DS;
}
Catch
{
return null;
}
}
<summary>
Executes an SQL statement from the database and returns the execution result to a data table
</summary>
<param name= "SQL" >sql statements </param>
<returns> Returns a data table information </returns>
public static DataTable getdatetable (String sql)
{
Try
{
SqlConnection con = new SqlConnection (Getconn ());
SqlDataAdapter ad = new SqlDataAdapter (sql, con);
DataSet ds = new DataSet ();
Ad. Fill (DS);
Return DS. Tables[0];
}
Catch
{
return null;
}
}
<summary>
Executes an SQL statement and returns the result of the operation, returning
</summary>
<param name= "SQL" >sql statements </param>
<returns> return impact record number </returns>
public static int Getexecute (String sql)
{
SqlConnection con = new SqlConnection (Getconn ());
Try
{
Con. Open ();
SqlCommand com = new SqlCommand (sql, con);
int row = (int) com. ExecuteNonQuery ();
Con. Close ();
return row;
}
Catch
{
Con. Close ();
return 0;
}
}
<summary>
Executes an SQL statement and returns the first column of data in the first row
</summary>
<param name= "SQL" > Execute an SQL statement </param>
<returns> returns one row of data </returns>
public static Object Getexecutescalar (String sql)
{
SqlConnection con = new SqlConnection (Getconn ());
Try
{
Con. Open ();
SqlCommand com = new SqlCommand (sql, con);
Object row = com. ExecuteScalar ();
Con. Close ();
return row;
}
catch (Exception ex)
{
Con. Close ();
return null;
}
}
<summary>
Executes multiple SQL statements to implement database transactions.
</summary>
<param name= "sqlstringlist" > Multiple SQL statements </param>
public static int Executesqltran (list<string> sqlstringlist)
{
using (SqlConnection conn = new SqlConnection (Getconn ()))
{
Conn. Open ();
SqlCommand cmd = new SqlCommand ();
Cmd. Connection = conn;
SqlTransaction TX = conn. BeginTransaction ();
Cmd. Transaction = TX;
Try
{
int count = 0;
for (int n = 0; n < sqlstringlist.count; n++)
{
String strSQL = Sqlstringlist[n];
if (strSQL. Trim (). Length > 1)
{
Cmd.commandtext = strSQL;
Count + = cmd. ExecuteNonQuery ();
}
}
Tx.commit ();
return count;
}
Catch
{
Tx. Rollback ();
return 0;
}
}
}
}
}


This article is from the "Imagine the Sky" blog, please be sure to keep this source http://kinwar.blog.51cto.com/3723399/1671475

C # Data Common operation functions

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.