C#,连接数据库字符串汇总

//-------------------SQL Server连接数据库字符串-------------20161216-----------------------------
Data Source=.\SQLEXPRESS; Initial Catalog=GSSMS20160720;User ID=sa;Password=sa
注释: .\SQLEXPRESS为 服务地址      GSSMS20160720 为 数据库名  


//-------------------ACCESS连接数据库字符串----------20161216-----------------------------
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=data.accdb     连接没加密数据库存字符串
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=data.accdb;Jet OLEDB:Database Password=123456    连接加密数据库存字符串
注释:Meter.mdb   ACCESS文件 直接这样写为程序根目录

//--------------------SQLite连接字符串-----20171031-------------------
Data Source= " + AppDomain.CurrentDomain.BaseDirectory + "DataBase\\address.db" + ";Pooling=true;FailIfMissing=false 
注释 " + AppDomain.CurrentDomain.BaseDirectory + "DataBase\\address.db" 为Sqlite文件址址


//--------------------Oracle连接字符串-----20180506-------------------

data source = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = IP地址)(PORT = 端串口号默认的是1521)))(CONNECT_DATA = (SERVICE_NAME = 实体名))); User ID = 帐号; Password = 密码; Pooling = True; Max Pool Size = 10; Min Pool Size = 1;

        /// <summary>
        ///SQL Server  测试数据是否连接    20180522
        /// </summary>
        /// <param name="connStr">连接字符串</param>
        /// <returns></returns>
        public string TestConnection(string connStr)
        {
            try
            {
                //SqlConnection是SQL server  OleDbConnection是Access不同的数据库不一样
                using (SqlConnection connection = new SqlConnection(connStr))
                {
                    connection.Open();
                    if (connection.State.ToString().Contains("Open"))
                    {
                        return "连接成功";
                    }
                    else
                    {
                        return "未连接成功";
                    }
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }


评论