如何在不使用Entity Framework的情况下从.Net Core连接到SQL Server?如何在不使用Entity Framework的情况下从.Net Core连接到SQL Server?
回答
,你可以简单地使用它们使用SqlConnection
这里有一个例子传统的方式
public class BaseDataAccess
{
protected string ConnectionString { get; set; }
public BaseDataAccess()
{
}
{
public BaseDataAccess(string connectionString)
private SqlConnection GetConnection()
this.ConnectionString = connectionString;
}
{
if (connection.State != ConnectionState.Open)
SqlConnection connection = new SqlConnection(this.ConnectionString);
connection.Open();
return connection;
SqlCommand command = new SqlCommand(commandText, connection as SqlConnection);
}
protected DbCommand GetCommand(DbConnection connection, string commandText, CommandType commandType)
{
protected SqlParameter GetParameter(string parameter, object value)
command.CommandType = commandType;
return command;
}
{
parameterObject.Direction = ParameterDirection.Input;
SqlParameter parameterObject = new SqlParameter(parameter, value != null ? value : DBNull.Value);
return parameterObject;
}
SqlParameter parameterObject = new SqlParameter(parameter, type); ;
protected SqlParameter GetParameterOut(string parameter, SqlDbType type, object value = null, ParameterDirection parameterDirection = ParameterDirection.InputOutput)
{
if (type == SqlDbType.NVarChar || type == SqlDbType.VarChar || type == SqlDbType.NText || type == SqlDbType.Text)
{
}
parameterObject.Size = -1;
}
parameterObject.Direction = parameterDirection;
if (value != null)
{
parameterObject.Value = value;
}
else
{
parameterObject.Value = DBNull.Value;
}
return parameterObject;
DbCommand cmd = this.GetCommand(connection, procedureName, commandType);
protected int ExecuteNonQuery(string procedureName, List<DbParameter> parameters, CommandType commandType = CommandType.StoredProcedure)
{
int returnValue = -1;
try
{
using (SqlConnection connection = this.GetConnection())
{
if (parameters != null && parameters.Count > 0)
{
cmd.Parameters.AddRange(parameters.ToArray());
}
using (DbConnection connection = this.GetConnection())
returnValue = cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
//LogException("Failed to ExecuteNonQuery for " + procedureName, ex, parameters);
throw;
}
return returnValue;
}
protected object ExecuteScalar(string procedureName, List<SqlParameter> parameters)
{
object returnValue = null;
try
{
{
}
DbCommand cmd = this.GetCommand(connection, procedureName, CommandType.StoredProcedure);
if (parameters != null && parameters.Count > 0)
{
cmd.Parameters.AddRange(parameters.ToArray());
}
returnValue = cmd.ExecuteScalar();
}
}
catch (Exception ex)
{
//LogException("Failed to ExecuteScalar for " + procedureName, ex, parameters);
throw;
return returnValue;
}
ds = cmd.ExecuteReader(CommandBehavior.CloseConnection);
protected DbDataReader GetDataReader(string procedureName, List<DbParameter> parameters, CommandType commandType = CommandType.StoredProcedure)
{
DbDataReader ds;
try
{
DbConnection connection = this.GetConnection();
{
DbCommand cmd = this.GetCommand(connection, procedureName, commandType);
if (parameters != null && parameters.Count > 0)
{
cmd.Parameters.AddRange(parameters.ToArray());
}
}
}
catch (Exception ex)
{
}
//LogException("Failed to GetDataReader for " + procedureName, ex, parameters);
throw;
}
return ds;
}
更可以找到here
更新
你必须添加的NuGet包
Install-Package System.Data.SqlClient
that is still confusing for me... .Net Core & .Net standard vs regular .Net: How do we know which packages we can use with .Net core?
依赖意味着什么,你应该以使用软件包或将的NuGet安装它为你 了解更多的依赖关系是如何工作的。NET看看已安装在机器上here
注意 如果NuGet包目标.net standard
库大多都.NET的核心和.NET标准框架的工作
可以从对.NET核心引用标准.NET Framework类?没有这种战胜.Net Core的观点? –
引用链接指向我们命名为“Microsoft.EntityFrameworkCore.SqlServer” –
你必须添加NuGet包 –
- 1. 如何在不使用App.config的情况下使用Entity Framework
- 2. 如何在没有配置文件的情况下使用Oracle Entity Framework
- 3. ASP .Net Core - Entity Framework 7
- 4. 如何在没有Nuget的情况下获取Entity Framework 4.2?
- 5. 如何在不使用查询语法的情况下在Entity Framework中执行左外连接?
- 6. 如何在不连接到AddressBook的情况下使用ABPersonViewController
- 7. 如何从.Net Core连接到MySQL
- 8. 如何在我的情况下使用SQL Server连接字符串?
- 9. SQL Server:在没有SSMS的情况下启用远程连接
- 10. .NET 4.6.1上的Entity Framework Core 2.0
- 11. 如何在不使用sudo的情况下在Ubuntu上安装.Net Core?
- 12. 有关使用JDBC连接到SQL Server的奇怪情况
- 13. 在一种情况下无法使用SSMS远程连接到SQL Server 2014
- 14. 如何在不使用INNER JOIN的情况下连接表?
- 15. 如何使用Visual Studio 2010 Express连接到SqlExpress for Entity Framework?
- 16. 将现有的.NET Entity Framework图层从Sql Server更改为MySql
- 17. .Net Core - Entity Framework - MySQL> UseMysql not found
- 18. Entity Framework的SQL Server Express连接字符串代码优先
- 19. 如何在不安装Zend Framework的情况下使用Zend库
- 20. 如何在不使用EF的情况下连接到ASP.NET Core Web API中的数据库?
- 21. 如何使用if语句的情况下在SQL Server一起
- 22. 如何在使用SQL Server的情况下进行分组?
- 23. 如何在不使用AppDomains的情况下实现.net插件?
- 24. 如何从.NET应用程序连接到SQL Server Compact?
- 25. 从Entity Framework上下文获取SQL Server Compact文件的路径?
- 26. C#:如何在启用SSL的情况下连接到Active Directory?
- 27. 如何使用Perl连接到SQL Server?
- 28. Fluent Api in Entity Framework 6不兼容Entity Framework Core
- 29. 在SQL Server中使用的情况下,当不想空
- 30. 如何使用Emacs连接到SQL Server?
你已经尝试到目前为止 –
@BRAHIMKamel我是新来的.NET的核心,到目前为止,所有的搜索拿出**有** EF –