如何在不使用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. 如何在不使用Entity Framework的情況下使用ASP NET MVC5從SQL Server顯示錶?
- 3. 如何在沒有配置文件的情況下使用Oracle Entity Framework
- 4. ASP .Net Core - Entity Framework 7
- 5. SQL Server:如何在不使用點符號的情況下執行半連接?
- 6. 如何在沒有Nuget的情況下獲取Entity Framework 4.2?
- 7. 如何在不使用查詢語法的情況下在Entity Framework中執行左外連接?
- 8. 如何從.Net Core連接到MySQL
- 9. 使用連接的情況下sql
- 10. Ksh:如何在不使用tnsnames.ora的情況下連接到sqlplus
- 11. 如何在不連接到AddressBook的情況下使用ABPersonViewController
- 12. 如何在我的情況下使用SQL Server連接字符串?
- 13. .NET 4.6.1上的Entity Framework Core 2.0
- 14. SQL Server:在沒有SSMS的情況下啓用遠程連接
- 15. 如何讓Entity Framework不連接表?
- 16. 如何在不使用sudo的情況下在Ubuntu上安裝.Net Core?
- 17. 有關使用JDBC連接到SQL Server的奇怪情況
- 18. .Net Core - Entity Framework - MySQL> UseMysql not found
- 19. 在一種情況下無法使用SSMS遠程連接到SQL Server 2014
- 20. 如何在不使用LINQ to SQL和Entity Framework過度調用數據庫的情況下優化此查詢?
- 21. 將現有的.NET Entity Framework圖層從Sql Server更改爲MySql
- 22. 如何使用Visual Studio 2010 Express連接到SqlExpress for Entity Framework?
- 23. 如何在不使用INNER JOIN的情況下連接表?
- 24. 如何使用的情況下用select語句在SQL Server 2000
- 25. 在我的情況下如何在SQL Server中使用NOT EXISTS?
- 26. Entity Framework的SQL Server Express連接字符串代碼優先
- 27. 默認情況下,Entity Framework在哪裏保存數據?
- 28. 如何使用Entity Framework 4組織連接到不同的數據庫?
- 29. 如何在不安裝Zend Framework的情況下使用Zend庫
- 30. 在ASP.NET Core 1.0中使用Entity Framework 7腳手架SQL Server視圖?
你已經嘗試到目前爲止 –
@BRAHIMKamel我是新來的.NET的核心,到目前爲止,所有的搜索拿出**有** EF –