• <small id='CJGNf'></small><noframes id='CJGNf'>

    <i id='CJGNf'><tr id='CJGNf'><dt id='CJGNf'><q id='CJGNf'><span id='CJGNf'><b id='CJGNf'><form id='CJGNf'><ins id='CJGNf'></ins><ul id='CJGNf'></ul><sub id='CJGNf'></sub></form><legend id='CJGNf'></legend><bdo id='CJGNf'><pre id='CJGNf'><center id='CJGNf'></center></pre></bdo></b><th id='CJGNf'></th></span></q></dt></tr></i><div id='CJGNf'><tfoot id='CJGNf'></tfoot><dl id='CJGNf'><fieldset id='CJGNf'></fieldset></dl></div>

      <legend id='CJGNf'><style id='CJGNf'><dir id='CJGNf'><q id='CJGNf'></q></dir></style></legend>

        • <bdo id='CJGNf'></bdo><ul id='CJGNf'></ul>
      1. <tfoot id='CJGNf'></tfoot>
      2. 如何在不使用实体框架的情况下从 .Net Core 连接到 SQL Server?

        时间:2023-06-25
            <bdo id='cgkiU'></bdo><ul id='cgkiU'></ul>
              <tbody id='cgkiU'></tbody>

            • <i id='cgkiU'><tr id='cgkiU'><dt id='cgkiU'><q id='cgkiU'><span id='cgkiU'><b id='cgkiU'><form id='cgkiU'><ins id='cgkiU'></ins><ul id='cgkiU'></ul><sub id='cgkiU'></sub></form><legend id='cgkiU'></legend><bdo id='cgkiU'><pre id='cgkiU'><center id='cgkiU'></center></pre></bdo></b><th id='cgkiU'></th></span></q></dt></tr></i><div id='cgkiU'><tfoot id='cgkiU'></tfoot><dl id='cgkiU'><fieldset id='cgkiU'></fieldset></dl></div>

                1. <small id='cgkiU'></small><noframes id='cgkiU'>

                  <legend id='cgkiU'><style id='cgkiU'><dir id='cgkiU'><q id='cgkiU'></q></dir></style></legend>

                  <tfoot id='cgkiU'></tfoot>
                  本文介绍了如何在不使用实体框架的情况下从 .Net Core 连接到 SQL Server?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何在不使用实体框架的情况下从 .Net Core 连接到 SQL Server?

                  How can we connect to SQL Server from .Net Core without using Entity Framework?

                  推荐答案

                  如果您对另一个答案中的 BaseDataAccess 类格式感到惊讶,并且参考了与我相同的文章,这里是格式良好的示例...希望它会为您节省一些时间

                  If you surprised with BaseDataAccess class format in another answer and referenced article same as me, here is well formatted example... hopefully it will save you some time

                  public class BaseDataAccess
                  {
                      protected string ConnectionString { get; set; }
                  
                      public BaseDataAccess()
                      {
                      }
                  
                      public BaseDataAccess(string connectionString)
                      {
                          this.ConnectionString = connectionString;
                      }
                  
                      private SqlConnection GetConnection()
                      {
                          SqlConnection connection = new SqlConnection(this.ConnectionString);
                          if (connection.State != ConnectionState.Open)
                              connection.Open();
                          return connection;
                      }
                  
                      protected DbCommand GetCommand(DbConnection connection, string commandText, CommandType commandType)
                      {
                          SqlCommand command = new SqlCommand(commandText, connection as SqlConnection);
                          command.CommandType = commandType;
                          return command;
                      }
                  
                      protected SqlParameter GetParameter(string parameter, object value)
                      {
                          SqlParameter parameterObject = new SqlParameter(parameter, value != null ? value : DBNull.Value);
                          parameterObject.Direction = ParameterDirection.Input;
                          return parameterObject;
                      }
                  
                      protected SqlParameter GetParameterOut(string parameter, SqlDbType type, object value = null, ParameterDirection parameterDirection = ParameterDirection.InputOutput)
                      {
                          SqlParameter parameterObject = new SqlParameter(parameter, type); ;
                  
                          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;
                      }
                  
                      protected int ExecuteNonQuery(string procedureName, List<DbParameter> parameters, CommandType commandType = CommandType.StoredProcedure)
                      {
                          int returnValue = -1;
                  
                          try
                          {
                              using (SqlConnection connection = this.GetConnection())
                              {
                                  DbCommand cmd = this.GetCommand(connection, procedureName, commandType);
                  
                                  if (parameters != null && parameters.Count > 0)
                                  {
                                      cmd.Parameters.AddRange(parameters.ToArray());
                                  }
                  
                                  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
                          {
                              using (DbConnection connection = this.GetConnection())
                              {
                                  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;
                      }
                  
                      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());
                                  }
                  
                                  ds = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                              }
                          }
                          catch (Exception ex)
                          {
                              //LogException("Failed to GetDataReader for " + procedureName, ex, parameters);
                              throw;
                          }
                  
                          return ds;
                      }
                  }
                  

                  这篇关于如何在不使用实体框架的情况下从 .Net Core 连接到 SQL Server?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:.net core 2.1 MVC for MySQL 数据库中的瞬时故障处理 下一篇:如何将 SQL 输出参数与 FromSqlInterpolated 一起使用?或替代

                  相关文章

                    • <bdo id='Z9lH0'></bdo><ul id='Z9lH0'></ul>
                  1. <small id='Z9lH0'></small><noframes id='Z9lH0'>

                    <legend id='Z9lH0'><style id='Z9lH0'><dir id='Z9lH0'><q id='Z9lH0'></q></dir></style></legend><tfoot id='Z9lH0'></tfoot>
                    1. <i id='Z9lH0'><tr id='Z9lH0'><dt id='Z9lH0'><q id='Z9lH0'><span id='Z9lH0'><b id='Z9lH0'><form id='Z9lH0'><ins id='Z9lH0'></ins><ul id='Z9lH0'></ul><sub id='Z9lH0'></sub></form><legend id='Z9lH0'></legend><bdo id='Z9lH0'><pre id='Z9lH0'><center id='Z9lH0'></center></pre></bdo></b><th id='Z9lH0'></th></span></q></dt></tr></i><div id='Z9lH0'><tfoot id='Z9lH0'></tfoot><dl id='Z9lH0'><fieldset id='Z9lH0'></fieldset></dl></div>