C# Asp.NETCore2.0应用程序与ADO.Net示例
我想用C# Asp.NETCore2.0应用程序与ADO.Net示例,c#,asp.net-mvc,ado.net,asp.net-core-2.0,C#,Asp.net Mvc,Ado.net,Asp.net Core 2.0,我想用ADO.net创建asp.net core 2.0 MVC应用程序,我搜索了Google/Microsoft,在那里找到了实体框架的示例,但没有ADO.net的示例 我需要 从json或配置文件中从配置文件读取的连接字符串(我知道默认情况下没有Web.config,但需要从配置文件访问配置条目) 为3层架构创建DAL。(我知道asp.net核心不支持ADO.net,但)我的要求是使用Sqlconnection,Sqlcommand与ADO.net中的普通MVC应用程序相同 任何人都可以举一
ADO.net
创建asp.net core 2.0 MVC
应用程序,我搜索了Google/Microsoft,在那里找到了实体框架的示例,但没有ADO.net的示例
我需要
任何人都可以举一个例子来说明这一点,或者完全理解其中的链接吗?我认为这将有助于每一个了解Asp.NETMVC,但没有Asp.NETCore经验的人 如果在.NET标准2.0类库中定义DAL,则可以同时使用
SqlConnection
和SqlCommand
,例如:
public class MyDal
{
private readonly string _connectionString;
public MyDal(string connectionString)
{
_connectionString = connectionString;
}
public IEnumerable<string> GetSomeData()
{
using (SqlConnection conn = new SqlConnection())
{
using (SqlCommand command = new SqlCommand(_connectionString, conn))
{
...
}
}
}
}
…您使用它来提取连接字符串:
public class ValuesController : Controller
{
private readonly IConfiguration _config;
public ValuesController(IConfiguration config)
{
_config = config;
}
[HttpGet]
public IEnumerable<string> Get()
{
MyDal dal = new MyDal(_config.GetConnectionString("conn"));
return dal.GetSomeData();
}
}
我得到了一个关于如何使用ConfigJSON中的连接字符串的答案。假设我有
appsetting.json
,如下所示
{
"connectionString": "data source=servername;initial catalog=yourdb;user id=sa;password=pwd;MultipleActiveResultSets=True;"
}
现在我想访问这个连接字符串,所以代码将是
using Microsoft.Extensions.Configuration;
using System.IO;
protected string ConnectionString { get; set; }
public GetDBConnString()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory() + "/").AddJsonFile("config.json", false)
.Build();
this.ConnectionString = configuration.GetSection("connectionString").Value;
}
我想出了下面的解决办法。也许它对你有用 在.NETCore中,默认情况下不存在SqlClient。您需要从NuGet Package Manager添加它。因此,请遵循以下步骤来实现这一点
{
"name": "asp.net",
"private": true,
"dependencies": {
},
"connectionString": "data source=*************;initial catalog=****;user id=***;password=****;MultipleActiveResultSets=True;Connection Timeout=300;"
}
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
namespace MyProject.DAL
{
public class SQLDataAccess
{
protected string ConnectionString { get; set; }
public SQLDataAccess()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetParent(Directory.GetCurrentDirectory()) + "/MyProject.DAL").AddJsonFile("config.json", false)
.Build();
this.ConnectionString = configuration.GetSection("connectionString").Value;
}
private SqlConnection GetConnection()
{
SqlConnection connection = new SqlConnection(this.ConnectionString);
if (connection.State != ConnectionState.Open)
connection.Open();
return connection;
}
public DbDataReader GetDataReader(string procedureName, List<SqlParameter> parameters, CommandType commandType = CommandType.StoredProcedure)
{
DbDataReader dr;
try
{
DbConnection connection = this.GetConnection();
{
DbCommand cmd = this.GetCommand(connection, procedureName, commandType);
if (parameters != null && parameters.Count > 0)
{
cmd.Parameters.AddRange(parameters.ToArray());
}
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
}
catch (Exception ex)
{
throw;
}
return dr;
}
}
}
使用系统数据;
使用System.Data.Common;
使用System.Data.SqlClient;
名称空间MyProject.DAL
{
公共类SQLDataAccess
{
受保护的字符串连接字符串{get;set;}
公共SQLDataAccess()
{
var configuration=new ConfigurationBuilder()
.SetBasePath(Directory.GetParent(Directory.GetCurrentDirectory())+“/MyProject.DAL”).AddJsonFile(“config.json”,false)
.Build();
this.ConnectionString=configuration.GetSection(“ConnectionString”).Value;
}
私有SqlConnection GetConnection()
{
SqlConnection=newsqlconnection(this.ConnectionString);
if(connection.State!=ConnectionState.Open)
connection.Open();
回路连接;
}
公共DbDataReader GetDataReader(字符串过程重命名,列表参数,CommandType CommandType=CommandType.StoredProcess)
{
DbDataReader-dr;
尝试
{
DbConnection=this.GetConnection();
{
DbCommand cmd=this.GetCommand(连接、过程重命名、命令类型);
if(parameters!=null&¶meters.Count>0)
{
cmd.Parameters.AddRange(Parameters.ToArray());
}
dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
}
捕获(例外情况除外)
{
投掷;
}
返回dr;
}
}
}
你读过这个吗@mm8,是的,实体库中的c sharp代码,与我在asp.net内核的ado端中所需的代码相同。谢谢你的帮助,我举了个例子。使用.NET标准,DAL本身可以在ASP.NET和ASP.NET Core应用程序之间轻松共享。是的,检查并创建ASP.NET Core 2及以上版本的appBest answer。
{
"name": "asp.net",
"private": true,
"dependencies": {
},
"connectionString": "data source=*************;initial catalog=****;user id=***;password=****;MultipleActiveResultSets=True;Connection Timeout=300;"
}
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
namespace MyProject.DAL
{
public class SQLDataAccess
{
protected string ConnectionString { get; set; }
public SQLDataAccess()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetParent(Directory.GetCurrentDirectory()) + "/MyProject.DAL").AddJsonFile("config.json", false)
.Build();
this.ConnectionString = configuration.GetSection("connectionString").Value;
}
private SqlConnection GetConnection()
{
SqlConnection connection = new SqlConnection(this.ConnectionString);
if (connection.State != ConnectionState.Open)
connection.Open();
return connection;
}
public DbDataReader GetDataReader(string procedureName, List<SqlParameter> parameters, CommandType commandType = CommandType.StoredProcedure)
{
DbDataReader dr;
try
{
DbConnection connection = this.GetConnection();
{
DbCommand cmd = this.GetCommand(connection, procedureName, commandType);
if (parameters != null && parameters.Count > 0)
{
cmd.Parameters.AddRange(parameters.ToArray());
}
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
}
catch (Exception ex)
{
throw;
}
return dr;
}
}
}