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

查看:842
本文介绍了如何在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,
S ystem.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天全站免登陆