C # classes full access to SQlServer

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.ServiceModel.Web;
using System.Text;

namespace MDK.NT.Common
{
    public class DBHelper
    {
        private string m_dbs;

        /// <summary>
        /// 构造函数
        /// </summary>
        public DBHelper() { }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="connectString">数据库连接字符串</param>
        the DBHelper public (String connectString) 
        {
            m_dbs = connectString; 
        } 
        public String the ConnectString 
        { 
            GET m_dbs {return;} 
            SET = {m_dbs value;} 
        } 

        /// <Summary> 
        /// inserted and acquires ID 
        /// </ Summary> 
        / // <param name = "connectString" > database connection string </ param> 
        /// <param name = "commandStr"> command to obtain the SQL statement contains the ID </ param> 
        /// <Returns> new record ID </ Returns> 
        public int ExecuteScalarInsert (connectString String, String commandStr) 
        { 
            String ERR = "";
            int K = 0; 
            IF (String.IsNullOrEmpty (connectString ))
            {
                return -1;
            }
            using (SqlConnection dbc = new SqlConnection(connectString))
            {
                SqlCommand insert = new SqlCommand(commandStr, dbc);

                try
                {
                    dbc.Open();
                    ret = Convert.ToInt32(insert.ExecuteScalar());
                }
                catch(Exception ex)
                {
                    err = ex.Message;
                }
            }

            if (err.Length > 0)
            {
                new new the WebFaultException the throw <SimpleException> (new new SimpleException () {} ERR = the Message, HttpStatusCode.InternalServerError); 
            } 
            the else 
            { 
                return RET; 
            } 
        } 

        /// <Summary> 
        /// inserted and acquires ID 
        /// </ Summary> 
        /// <param name = "commandStr" > SQL statement contains the ID acquisition command </ param> 
        /// <Returns> new record ID </ Returns> 
        public int ExecuteScalarInsert (String commandStr) 
        { 
            String ERR = ""; 
            RET = 0 int; 
            IF (String.IsNullOrEmpty(m_dbs ))
            {
                return -1;
            }
            using (SqlConnection dbc = new SqlConnection(m_dbs))
            {
                SqlCommand insert = new SqlCommand(commandStr, dbc);

                try
                {
                    dbc.Open();
                    ret = Convert.ToInt32(insert.ExecuteScalar());
                }
                catch(Exception ex)
                {
                    err = ex.Message;
                }
            }

            if (err.Length > 0)
            {
                throw new WebFaultException<SimpleException>(new SimpleException() { Message = err }, HttpStatusCode.InternalServerError);
            }
            else
            {
                return ret;
            }
        }

        /// <Summary> 
            {
        /// add, delete, update operations 
        /// </ Summary> 
        /// <param name = "connectString"> database connection string </ param> 
        /// <param name = "commandstr"> the SQL statement </ param> 
        /// <Returns> number of rows affected </ Returns> 
        public int CommandExecuteNonQuery (connectString String, String commandstr) 
        { 
            IF (String .IsNullOrEmpty (connectString) || String .IsNullOrEmpty (commandstr)) 
            { 
                return -1; 
            } 
            String ERR = ""; 
            int Result = 0;
            using (SqlConnection dbc = new SqlConnection(connectString))
                SqlCommand command = new SqlCommand(commandstr, dbc);
                try
                {
                    dbc.Open();
                    result = command.ExecuteNonQuery();
                }
                catch(Exception ex)
                {
                    err = ex.Message;
                }
            }

            if (err.Length > 0)
            {
                throw new WebFaultException<SimpleException>(new SimpleException() { Message = err }, HttpStatusCode.InternalServerError);
            }
            else
            {
                return result;
            }
            
        }
        
