First, Introduction
In many of the projects that require performance, we use the traditional ADO to do our daily work, and there are some netizens asking questions about . NET Core Operations SQL Server to answer in this article.
The purpose of this article is to point out some common problems with using the ADO . NET SqlClient operation of SQL Server databases, in the third part of this article, if you only care about resolving the problem, skip the first two sections to read.
second, the use of ADO
Start by building good one ASP. NET MVC Core project or. NET Core Class Library project, of course, it can also be a console program;
To use ADO and SqlClient, you will refer to the System.Data.Common and System.Data.SqlClient two assemblies, and the point two names can jump to their nuget addresses.
In. NET core, the functionality is partitioned by the Assembly, in fact, System.Data.Common encapsulation is ADO. NET , which contains the following namespaces and types:
System.Data.Common.DbConnectionSystem.Data.Common.DbExceptionSystem.Data.Common.DbParameterSystem.Data.DbTypeSystem.Data . Common.DbDataReaderSystem.Data.Common.DbCommandSystem.Data.Common.DbTransactionSystem.Data.Common.DbParameterCollectionSy Stem. Data.Common.DbProviderFactory
You can install it in two ways:
1.NuGet
pm> install-package System.Data.Common
pm> install-package System.Data.SqlClient
2.project.json
"Dependencies": { "System.Data.Common": "4.1.0-*", "System.Data.SqlClient": "4.1.0 -* ", " system.runtime ":" 4.1.0-* "}
3. Using SqlClient
usingSystem;usingSystem.Data.SqlClient;namespacedbtest{ Public classProgram { Public Static voidMain (string[] args) { using(SqlConnection con =NewSqlConnection (CONNSTR)) {con. Open (); Try { using(SqlCommand command =NewSqlCommand ("SELECT * from Sampletable", con)) {command. ExecuteNonQuery (); } } Catch{Console.WriteLine ("Something went wrong"); }} console.read (); } }}
third, frequently asked questions1.SQL Server version issues
This problem, the appearance of a connection timeout error:
Unhandled exception:system.data.sqlclient.sqlexception:a network-related or instance-specific error occurred while Establishing a connection to SQL Server. The server was not found or is not accessible. Verify The instance name is correct and that SQL Server is configured to allow remote connections. (PROVIDER:TCP provider, Error:35-an internal exception was caught)---> System.AggregateException:One or more Erro RS occurred. (No such device or address)---> system.net.internals.socketexceptionfactory+extendedsocketexception:no such device or address
However, this error is due to the SQL Server version issue, the SqlClient in. NET core can support the minimum SQL Server version of SQL Server R2 SP3, if your database is smaller than this version, this exception occurs.
The official issues here: https://github.com/dotnet/corefx/issues/9719
The SQL Server R2 SP3 patch is as follows:
https://www.microsoft.com/zh-cn/download/details.aspx?id=44271
What's more, join Mul in the connection string ? tipleactiveresultset? s=false
2.Runtime Run-time issues
When deploying to Windows and IIS, the System.Data.SqlClient assembly is currently dependent on the Windows environment when running with VC + +: Microsoft Visual C + + runtime
How to use and frequently asked questions about ADO SqlClient in. NET Core