如何在 ASP.NET Core 中使用 SqlClient? [英] How to use SqlClient in ASP.NET Core?

查看:59
本文介绍了如何在 ASP.NET Core 中使用 SqlClient?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 ASP.net Core 中使用 SQLClient 库,但似乎无法使其正常工作.我在网上找到了这篇关于如何设置的文章,但它对我不起作用:

还有其他人让这个工作吗?

解决方案

我想你可能错过了教程中的这一部分:

<块引用>

您需要的不是引用 System.Data 和 System.Data.SqlClient从 Nuget 获取:

System.Data.Common 和 System.Data.SqlClient.

目前这会在 project.json 中创建依赖项 –> aspnetcore50这两个库的部分.

"aspnetcore50": {依赖关系":{"System.Runtime": "4.0.20-beta-22523","System.Data.Common": "4.0.0.0-beta-22605",System.Data.SqlClient":4.0.0.0-beta-22605"}}

尝试通过 Nuget 获取 System.Data.Common 和 System.Data.SqlClient,看看这是否为您添加了上述依赖项,但简而言之,您缺少 System.Runtime.

根据 Mozarts 的回答,如果您使用 .NET Core 3+,请改用 Microsoft.Data.SqlClient.

I am trying to use SQLClient library in the ASP.net Core but cant seem to get it working. I found this article online advising how to setup but its not working for me: http://blog.developers.ba/using-classic-ado-net-in-asp-net-vnext/

I have a simple console application package. My project.json looks like this:

{
  "version": "1.0.0-*",
  "description": "DBTest Console Application",
  "authors": [ "" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",

  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "System.Data.Common": "4.0.1-beta-23516",
    "System.Data.SqlClient" :  "4.0.0-beta-23516"
  },

  "commands": {
    "DBTest": "DBTest"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": {
      "dependencies": {
        "Microsoft.CSharp": "4.0.1-beta-23516",
        "System.Collections": "4.0.11-beta-23516",
        "System.Console": "4.0.0-beta-23516",
        "System.Linq": "4.0.1-beta-23516",
        "System.Threading": "4.0.11-beta-23516"
      }
    }
  }
}

And I try the following code:

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();
        }
    }
}

But get the following errors:

Anyone else got this working?

解决方案

I think you may have missed this part in the tutorial:

Instead of referencing System.Data and System.Data.SqlClient you need to grab from Nuget:

System.Data.Common and System.Data.SqlClient.

Currently this creates dependency in project.json –> aspnetcore50 section to these two libraries.

"aspnetcore50": {
       "dependencies": {
           "System.Runtime": "4.0.20-beta-22523",
           "System.Data.Common": "4.0.0.0-beta-22605",
           "System.Data.SqlClient": "4.0.0.0-beta-22605"
       }
}

Try getting System.Data.Common and System.Data.SqlClient via Nuget and see if this adds the above dependencies for you, but in a nutshell you are missing System.Runtime.

Edit: As per Mozarts answer, if you are using .NET Core 3+, reference Microsoft.Data.SqlClient instead.

这篇关于如何在 ASP.NET Core 中使用 SqlClient?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