        /// <summary>
        /// 添加、删除、更新操作
        /// </summary>
        /// <param name="commandstr">SQL语句</param>
        /// <returns>受影响的行数</returns>
        public int CommandExecuteNonQuery(string commandstr)
        {
            if (string.IsNullOrEmpty(m_dbs )||string .IsNullOrEmpty (commandstr ))
            {
                return -1;
            }
            string err = "";
            int result = 0;
            using (SqlConnection dbc = new SqlConnection(m_dbs))
            {
                SqlCommand command = new SqlCommand(commandstr, dbc);
                try
                {
                    dbc.Open();
                    result = command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    err = ex.Message;
                }
            }

            if (err.Length > 0)
            {
                throw new WebFaultException<SimpleException>(new SimpleException() { Message = err }, HttpStatusCode.InternalServerError);
            }
            else
            {
                return result;
            }
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="connectString">数据库连接字符串</param>
        /// <param name="selectstr">SQL语句</param>
        /// <returns>数据表</returns>
        public DataTable GetCommand(string connectString, string selectstr)
        {
            if(string .IsNullOrEmpty (connectString )||string .IsNullOrEmpty (selectstr ))
            {
                return null;
            }
            DataTable table = new DataTable();

            string err = "";
            using (SqlConnection dbc = new SqlConnection(connectString))
            {
                try
                {
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.SelectCommand = new SqlCommand(selectstr, dbc);
                    adapter.Fill(table);
                }
                catch(Exception ex)
                {
                    err = ex.Message;
                }
            }

            if (err.Length > 0)
            {
                throw new WebFaultException<SimpleException>(new SimpleException() { Message = err }, HttpStatusCode.InternalServerError);
            }
            else
            {
                return table;
            }
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="selectstr">SQL语句</param>
        /// <returns>数据表</returns>
        public DataTable GetCommand(string selectstr)
        {
            if (string.IsNullOrEmpty(m_dbs))
            {
                return null;
            }
            DataTable table = new DataTable();
            string err = "";
            using (SqlConnection dbc = new SqlConnection(m_dbs))
            {
                try
                {
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.SelectCommand = new SqlCommand(selectstr, dbc);
                    adapter.Fill(table);
                }
                catch(Exception ex)
                {
                    err = ex.Message;
                }
            }

            IF (err.Length> 0) 
            {
                the throw new new the WebFaultException <SimpleException> (new new SimpleException () {} ERR = the Message, HttpStatusCode.InternalServerError); 
            } 
            the else 
            { 
                return Table; 
            } 
        } 

        /// <Summary> 
        /// execute a transaction 
        /// </ summary> 
        All statements /// <param name = "commands" > transaction to be executed </ param> 
        /// <returns a> transaction is executed successfully </ returns a> 
        public BOOL ExecuteTransaction (List <String > Commands) 
        { 
            IF (String.IsNullOrEmpty (m_dbs)||commands ==null )
            {
                return false; 
            } 

            String ERR = "";
            bool ret = false;
            using (SqlConnection dbc = new SqlConnection(m_dbs))
            {
                dbc.Open();
                using (SqlTransaction transaction = dbc.BeginTransaction())
                {
                    try
                    {
                        foreach (string commandstr in commands)
                        {
                            SqlCommand command = new SqlCommand(commandstr, dbc);
                            command.Transaction = transaction;
                            command.ExecuteNonQuery();
                        }
                        transaction.Commit();
                        ret = true;
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        err = ex.Message;
                    }
                }
            }

            if (err.Length > 0)
            {
                throw new WebFaultException<SimpleException>(new SimpleException() { Message = err }, HttpStatusCode.InternalServerError);
            }
            else
            {
                return ret;
            }
        }

        /// <Summary> 
        /// 执行一个事务
        /// </summary>
        /// <param name = "connectString"> database connection string </ param> 
        All statements /// <param name = "commands" > transaction to be performed </ param> 
        /// <returns> successful execution of the transaction </ Returns> 
        public BOOL ExecuteTransaction (String connectString, List <String> Commands) 
        { 
            iF (String.IsNullOrEmpty (connectString) || Commands == null) 
            { 
                return to false; 
            } 

            String ERR = "" ; 
            BOOL RET = to false; 
            the using (the SqlConnection the SqlConnection new new DBC = (connectString)) 
            { 
                dbc.Open ();
                using (SqlTransaction transaction = dbc.BeginTransaction())
                {
                    try
                    {
                        foreach (string commandstr in commands)
                        {
                            SqlCommand command = new SqlCommand(commandstr, dbc);
                            command.Transaction = transaction;
                            command.ExecuteNonQuery();
                        }
                        transaction.Commit();
                        ret = true;
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        err = ex.Message;
                    }
                }
            }

            if (err.Length > 0)
            {
                throw new WebFaultException<SimpleException>(new SimpleException() { Message = err }, HttpStatusCode.InternalServerError);
            }
            else
            {
                return ret;
            }
        }
    }
}

  This class writing and writing, pretending to be recorded.

Guess you like

Origin www.cnblogs.com/zebra-bin/p/11098468.html