zoukankan      html  css  js  c++  java
  • .NET Core中ADO.NET SqlClient的使用与常见问题

    一、简介

      在很多要求性能的项目中,我们都要使用传统的ADO.NET的方式来完成我们日常的工作;目前有一些网友问有关于.NET Core操作SQL Server的问题在本文中解答一下。

      本文旨在指出,在.NET Core中使用ADO.NET SqlClient操作SQL SERVER数据库时的一些常见的问题,在本文的第三部分,如果只关心解决问题,请跳过第两部分进行阅读。

    二、使用ADO.NET

      首先建立好一个ASP.NET MVC Core Project 或 .NET Core Class Library Project , 当然也可以是一个控制台程序;

      要使用ADO.NET和SQLCLient就要引用System.Data.CommonSystem.Data.SqlClient两个程序集,点这两个名称可以跳到它们的Nuget地址。

      在.NET CORE的ADO.NET中功能被程序集所划分,其实System.Data.Common封装的就是ADO.NET的抽象部分,它包含如下命名空间和类型:

    System.Data.Common.DbConnection
    System.Data.Common.DbException
    System.Data.Common.DbParameter
    System.Data.DbType
    System.Data.Common.DbDataReader
    System.Data.Common.DbCommand
    System.Data.Common.DbTransaction
    System.Data.Common.DbParameterCollection
    System.Data.Common.DbProviderFactory

      可以使用两种方法进行安装:

     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.使用SqlClient

    using System;
    using System.Data.SqlClient;
    
    namespace DBTest
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                using (SqlConnection con = new SqlConnection(ConnStr)) {
                    con.Open();
                    try {
                        using (SqlCommand command = new SqlCommand("SELECT * FROM SAMPLETABLE", con)) {
                            command.ExecuteNonQuery();
                        }
                    }
                    catch {
                        Console.WriteLine("Something went wrong");
                    }
                }
                Console.Read();
            }
        }
    }

    三、常见问题

     1.SQL Server版本问题

      这个问题,表象上体现的是一个连接超时的错误:

      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 was not accessible. Verify that 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 errors occurred. (No such device or address) ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address

      但是这个错误却是因为SQL Server的版本问题造成的,.NET Core中的SqlClient能支持的SQL Server最小版本为 SQL Server 2008 R2 SP3,如果你的数据库小于这个版本,就会出现这个异常。

      官方的Issues在此:https://github.com/dotnet/corefx/issues/9719

      SQL Server 2008 R2 SP3补丁的下载地址如下:

      https://www.microsoft.com/zh-cn/download/details.aspx?id=44271

      还有就是将连接字符串中的加入Mul‌​tipleActiveResultSet‌​s=false

     2.Runtime运行时问题

      在部署到Windows和IIS时,System.Data.SqlClient 这个程序集在Windows环境用会依赖于VC++的运行时,目前依赖的为: Microsoft Visual C++ 2012 Runtime

  • 相关阅读:
    php intval()函数
    MVC开发模式
    Session详解
    JSP入门
    Response中文乱码问题
    cookie入门
    idea实现更改servlet模板
    使用new和newInstance()创建类的区别
    Servlet 3.0 新特性详解
    web Servlet 3.0 新特性之web模块化编程,web-fragment.xml编写及打jar包
  • 原文地址:https://www.cnblogs.com/maxzhang1985/p/5941495.html
Copyright © 2011-2022 走看看