public static DbDataAdapter CreateDbDataAdapter(string connName) { DbConnSettings settings = GetDbConnSettings(connName); DbDataAdapter adapter = settings.ProviderFactory.CreateDataAdapter(); return(adapter); }
private static DbConnSettings GetDbConnSettings(string connName) { lock (dbConnSettingsList) { foreach (DbConnSettings mySettings in dbConnSettingsList) { if (mySettings.ConnName.Equals(connName)) { return(mySettings); } } ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings[connName]; if (settings == null) { throw new Exception("在配置文件中没找到名称为 " + connName + " 的数据库连接串配置"); } DbConnSettings mySettings2 = new DbConnSettings(); dbConnSettingsList.Add(mySettings2); mySettings2.ConnName = connName; mySettings2.ProviderName = settings.ProviderName; mySettings2.ProviderFactory = DbProviderFactories.GetFactory(settings.ProviderName); mySettings2.ConnStr = DecryptPasswordInDbConnStr(settings.ConnectionString); return(mySettings2); } }
public static DbConnection GetDbConnection(string connName) { DbConnSettings settings = GetDbConnSettings(connName); DbConnection conn = settings.ProviderFactory.CreateConnection(); conn.ConnectionString = settings.ConnStr; return(conn); }
public static bool TestDbConnect(string connName, out string msg) { bool ok = false; StringBuilder sb = new StringBuilder(); try { DbConnSettings settings = GetDbConnSettings(connName); sb.Append("<br> DB ProviderName=\"").Append(settings.ProviderName).Append("\""); DbConnection conn = settings.ProviderFactory.CreateConnection(); conn.ConnectionString = settings.ConnStr; string str = settings.ConnStr.ToUpper(); int inx1 = str.IndexOf("PASSWORD="******"PWD="); } if (inx1 < 0) { inx1 = str.IndexOf("PASSWORD"); } if (inx1 < 0) { inx1 = str.IndexOf("PWD"); } str = settings.ConnStr; if (inx1 >= 0) { int inx2 = str.IndexOf(";", inx1); if (inx2 >= 0) { str = str.Remove(inx1, inx2 - inx1 + 1); } else { str = str.Remove(inx1); } } sb.Append("<br> DB ConnectionString=\"").Append(str).Append("\""); conn.Open(); conn.Close(); sb.Append("<br>"); sb.Append("<br> 连接到数据库成功"); ok = true; } catch (Exception e) { sb.Append("<br>"); sb.Append("<br> 连接到数据库失败: "); sb.Append("<br> ").Append(e.ToString()); } msg = sb.ToString(); return(ok); }