使用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)它清楚地表明'加密=是'使用證書。 –