我想在ASP.net核心中使用SQLClient库,但好像无法使其正常工作。我在网上找到了一篇文章,介绍了如何设置它,但对我不起作用:http://blog.developers.ba/using-classic-ado-net-in-asp-net-vnext/。
我有一个简单的控制台应用程序包。我的project.json看起来像这样:
我有一个简单的控制台应用程序包。我的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();
}
}
}
但是出现了以下错误:
还有其他人成功实现了吗?
UPDATE,INSERT或DELETE
命令,那么为什么要使用command.ExecuteNonQuery();
?请查阅使用Fill()
方法从数据库返回数据或使用ExecuteScalar方法如果只返回单行。你还需要添加引用,不仅在.cs文件类头
的using
部分中添加,还需要手动将它们添加到项目的reference
节点中。 - MethodMan