/// <summary> /// 创建SqlServer(2000, 2005, 2008)数据访问类的实例。 /// 该方法在多线程下是安全的。 /// </summary> /// <param name="server">服务器名。</param> /// <param name="database">数据库名。</param> /// <param name="userId">用户名。</param> /// <param name="password">密码。</param> /// <returns>SqlServer(2000, 2005, 2008)数据访问类的实例。</returns> /// <exception cref="ArgumentException">如果server、database、userId任意一个为空,则抛出该异常。</exception> public static DataAccess CreateSqlServerInstance(string server, string database, string userId, string password) { lock (LockObj) { string error = Properties.Resources.InvalidSqlServerConnectionInfo; if (string.IsNullOrEmpty(server)) { throw new ArgumentException(error, "server"); } if (string.IsNullOrEmpty(database)) { throw new ArgumentException(error, "database"); } if (string.IsNullOrEmpty(userId)) { throw new ArgumentException(error, "userId"); } string provider = "System.Data.SqlClient"; //生成连接字符串 System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(); builder.Add("Data Source", server); builder.Add("Initial Catalog", database); builder.Add("User Id", userId); builder.Add("Password", password); return(new DataAccess(provider, builder.ConnectionString) { ParameterToken = '@' }); } }
private string GetConnectionString(DataStream stream) { var builder = new System.Data.SqlClient.SqlConnectionStringBuilder(); builder.Add("Data Source", stream.DBURL); builder.Add("Initial Catalog", stream.DBName); builder.Add("User Id", stream.DBUserName); builder.Add("Password", stream.DBPassword); return(builder.ConnectionString); }
private string GetConnectionString(DataStream stream) { stream.DbUserName = System.Environment.GetEnvironmentVariable("SQLSERVERUID"); stream.DbName = System.Environment.GetEnvironmentVariable("SQLSERVERDB"); stream.DbURL = System.Environment.GetEnvironmentVariable("SQLSERVERURL"); stream.DbPassword = System.Environment.GetEnvironmentVariable("SQLSERVERPWD"); var builder = new System.Data.SqlClient.SqlConnectionStringBuilder(); builder.Add("Data Source", stream.DbURL); builder.Add("Initial Catalog", stream.DbName); builder.Add("User Id", stream.DbUserName); builder.Add("Password", stream.DbPassword); return(builder.ConnectionString); }