如何在ASP.NET Core中使用SqlClient?

罗伯·麦凯比(Rob McCabe):

我正在尝试在ASP.net Core中使用SQLClient库,但似乎无法正常工作。我在网上找到了这篇文章,以提供有关如何设置的建议,但不适用于我:http : //blog.developers.ba/using-classic-ado-net-in-asp-net-vnext/

我有一个简单的控制台应用程序包。我的project.json看起来像这样:

{
  "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"
      }
    }
  }
}

我尝试以下代码:

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

但是出现以下错误:

在此处输入图片说明

其他人得到了这个工作吗?

MikeDub:

我认为您可能已经错过了本教程中的这一部分:

无需引用System.Data和System.Data.SqlClient,您需要从Nuget中获取:

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

当前,这在project.json –> aspnetcore50部分中创建了对这两个库的依赖关系。

"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"
       }
}

尝试通过Nuget获取System.Data.Common和System.Data.SqlClient ,看看是否为您添加了以上依赖关系,但总的来说,您缺少System.Runtime。

编辑:根据Mozarts的回答,如果您使用的是.NET Core 3+,请参考Microsoft.Data.SqlClient

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在ASP.NET Core中使用Bootstrap 4

如何在ASP.NET Core应用中使用HttpClient

如何在 ASP.Net Core 3 中使用 UseNodeModules?

如何在ASP.NET Core中使用jquery

如何在ASP.NET Core中使用区域

如何在ASP.NET Core中使用npm

如何在 Asp.Net Core 中使用 Ninject?

如何在 ASP.NET Core 中使用 API?

如何在 ASP.NET Core 中使用异步“ForEach”?

如何在asp.net中使用<%%>?

如何在.NET CORE中使用SqlException?

如何在.Net Core中使用ServiceExtensions?

如何在.NET Core中使用IUserIdProvider?

如何在Asp.net Core 2.0中使用log4net

如何在ASP.NET Core WebAPI中使用Newtonsoft:Json.NET?

Asp.net Core 2-如何在Asp.net Core 2.0中使用ServiceLocator

使用 IdentityDbContext 時如何在 ASP.NET Core MVC 中使用存儲庫模式?

我们如何在ASP.Net Core中使用HttpClient?

如何在asp.net.core中使用Ajax发布表单数据和文件?

如何在ASP.NET Core 3.0中使用ValidationAttribute验证多个操作参数

如何在Asp.net Core rc1中的静态方法中使用DI

如何在 ASP.NET Core 中使用 mysql 进行单元测试、actionresult

如何在后台进程中使用Asp .net core上传文件

如何在Rider中使用F#创建ASP.NET Core WebAPI项目?

如何在ASP.NET Core 2.1中使用IDesignTimeDbContextFactory实现?

如何在ASP.NET Core 5中使用FromSqlRaw删除记录?

如何在Visual Studio ASP.NET Core项目中使用语义UI反应?

如何在ASP.NET Core中使用HttpContextAccessor读取URI参数

如何在带有maxAllowedContentLength的IIS上的ASP.NET Core中使用MaxRequestBodySize?