将SQL Server存储过程传递给oracle pl / sql存储过程

时间:2015-06-24 19:28:25

标签: sql-server stored-procedures oracle11g

我想在SQL Server中有两个传递给Oracle的过程:

ALTER PROCEDURE [dbo].[usp_GetSettingValueByIndividualKey] 
     @PortalID INT
     ,@SettingKey NVARCHAR(256)
AS
BEGIN
    IF EXISTS (SELECT *
               FROM SettingValue
               WHERE settingKey = @SettingKey
                 AND SettingTypeID = @PortalID)
    BEGIN
        SELECT SettingValue as Value
        FROM SettingValue
        WHERE settingKey = @SettingKey
          AND SettingTypeID = @PortalID
    END
    ELSE
    BEGIN
        SELECT SettingValue as Value
        FROM SettingKey
        WHERE settingKey = @SettingKey
          AND PortalID = 1
    END
END

我从这个C#方法调用该存储过程:

public T ExecuteAsObject<T>(string StroredProcedureName, List<KeyValuePair<string, object>> ParaMeterCollection)
    {
        string orclConString = System.Configuration.ConfigurationManager.ConnectionStrings["orclConnString"].ConnectionString;
        OracleConnection oraConec = new OracleConnection(orclConString);

        try
        {
            //SqlDataReader SQLReader;
            //SqlCommand SQLCmd = new SqlCommand();
            //SQLCmd.Connection = SQLConn;
            //SQLCmd.CommandText = StroredProcedureName;
            //SQLCmd.CommandType = CommandType.StoredProcedure;

            oraConec.Open();
            OracleCommand oraCmd = new OracleCommand(StroredProcedureName, oraConec);
            oraCmd.CommandType = CommandType.StoredProcedure;
            oraCmd.BindByName = true;
            //Loop for Parameters
            for (int i = 0; i < ParaMeterCollection.Count; i++)
            {
                OracleParameter oracleParaMeter = new OracleParameter();
                oracleParaMeter.IsNullable = true;
                oracleParaMeter.ParameterName = ParaMeterCollection[i].Key;
                oracleParaMeter.Value = ParaMeterCollection[i].Value;
                oraCmd.Parameters.Add(oracleParaMeter);
            }
            //End of for loop

            OracleDataReader dr = oraCmd.ExecuteReader();
            dr.Read();

            //SQLConn.Open();
            //SQLReader = SQLCmd.ExecuteReader(CommandBehavior.CloseConnection);
            //ArrayList arrColl = DataSourceHelper.FillCollection(SQLReader, typeof(T));

            List<T> mList = new List<T>();
            ArrayList arrColl = DataSourceHelper.FillCollection(dr, typeof(T));

            oraConec.Close();

            if (dr != null)
            {
                dr.Close();
            }

            if (arrColl != null && arrColl.Count > 0)
            {
                return (T)arrColl[0];
            }
            else
            {
                return default(T);
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            oraConec.Close();
        }
    } 

另一个存储过程是:

ALTER PROCEDURE  [dbo].[sp_PortalGetList]
AS
BEGIN
    SELECT 
       Por.PortalID, [Name],
       LOWER(LTRIM(RTRIM(SEOName))) AS SEOName, 
       IsParent, Por.ParentID,
       (SELECT SEOName 
        FROM Portal AS port 
        WHERE Por.ParentID = port.PortalID) AS ParentPortalName,
       [dbo].[SettingValue].SettingValue AS DefaultPage
    FROM 
       dbo.Portal AS Por
    INNER JOIN 
       [dbo].[SettingValue] ON  Por.PortalID = [dbo].[SettingValue].settingtypeid 
    WHERE 
       SettingType='SiteAdmin' and SettingKey='PortalDefaultPage'
END

我从这个C#方法调用该存储过程:

  public List<T> ExecuteAsList<T>(string StroredProcedureName)
  {
        string orclConString = "connectionstrig";
        OracleConnection oraConec = new OracleConnection(orclConString);
        try
        {

            oraConec.Open();
            OracleCommand oraCmd =
 new OracleCommand(StroredProcedureName,   oraConec);
            oraCmd.CommandType = CommandType.StoredProcedure;
            oraCmd.BindByName = true;

            OracleDataReader dr = oraCmd.ExecuteReader();
            dr.Read();

            //SQLConn.Open();
            //SQLReader = SQLCmd.ExecuteReader(CommandBehavior.CloseConnection); //datareader automatically closes the SQL connection
            List<T> mList = new List<T>();
            mList = DataSourceHelper.FillCollection<T>(dr);

            if (dr != null)
            {
                dr.Close();
            }
            oraConec.Close();
            return mList;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            oraConec.Close();
        }
    }

0 个答案:

没有答案