Chinaunix首页 | 论坛 | 博客
  • 博客访问: 111871
  • 博文数量: 30
  • 博客积分: 2051
  • 博客等级: 大尉
  • 技术积分: 345
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-26 08:31
文章分类

全部博文(30)

文章存档

2017年(2)

2016年(1)

2014年(1)

2011年(2)

2009年(24)

我的朋友

分类:

2009-01-04 16:58:32

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.OleDb;

namespace My
{
    ///
    /// db 的摘要说明
    ///

    public class Base
    {

        static String dbname = System.Web.HttpContext.Current.Server.MapPath("~/app_data/info.mdb");
        static String strConn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" + dbname;

        protected static string strSQL;

        public Base()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }


        ///
        /// executing SQL commands
        ///

        /// string
        /// return int
        protected static int ExecuteSql(string strSQL)
        {
            OleDbConnection myCn = new OleDbConnection(strConn);
            OleDbCommand myCmd = new OleDbCommand(strSQL, myCn);
            
            try
            {
                myCn.Open();
                myCmd.ExecuteNonQuery();
                return 0;
            }
            catch (System.Data.OleDb.OleDbException e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                myCmd.Dispose();
                myCn.Close();
            }
        }


        ///
        ///executing SQL commands
        ///

        /// 要执行的SQL语句,为字符串类型string
        /// 返回执行情况,整形int
        protected static int ExecuteSqlEx(string strSQL)
        {
            OleDbConnection myCn = new OleDbConnection(strConn);
            OleDbCommand myCmd = new OleDbCommand(strSQL, myCn);

            try
            {
                myCn.Open();
                //只能这样创建一个SqlDataReader,因为该类没有公共的构造函数mfq                
                OleDbDataReader myReader = myCmd.ExecuteReader();
                //只调用一次,只能返回一行数据记录
                if (myReader.Read())
                {
                    return 0;
                }
                else
                {
                    throw new Exception("Value Unavailable!");
                }
            }
            catch (System.Data.OleDb.OleDbException e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                myCmd.Dispose();
                myCn.Close();
            }
        }


        ///
        /// get dataset
        ///

        /// (string)
        /// (DataSet)
        protected static DataSet ExecuteSql4Ds(string strSQL)
        {
            OleDbConnection myCn = new OleDbConnection(strConn);
            try
            {
                myCn.Open();
                OleDbDataAdapter sda = new OleDbDataAdapter(strSQL, myCn);
                DataSet ds = new DataSet("ds");
                sda.Fill(ds);
                return ds;
            }
            catch (System.Data.OleDb.OleDbException e)
            {
                throw new Exception("查询出错:" + e.Message);
            }
            finally
            {
                myCn.Close();
            }
        }


        ///
        /// get single value
        ///

        /// (string)
        /// (int)
        protected static int ExecuteSql4Value(string strSQL)
        {
            OleDbConnection myCn = new OleDbConnection(strConn);
            OleDbCommand myCmd = new OleDbCommand(strSQL, myCn);
            try
            {
                myCn.Open();
                object r = myCmd.ExecuteScalar();
                if (Object.Equals(r, null))
                {
                    throw new Exception("Value Unavailable!");
                }
                else
                {
                    return (int)r;
                }
            }
            catch (System.Data.OleDb.OleDbException e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                myCmd.Dispose();
                myCn.Close();
            }
        }


        ///
        /// get object
        ///

        /// (string)
        /// (object)
        protected static object ExecuteSql4ValueEx(string strSQL)
        {
            OleDbConnection myCn = new OleDbConnection(strConn);
            OleDbCommand myCmd = new OleDbCommand(strSQL, myCn);
            try
            {
                myCn.Open();
                object r = myCmd.ExecuteScalar();
                if (Object.Equals(r, null))
                {
                    throw new Exception("object unavailable!");
                }
                else
                {
                    return r;
                }
            }
            catch (System.Data.OleDb.OleDbException e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                myCmd.Dispose();
                myCn.Close();
            }
        }


        ///
        /// execute multipul SQL commands 
        ///

        /// string
        /// int
        /*protected static int ExecuteSqls(string[] strSQLs)
        {
            OleDbConnection myCn = new OleDbConnection(strConn);
            OleDbCommand myCmd = new OleDbCommand();
            int j = strSQLs.Length;

            try
            {
                myCn.Open();
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                throw new Exception(e.Message);
            }
            SqlTransaction myTrans = myCn.BeginTransaction();

            try
            {
                myCmd.Connection = myCn;
                myCmd.Transaction = myTrans;

                foreach (string str in strSQLs)
                {
                    myCmd.CommandText = str;
                    myCmd.ExecuteNonQuery();
                }
                myTrans.Commit();
                return 0;
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                myTrans.Rollback();
                throw new Exception(e.Message);
            }
            finally
            {
                myCmd.Dispose();
                myCn.Close();
            }
        }*/

    }
}
阅读(938) | 评论(0) | 转发(0) |
0

上一篇:irc 命令

下一篇:连接sql底层文件

给主人留下些什么吧!~~