【发布时间】:2016-02-16 22:45:25
【问题描述】:
我正在尝试在 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();
}
}
}
但是得到以下错误:
还有其他人解决这个问题吗?
【问题讨论】:
-
我在您的任何依赖项中都没有看到对 System.Runtime 的引用。你试过添加一个吗?
-
你也没有在你的 sql 中执行
UPDATE, INSERT or DELETE命令,那么为什么你使用command.ExecuteNonQuery();查找使用Fill()方法从数据库中返回数据,或者如果只返回一个 ExecuteScalar 方法单行。您不仅需要添加对using section in the .cs file class header的引用,还需要手动将它们添加到项目中的reference节点 -
您的错误表明您没有为 DNX 4.5.1 添加正确的参考。您正在同时构建两种项目类型。如果您不关心 DNX.4.5.1,请从您的配置中删除它,它应该会构建。
-
伙计们——非常感谢你们!删除了 DNX 4.5.1 部分,并将 System.Runtime 的依赖项添加到设置中,它运行良好(在 Visual Studio 重新启动后一切正常!)。再次感谢!!!
标签: c# asp.net-mvc asp.net-core sqlclient .net-core