zoukankan      html  css  js  c++  java
  • C# 调用SQL的存储过程的接口及实现

    1. 接口为ExecuteStoredProcedure(string storedProcedureName, params ObjectParameter[] parameters)
    2. 参数为存储过程名字, 及输入值。
    
    3. 思路:创建连接(连接中指定了是Sql/MySql/ODBC等等); 创建通用DbCommand;更改Text以及Type;添加通用Parameter(DBParameter是抽象类,因此需要判断connection类型);判断连接状态(需Open); 调用Execute方法; 关闭连接。
    
    4.
    
    实现如下:
    

    // In V1 of the EF, the context connection is always an EntityConnection
                    EntityConnection entityConnection = (EntityConnection)protocolDB.Connection;
     
                    // The EntityConnection exposes the underlying store connection
                    DbConnection storeConnection = entityConnection.StoreConnection;
                    DbCommand command = storeConnection.CreateCommand();
                    command.CommandText = storedProcedureName;
                    command.CommandType = CommandType.StoredProcedure;
     
                    if (storeConnection is SqlConnection)
                    {
                        foreach (ObjectParameter p in parameters)
                        {
                            command.Parameters.Add(new SqlParameter { ParameterName = p.Name, Value = p.Value });
                        }
                    }
                    else if (storeConnection is MySqlConnection)
                    {
                        foreach (ObjectParameter p in parameters)
                        {
                            command.Parameters.Add(new MySqlParameter { ParameterName = p.Name, Value = p.Value });
                        }
                    }
                    else
                    {
                        return enProtocolDBReturnCodes.OPERATION_FAILED;
                    }
     
                    bool openingConnection = command.Connection.State == ConnectionState.Closed;
                    if (openingConnection)
                    {
                        command.Connection.Open();
                    }
     
                    command.ExecuteNonQuery();
     
                    if (openingConnection && command.Connection.State == ConnectionState.Open)
                    { 
                        command.Connection.Close();
                    }
    
  • 相关阅读:
    Redis缓存穿透和雪崩
    Redis主从复制
    Redis发布订阅
    IO多路复用
    Synchronized解读
    日志导致jvm内存溢出相关问题
    tomcat及springboot实现Filter、Servlet、Listener
    MySQL主从复制针对trigger的特殊处理
    二、变量/常量/数据类型
    Ubuntu21.04 / Linux Mint20.2 安装 TradingView分析软件
  • 原文地址:https://www.cnblogs.com/muzizongheng/p/3169809.html
Copyright © 2011-2022 走看看