使用SqlConnection
对象为您提供了两个优点。首先,您可以确保连接字符串将被正确构建,因为您可以使用SqlConnectionStringBuilder
类来构建它。第二,它的速度比OLEDB快了很多,比如多了。
要建立此连接字符串...
Initial Catalog=emp_test;Persist Security Info=True;User ID=sa;Password=***;Data Source=172.21.70.94;Provider=SQLOLEDB;Use Encryption for Data=True;Trust Server Certificate=True;
...使用SqlConnectionStringBuilder
你会写一些像这样的代码...
var builder = new SqlConnectionStringBuilder();
builder.DataSource = "172.21.70.94";
builder.Encrypt = true;
builder.TrustServerCertificate = true;
builder.InitialCatalog = emp_test;
builder.PersistSecurityInfo = true;
builder.UserID = "sa";
builder.Password = "***";
var connection = new SqlConnection(builder.ToString());
...的Encrypt
属性保存这个在.NET Framework中定义...
Gets or sets a Boolean value that indicates whether SQL Server uses SSL encryption for all data sent between the client and server if the server has a certificate installed.
... TrustServerCertificate
属性保存这个定义在.NET Framework ...
Gets or sets a value that indicates whether the channel will be encrypted while bypassing walking the certificate chain to validate trust.
所以,我要说,这是最安全的方法。您可以确保.NET Framework将正确构建连接字符串和您可以根据基于其定义的证书获得一组很好的定义。
现在,既然您也连接到Oracle,那么最好的方法就是继续构建OLEDB连接,因为您没有多少选择。但是这两个连接都是IDbConnection
,因此您只需建立一个正确连接并返回IDbConnection
的工厂。
这意味着你得到了两全其美的效果,SqlConnection
对象的性能和易用性以及IDbConnection
的抽象,这样你的代码就不必改变了。
你可以试试这个给我吗? 'Initial Catalog = emp_test; Persist Security Info = True; User ID = sa; Password = ***; Data Source = 172.21.70.94; Provider = SQLOLEDB; Encrypt = yes;' –
嘿BigM,如果我给Encrypt = yes,连接建立,我认为不应该发生没有给予TrustServerCertificate。因此它从未考虑过这个属性。 – Praveen
请参考这个帖子(http://stackoverflow.com/questions/3674160/using-encrypt-yes-in-a-sql-server-connection-string-provider-ssl-provider)它清楚地表明'加密=是'使用证书。 –