常用OleDb连接数据库字符串和例子

来源:互联网 发布:淘宝可以自定义皮肤吗 编辑:程序博客网 时间:2024/04/29 11:39

    //1.OleDbConnection.ConnectinString:
    //1).oracle--->"Provider=MSDAORA/OraOledb.Oracle; Data Source=ORACLE8i7;User ID=;Password=;"
    //2).access--->"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:/bin/LocalAccess40.mdb"
    //3).sqlserver--->"Provider=SQLOLEDB;Data Source=MySQLServer;User ID=;Password=;"
    //    4).sqlserver-->"Provider=sqloledb;database=mysqlserver;uid=pwd=;";
    //常用连接数据字符串和例子
    class ClassOleDb
    {
        private System.Data.OleDb.OleDbConnection _connection;
        private System.Data.OleDb. OleDbCommand _commandCollection;
        //private System.Data.OleDb.OleDbCommand[] _commandCollection;

        private void InitConnection()
        {
            this._connection = new System.Data.OleDb.OleDbConnection();
            this._connection.ConnectionString =global::MyExcel.Properties.Settings.Default.priceConnectionString;
            _commandCollection = new OleDbCommand();
          
        }

        public ClassOleDb()
        {
            InitConnection(); 
        }

        /// <summary>
        /// 输入序号返回价格
        /// </summary>
        /// <param name="jihao"></param>
        /// <returns></returns>
        public string selcet(string jihao)
        {
            string selectstr = "SELECT 价格 FROM jiage WHERE 序号 = '" + @jihao+"'";
            _commandCollection.CommandText = selectstr;
            _commandCollection.CommandType = CommandType.TableDirect;
            _commandCollection.Connection= _connection;
            _connection.Open();
            OleDbDataReader reader = _commandCollection.ExecuteReader();

            string returnvalue = "0";
            if (reader.Read())
            {
                returnvalue = reader[0].ToString();
            }
            reader.Close();
            _connection.Close();
            return returnvalue;
        }

原创粉丝点击