我有一段代码想要在我的Dapper查询中切换数据库连接。
这很简单,但是当我将连接对象设置为另一个时,它实际上就无法工作。操作两侧的连接字符串保持不变。
我怀疑这与Dapper或数据库连接无关,但更根本。该代码不是我自己的,而是我掌握的东西。
我已经没有足够的想法来调试为什么会发生这种情况。关于我可以寻找的任何想法?
我冻结了所有其他线程,以确保没有其他事情可以在后台修改连接对象。
这是我正在运行的代码:
void SetConnection(){
if (Db.Diablo.Connection?.Name != Connection.ConnectionId)
{
ConnectionInfo conInfo = Connection.GetSqlConnectionInfo;
Console.WriteLine($"Previous DB.Diablo.Connection = {Db.Diablo.Connection.ConnectionString}");
Console.WriteLine($"Required DB.Diablo.Connection = {conInfo.ConnectionString}");
Db.Diablo.Connection = conInfo;
Console.WriteLine($"New DB.Diablo.Connection = {Db.Diablo.Connection.ConnectionString}");
}
}
哪个输出如下:
Previous DB.Diablo.Connection = data source = localhost\sqlexp_2008r2; initial catalog = Diablo_Demo_A; integrated security = true
Required DB.Diablo.Connection = data source = localhost\sqlexp_2008r2; initial catalog = Diablo_Demo_B; integrated security = true
New DB.Diablo.Connection = data source = localhost\sqlexp_2008r2; initial catalog = Diablo_Demo_A; integrated security = true
显然,它没有改变。
连接信息是DapperQuery对象的简单获取/设置属性:
public ConnectionInfo Connection{
get;
set;
}
我可以发布所有对象定义,但是要花很多时间。如果有帮助的话,我会很高兴!
谢谢。
更新:这是ConnectionInfo类型的代码:
using System;
using System.Data.SqlClient;
using JetBrains.Annotations;
// ReSharper disable MemberCanBePrivate.Global
namespace AmFunctions.Sql{
[Serializable] public class ConnectionInfo{
#region Constructors
public ConnectionInfo(string filename,[NotNull] string password,DbType type,int version){
DatabaseFileName = filename;
DatabaseType = type;
Password = password;
Version = version;
UpdateConnectionString();
}
public ConnectionInfo(string dataSource,string initialCatalog,DbType databaseType,string userId = "",[NotNull] string password = ""){
InitialCatalog = initialCatalog;
DataSource = dataSource;
UserId = userId;
Password = password;
DatabaseType = databaseType;
UpdateConnectionString();
}
public ConnectionInfo(string connectionString,DbType databaseType){
CustomConnectionString = connectionString;
IsCustomConnectionString = true;
DatabaseType = databaseType;
UpdateConnectionString();
}
#endregion
#region Constructors
ConnectionInfo(){ }
#endregion
#region Public Delegates and Enums
public enum DbType{ SqlCe,SqlLite,SqlServer,NotSet }
#endregion
#region Class Variables
string _encryption_key;
string _stored_password;
bool _use_integrated_security;
#endregion
#region Properties
public bool IsCustomConnectionString{ get; set; }
public bool UseIntegratedSecurity{
get => _use_integrated_security;
set{
_use_integrated_security = value;
UpdateConnectionString();
}
}
bool WindowsSecurity => UseIntegratedSecurity || UserId.IsnullOrWhiteSpace() || Password.IsnullOrWhiteSpace();
public DbType DatabaseType{ get; set;}
public int Version{ get; set; }
[NotNull] public string ConnectionString => getconnectionString(InitialCatalog);
public string CustomConnectionString{ get; set; }
public string DatabaseFileName{ get; set; }
public string DataSource{ get; set; }
public string InitialCatalog{ get; set; }
[NotNull] public string MasterConnectionString => getconnectionString("Master");
[NotNull] public string Password{ get => Encryption.Decrypt(_stored_password,_encryption_key); set => _stored_password = Encryption.Encrypt(value,_encryption_key); }
public string UserId{ get; set; }
public string Name{ get; set; }
public bool Isactive{ get; set; }
public bool CanTest =>(IsCustomConnectionString && CustomConnectionString.IsnotNullOrWhiteSpace()) || (CanTestMaster && !InitialCatalog.IsnullOrWhiteSpace());
public bool CanTestMaster=> !DataSource.IsnullOrWhiteSpace();
public bool TestConnection(bool throwExceptionIfNot = true,bool testConnectionToServerOnly = false)
{
switch (DatabaseType) {
case DbType.SqlCe:
return true;
case DbType.SqlLite: return true;
case DbType.SqlServer:
return TestOk_sql(throwExceptionIfNot,testConnectionToServerOnly);
case DbType.NotSet: return false;
default: throw new ArgumentOutOfRangeException();
}
}
bool TestOk_sql(bool throwExceptionIfNot,bool testConnectionToServerOnly ){
string connectionString = testConnectionToServerOnly ? MasterConnectionString : ConnectionString;
try
{
using (SqlConnection db = new SqlConnection(connectionString + ";connection timeout = 2"))
{
//Try and open it
db.Open();
db.Close();
return true;
}
}
catch (Exception)
{
if (throwExceptionIfNot) throw;
return false;
}
}
#endregion
#region Methods
public void SetEncryptionKey(string key){
_stored_password = Password;
_encryption_key = key;
Password = _stored_password;
}
public void SetPassword(string password,string encryptionKey){
_stored_password = password;
_encryption_key = "";
SetEncryptionKey(encryptionKey);
}
void UpdateConnectionString(){
//if(IsCustomConnectionString) return;
switch(DatabaseType){
case DbType.SqlCe:
CustomConnectionString = "Data Source='%server%'" + "; lcid=1033; Password=%password%;" + " Encrypt = TRUE; Case Sensitive = FALSE";
break;
case DbType.SqlLite:
string password = Password.IsnullOrWhiteSpace() ? "" : "Password = %password%; ";
CustomConnectionString = $"Data Source = {DatabaseFileName}; Version = {Version}; {password}Pooling=True; Max Pool Size=100";
break;
case DbType.SqlServer:
string securityString = WindowsSecurity ? "integrated security = true" : "user id = %username%; password = %password%";
CustomConnectionString = $"data source = %server%; initial catalog = %database%; {securityString}";
break;
default:
CustomConnectionString = "";
break;
}
}
[NotNull]
string getconnectionString(string initialCatalog){
if(CustomConnectionString==null) UpdateConnectionString();
return CustomConnectionString?.Replace("%server%",DataSource)
.Replace("%database%",initialCatalog)
.Replace("%username%",UserId)
.Replace("%password%",Password) ??
"";
}
#endregion
}
}