回 帖 发 新 帖 刷新版面

主题:关于连接access数据库

请问:
在asp.net中使用 c#连接access数据库的完整代码是什么?
谢谢!!!

回复列表 (共4个回复)

沙发

定义为:"provider=Microsoft.Jet.OLEDB.4.0;data source=MyDrive:MyPath"
实际的例子:
string strconnection="Provider="Microsoft.Jet.OLeDb.4.0;";
strconnection+=@"Data Source=c:\a.mdb";   //具体的数据库文件名称和路径
OleDbConnection objcon=new OleDbConnection(strconnection);
objcon.Open();

板凳

public class MSAccessDB
    {
        private OleDbConnection AccessConn = new System.Data.OleDb.OleDbConnection();
        public MSAccessDB()
        {
            // 
            // TODO: 在此处添加构造函数逻辑 
            // 
        }

        #region"Open 用于打开数据库的方法"
        /// <summary> 
        /// 用于打开Access数据库 
        /// </summary> 
        /// <returns>返回类型为 Bool 类型</returns> 
        private bool Open()
        {
            try
            {
                string dbm = ConfigurationSettings.AppSettings["DBConnectionStrings"].ToString();
                AccessConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbm;// +@"App_Data\ysun.mdb"; //+ ";User ID=Admin;Database Password="; 
                AccessConn.Open();
                return true;
            }
            catch(Exception ex)
            {
                throw ex;
                return false;
            }
        }
        #endregion

        #region"用于关闭连接"
        private void ConnClose()
        {
            if (ConnState())
            {
                AccessConn.Close();
            }
        }
        #endregion

        #region"ConnState 用于返回当前连接的状态"
        private bool ConnState()
        {
            if (AccessConn.State.ToString() == "Open")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

3 楼

#region"ExecuteNonQuery 用于运行SQL语句的方法"
        /// <summary> 
        /// 用于运行SQL语句的方法 
        /// 返回值为一个bool 
        /// </summary> 
        /// <param name="CmdStr">要运行的 SQL 语句</param> 
        /// <returns>返回值为 bool 类型 </returns> 
        public bool ExecuteNonQuery(string CmdStr)
        {
            try
            {
                if (Open())
                {
                    OleDbCommand cmd1 = new OleDbCommand(CmdStr, AccessConn);
                    //cmd1.CommandText = @CmdStr;
                    cmd1.ExecuteNonQuery();//运行SQL语句 
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
            finally
            {
                ConnClose();
            }
        }
        #endregion

        #region "GetDataSet 用于运行SQL语句的方法"
        /// <summary> 
        /// 返回值为一个DataSet 
        /// </summary> 
        /// <param name="CmdStr">要运行的 SQL 语句</param> 
        /// <returns>返回值为 DataSet 类型 </returns> 
        public DataSet GetDataSet(string CmdStr)
        {
            try
            {
                if (Open())
                {
                    DataSet MyDataSet = new DataSet();
                    OleDbDataAdapter myaccessdp = new OleDbDataAdapter();
                    myaccessdp.SelectCommand = new OleDbCommand(@CmdStr, AccessConn);
                    myaccessdp.Fill(MyDataSet);
                    return MyDataSet;
                }
                return null;
            }
            catch
            {
                return null;
            }
            finally
            {
                ConnClose();
            }
        }
        #endregion

        #region "GetDataTable 用于运行SQL语句的方法"
        /// <summary> 
        /// 返回值为一个DataTable 
        /// </summary> 
        /// <param name="CmdStr">要运行的 SQL 语句</param> 
        /// <returns>返回值为 DataSet 类型 </returns> 
        public DataTable GetDataTable(string CmdStr)
        {
            try
            {
                DataSet ds = GetDataSet(CmdStr);
                if (ds.Tables.Count > 0)
                {
                    return ds.Tables[0];
                }
                return null;
            }
            catch
            {
                return null;
            }
        }
        #endregion

    }

4 楼

#region"ExecuteNonQuery 用于运行SQL语句的方法"
        /// <summary> 
        /// 用于运行SQL语句的方法 
        /// 返回值为一个bool 
        /// </summary> 
        /// <param name="CmdStr">要运行的 SQL 语句</param> 
        /// <returns>返回值为 bool 类型 </returns> 
        public bool ExecuteNonQuery(string CmdStr)
        {
            try
            {
                if (Open())
                {
                    OleDbCommand cmd1 = new OleDbCommand(CmdStr, AccessConn);
                    //cmd1.CommandText = @CmdStr;
                    cmd1.ExecuteNonQuery();//运行SQL语句 
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
            finally
            {
                ConnClose();
            }
        }
        #endregion

        #region "GetDataSet 用于运行SQL语句的方法"
        /// <summary> 
        /// 返回值为一个DataSet 
        /// </summary> 
        /// <param name="CmdStr">要运行的 SQL 语句</param> 
        /// <returns>返回值为 DataSet 类型 </returns> 
        public DataSet GetDataSet(string CmdStr)
        {
            try
            {
                if (Open())
                {
                    DataSet MyDataSet = new DataSet();
                    OleDbDataAdapter myaccessdp = new OleDbDataAdapter();
                    myaccessdp.SelectCommand = new OleDbCommand(@CmdStr, AccessConn);
                    myaccessdp.Fill(MyDataSet);
                    return MyDataSet;
                }
                return null;
            }
            catch
            {
                return null;
            }
            finally
            {
                ConnClose();
            }
        }
        #endregion

        #region "GetDataTable 用于运行SQL语句的方法"
        /// <summary> 
        /// 返回值为一个DataTable 
        /// </summary> 
        /// <param name="CmdStr">要运行的 SQL 语句</param> 
        /// <returns>返回值为 DataSet 类型 </returns> 
        public DataTable GetDataTable(string CmdStr)
        {
            try
            {
                DataSet ds = GetDataSet(CmdStr);
                if (ds.Tables.Count > 0)
                {
                    return ds.Tables[0];
                }
                return null;
            }
            catch
            {
                return null;
            }
        }
        #endregion

    }

我来回复

您尚未登录,请登录后再回复。点此登录或注册