C#数据公共操作函数

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>
        /// 获取数据库连接地址,通过app.config文件来进行接收
        /// </summary>
        /// <returns>返回一个连接字符串信息</returns>
        public static string GetConn()
        {
            string conn = ConfigurationManager.AppSettings["ConnectionString"];

/*

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!-- 连接字符串是否加密-->
    <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>
        /// 执行一条sql语句,返回一个数据集
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns>返回数据集</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>
        /// 从数据库中执行一条sql语句,并将执行结果返回到一个数据表中
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns>返回一个数据表格信息</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>
        /// 执行一条sql语句并将操作结果,返回
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns>返回影响记录条数</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>
        /// 执行一条sql语句,并将第一行第一列数据返回
        /// </summary>
        /// <param name="sql">执行一条sql语句</param>
        /// <returns>返回一行一列的数据</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>
        /// 执行多条SQL语句,实现数据库事务。
        /// </summary>
        /// <param name="SQLStringList">多条SQL语句</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;
                }
            }
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章