这就是所谓的多层架构里的数据链接层DLL了.
在某一个类里把连接数据库的方法封装,当然还有其他一些数据库操作的类都可以封起来.用的时候只要调用就可以.

解决方案 »

  1.   

        public static class DbConnect
        {
            private const string m_strConnString = "Provider=microsoft.jet.oledb.4.0;data source=c:\\school.mdb";         public static OleDbConnection GetConnection()
            {
                return new OleDbConnection(m_strConnString); 
            }
        }        protected void Page_Load(object sender, EventArgs e)
            {
                OleDbConnection conn = DbConnect.GetConnection();
                conn.Open();
            }如果只是达到楼主要求的功能的话,这样就可以了,但是这么做会把数据库的操作都放到表现层,未体现出分层的概念。
      

  2.   

    你看看PETSHOP先你的软件不是一个好的面向对象的软件。不要把数据层和表现层放在一起。
      

  3.   

    namespace PetShop.DBUtility {    /// <summary>
        /// The SqlHelper class is intended to encapsulate high performance, 
        /// scalable best practices for common uses of SqlClient.
        /// </summary>
        public abstract class SqlHelper {        //Database connection strings
            public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["SQLConnString1"].ConnectionString;
            public static readonly string ConnectionStringInventoryDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString2"].ConnectionString;
            public static readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString3"].ConnectionString;
    public static readonly string ConnectionStringProfile = ConfigurationManager.ConnectionStrings["SQLProfileConnString"].ConnectionString;

            // Hashtable to store cached parameters
            private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());        /// <summary>
            /// Execute a SqlCommand (that returns no resultset) against the database specified in the connection string 
            /// using the provided parameters.
            /// </summary>
            /// <res>
            /// e.g.:  
            ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
            /// </res>
            /// <param name="connectionString">a valid connection string for a SqlConnection</param>
            /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
            /// <param name="commandText">the stored procedure name or T-SQL command</param>
            /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
            /// <returns>an int representing the number of rows affected by the command</returns>
            public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) {            SqlCommand cmd = new SqlCommand();            using (SqlConnection conn = new SqlConnection(connectionString)) {
                    PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                    int val = cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                    return val;
                }
            }        /// <summary>
            /// Execute a SqlCommand (that returns no resultset) against an existing database connection 
            /// using the provided parameters.
            /// </summary>
            /// <res>
            /// e.g.:  
            ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
            /// </res>
            /// <param name="conn">an existing database connection</param>
            /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
            /// <param name="commandText">the stored procedure name or T-SQL command</param>
            /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
            /// <returns>an int representing the number of rows affected by the command</returns>
            public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) {            SqlCommand cmd = new SqlCommand();            PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
                int val = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                return val;
            }        /// <summary>
            /// Execute a SqlCommand (that returns no resultset) using an existing SQL Transaction 
            /// using the provided parameters.
            /// </summary>
            /// <res>
            /// e.g.:  
            ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
            /// </res>
            /// <param name="trans">an existing sql transaction</param>
            /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
            /// <param name="commandText">the stored procedure name or T-SQL command</param>
            /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
            /// <returns>an int representing the number of rows affected by the command</returns>
            public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) {
                SqlCommand cmd = new SqlCommand();
                PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
                int val = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                return val;
            }
    看看高手的对你有帮助他用WEB.CONFIG的读取
      

  4.   

    - -
    看看我的资源里有用Access做的东西。里面有源码。
      

  5.   

    楼上的都已经回答了,自己写一个类,把数据库相关的操作封装好。以后需要的时候,直接调用就行了。class DB
        {
            private bool DBIsAlive;        public SqlConnection Conn1;
            public SqlCommand Cmd1;
            public SqlDataReader sdr;
            public string ErrMsg;
            public string DBLocation, DBName, DBUser, DBPaswd;
            private string con_str;
            private int ConnectDB()
            {
                int reuslt = 1;//·µ»ØÖµ
                if (DBLocation == "" || DBName == "" || DBUser == "" || DBPaswd == "")
                    return -1;  
                string ConStr = "uid=" + DBUser + ";password=" + DBPaswd + ";database=" + DBName + ";server=" + DBLocation;
                Conn1 = new SqlConnection();
                Conn1.ConnectionString = ConStr;
                con_str = ConStr;
                
                try
                {
                    Conn1.Open();
                    DBIsAlive = true;
                }
                catch(Exception ee)
                {
                    Conn1.Close();
                    DBIsAlive = false;
                    ErrMsg = ee.Message;
                    reuslt = -2;    //Êý¾Ý¿âÁ¬½Óʧ°Ü
                }
                return reuslt;
            }        private int DisconnectDB()
            {
                int result = 1;
                if (DBIsAlive)
                {
                    try
                    {
                        Conn1.Close();
                        //Conn1.Dispose();
                    }
                    catch(Exception ee)
                    {
                        ErrMsg = ee.Message;
                        result = -3; //Êý¾Ý¿â¶Ï¿ªÊ§°Ü
                    }
                }
                else
                {
                    try
                    {
                        //Conn1.Dispose();
                        Conn1.Close();
                    }
                    catch(Exception ee)
                    {
                        result = -3;
                        ErrMsg = ee.Message;
                    }
                }
                return result;
            }        public bool DB_Content_Test()
            {
                bool result = false;
                if (ConnectDB() == 1)
                    result = true;
                if (DisconnectDB() != 1)
                    result = false;
                return result;
            }                public SqlDataReader SelectDetial(string command)
            {
                
                if (command != null)
                {
                    ConnectDB();
                    Cmd1 = new SqlCommand();
                    Cmd1.CommandText = command;
                    Cmd1.Connection = Conn1;
                    sdr = Cmd1.ExecuteReader();
                }
                return sdr;
            
            }        public void UpdateDetial(string command)
            {            if (command != null)
                {
                    ConnectDB();
                    Cmd1 = new SqlCommand();
                    Cmd1.CommandText = command;
                    Cmd1.Connection = Conn1;
                    Cmd1.ExecuteNonQuery();
                }
                        }
            
        }