熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

C#如何執行存儲過程

2022-06-13   來源: ASP編程 

  以Oracle為例其它數據庫僅改變數據庫對象就可以下面是帶參數的不帶參數的話paramete為null即可

  參數對應

  view plainprint?
 public static int UploadStartCloseSoftInfo(string[] args)
   {
        OracleParameter parameter = new OracleParameter(參數名 OracleType類型);
        parameterValue =類型轉換(args[]);

  OracleParameter parameter = new OracleParameter(參數名 OracleType類型);
        parameterValue =類型轉換(args[]);
               
        OracleParameter parametern = new OracleParameter(參數名n OracleType類型);
        parameternValue =類型轉換(args[n]);
        OracleParameter[] parameter
                         = {parameterparameter parametern};

  return SDProviderRunProcedure(存儲過程名 parameter);
        }
    }
}
執行

  view plainprint?
public class SDProvider
{
       public static string GetConnectionString()
       {
           string conStr =連接數據庫字符串;
           return conStr ;
       }

  public static int RunProcedure(string  prName IDataParameter[] parameters)
       {
           try
           {
               using(OracleConnection connection=
               new OracleConnection(GetConnectionString()))
                {
                    connectionOpen();
                    OracleCommand command =
                    BuildQueryCommand(connection storedProcName parameters);

  return  commandExecuteNonQuery();
               }
           }
           catch
           {
           }
           return ;
       }
        private static OracleCommand BuildQueryCommand
                                     (OracleConnection connection
                                     string storedProcName
                                     IDataParameter[]parameters)
        {
          OracleCommand command = new OracleCommand(storedProcName connection);
          commandCommandType = CommandTypeStoredProcedure;
          if(parameters!=null)
          {
             foreach (OracleParameter parameter in parameters)
             {
               commandParametersAdd(parameter);
             }
          }
            return command;
        }
}
注意

  存儲過程名正確

  參數名參參數名這些的名字要和存儲過程中的參數名一樣順序和類型均要對應一致


From:http://tw.wingwit.com/Article/program/ASP/201311/21743.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.