EDIT: I understand the migration from 100 to 101 for newer .NET projects. What I am failing to understand is how the 102 namespace communicates with SQL Server. This question comes to mind considering that Microsoft has now deprecated one of its clients, the SQL Native Client (SNAC).
i.e. *.Data.SqlClient
命名空间如何与SQL Server 2022数据库通信?随着SQL Native Client弃用的宣布,开发人员是否需要担心他们的代码库?
通过使用C#编写代码并使用Microsoft SQL Server后端,我们目前正在将数据库从SQL Server 2019迁移到较新的SQL Server 2022环境.我们的代码库显式使用System.Data.SqlClient
或更新的Microsoft.Data.SqlClient
命名空间中的SqlConnection
个对象.
根据微软的网站,这SQL Server Native Client (SNAC) is now deprecated in SQL Server 2022人.我们的代码库是否需要从使用System.Data.SqlClient.SqlConnection
个对象(或Microsoft.Data.SqlClient.SqlConnection
个)迁移,以便继续使用较新的SQL Server 2022(+)实例?
示例连接字符串:
<add name="myConnection"
connectionString="Server=DEV-SQLSERVER2019-01.mycompany.local,1433;TrustServerCertificate=True;Database=Tokens;User ID=TokensApp;Password=ABC123"
providerName="Microsoft.Data.SqlClient"/>
示例代码:
//using System.Data.SqlClient;
using Microsoft.Data.SqlClient;
private static string CreateToken(string code, string email)
{
string token;
try
{
using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString))
{
sqlConn.Open();
using (SqlCommand sqlCmd = new SqlCommand())
{
sqlCmd.Connection = sqlConn;
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = "spCreateToken";
sqlCmd.Parameters.Add("@Token", SqlDbType.UniqueIdentifier).Value = NEW_GLOBAL_TOKEN();
sqlCmd.Parameters.Add("@Code", SqlDbType.NVarChar, 6).Value = code.ToUpper();
sqlCmd.Parameters.Add("@Email", SqlDbType.NVarChar, 80).Value = email.ToLower();
token = sqlCmd.ExecuteScalar().ToString();
}
}
}
catch (SqlException exSQL) when (exSQL.Message.Contains("Violation of UNIQUE KEY constraint"))
{
throw new Exception(String.Format("This email address '{0}' has already been sent an invitiation.", email));
}
catch (Exception ex) when (ex.Message.Equals("Object reference not set to an instance of an object."))
{
throw new InvalidOperationException(String.Format("The email address provided '{0}' has already signed-up.", email));
}
return token;
}
如果此代码库确实需要更改-我应该如何在我的C#代码库中与Microsoft SQL Server 2022+数据库对接:
- 连接字符串格式
- 数据库连接类类型
- 数据库命令类类型
- 个人数据库对象类型(S)
- 单个数据库异常类类型
- 数据库交易类类型(S)
- SQL Server数据库用户定义值/表值参数类型
我已经读到了新的SQL Server 2022的弃用更改,但我不清楚这是否或如何实际影响我们的代码库,该代码库使用C#并显式调用SqlConnection
类来连接到我们的Microsoft SQL Server数据库.看起来,如果需要的话,迁移路径将很麻烦,以确保使用新的SQL客户端对象类型实现正确的功能.