C#:数据库操作(待补充)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data.OleDb;

using System.Data;

using System.Windows.Forms;



namespace MyCommanHelper

{

    public class DataBaseHelper

    {



    #region 字段变量



        public static volatile string Connstring = "";

        private static volatile OleDbConnection connection = null;



    #endregion



    #region 方法



        #region Access



        // Methods

        /// <summary>

        /// 获取Access2003连接字符串

        /// </summary>

        /// <param name="sFilePath">Access文件路径</param>

        /// <returns></returns>

        public static string GetAccess2003ConnectionString(string sFilePath)

        {

            return ("Provider=Microsoft.JET.OLEDB.4.0;data source=" + sFilePath);

        }



        /// <summary>

        /// 获取Access2007连接字符串

        /// </summary>

        /// <param name="sFilePath">Access文件路径</param>

        /// <returns></returns>

        public static string GetAccess2007ConnectionString(string sFilePath)

        {

            return ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath);

        }



        #endregion



        #region SqlServer



        #endregion



        #region Oracle

        public static string GetOracleBlobConnectionString(string sUser, string sPassword, string sServiceName)

        {

            return ("server=oratest;Data Source=" + sServiceName + ";User ID=" + sUser + ";Password=" + sPassword);

        }



        public static string GetOracleConnectionString(string sUser, string sPassword, string sServiceName)

        {

            return ("Provider=OraOLEDB.Oracle.1;Data Source=" + sServiceName + ";User ID=" + sUser + ";Password=" + sPassword);

        }



        #endregion



        #region 公共



        /// <summary>

        /// 初始化新实例

        /// </summary>

        /// <returns></returns>

        public static bool Init()

        {

            try

            {

                connection = new OleDbConnection(Connstring);

                return true;

            }

            catch (Exception)

            {

                return false;

            }

        }



        /// <summary>

        /// 执行非查询操作

        /// </summary>

        /// <param name="sqlStr">执行SQL语句</param>

        /// <returns>返回影响行数,值为-1 表示未创建连接实例或执行语句错误</returns>

        public static int ExecNonQuery(string sqlStr)

        {

            int rslt = -1;



            if (null == connection)

            {

                MessageBox.Show("无连接实例!");

                return rslt;

            }



            try

            {

                if (connection.State == ConnectionState.Closed)

                {

                    connection.Open();

                }

                OleDbCommand command = new OleDbCommand(sqlStr, connection);

                rslt = command.ExecuteNonQuery();

                command.Dispose();

                //connection.Close();

                //connection.Dispose();

            }

            catch (Exception ex)

            {

                throw ex;

            }

            finally

            {

                connection.Close();

            }

            return rslt;

        }



        /// <summary>

        /// 获取只包含指定的数据的数据集

        /// </summary>

        /// <param name="sqlStr">Sql指令</param>

        /// <param name="sTable">指定数据</param>

        /// <returns>数据集 值为null 表示未创建连接实例</returns>

        public static DataSet ExecQuery(string sqlStr, string sTable)

        {

            DataSet set2 = null;

            if (null == connection)

            {

                MessageBox.Show("无连接实例!");

                return set2;

            }



            try

            {

                if (connection.State == ConnectionState.Closed)

                {

                    connection.Open();

                }

                OleDbDataAdapter adapter = new OleDbDataAdapter(sqlStr, connection);

                DataSet dataSet = new DataSet();

                if (dataSet.Tables.Contains(sTable))

                {

                    dataSet.Tables[sTable].Clear();

                }

                adapter.Fill(dataSet, sTable);

                set2 = dataSet;

            }

            catch (Exception exception)

            {

                throw exception;

            }

            finally

            {

                connection.Close();

                //connection.Dispose();

                //connection = null;

            }

            return set2;

        }



        /// <summary>

        /// 获得第一行数据

        /// </summary>

        /// <param name="sql"></param>

        /// <param name="sTable"></param>

        /// <returns></returns>

        public static DataRow GetFirstRow(string sql, string sTable)

        {

            DataRow row = null;

            DataSet set = ExecQuery(sql, sTable);

            if (set.Tables[sTable].Rows.Count > 0)

            {

                row = set.Tables[sTable].Rows[0];

            }

            return row;

        }



        /// <summary>

        /// 获得第一行第一个字段的值

        /// </summary>

        /// <param name="sql"></param>

        /// <param name="sTable"></param>

        /// <returns></returns>

        public static string GetFirstFieldValue(string sql, string sTable)

        {

            string str = "";

            DataSet set = ExecQuery(sql, sTable);

            if (set.Tables[sTable].Rows.Count > 0)

            {

                DataRow row = set.Tables[sTable].Rows[0];

                str = row[0].ToString();

            }

            return str;

        }



        /// <summary>

        /// 释放连接资源

        /// </summary>

        public static void Dispose()

        {

            if (null != connection)

            {

                if (connection.State != ConnectionState.Closed)

                {

                    connection.Close();

                }

                connection.Dispose();

                connection = null;

            }

        }



        #endregion



    #endregion



    }

}

 

你可能感兴趣的:(数据库)