CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

有用DataAccessApplicationBlock.msi(Microsoft DataAcess Application Block for .Net)做数据访问的没啊??

楼主wokagoka(碧海)2005-03-04 08:52:48 在 .NET技术 / C# 提问

用ExecuteNonQuery执行带参数的存储过程的时候怎么返回OUTPUT的返回值啊?偶用这个:  
  public   static   int   ExecuteNonQuery(string   connectionString,   string   spName,   params   object[]   parameterValues)  
  {  
  if(   connectionString   ==   null   ||   connectionString.Length   ==   0   )   throw   new   ArgumentNullException(   "connectionString"   );  
  if(   spName   ==   null   ||   spName.Length   ==   0   )   throw   new   ArgumentNullException(   "spName"   );  
   
  //   If   we   receive   parameter   values,   we   need   to   figure   out   where   they   go  
  if   ((parameterValues   !=   null)   &&   (parameterValues.Length   >   0))    
  {  
  //   Pull   the   parameters   for   this   stored   procedure   from   the   parameter   cache   (or   discover   them   &   populate   the   cache)  
  OracleParameter[]   commandParameters   =   ParamsCache.GetSpParameterSet(connectionString,   spName);  
   
  //   Assign   the   provided   values   to   these   parameters   based   on   parameter   order  
  AssignParameterValues(commandParameters,   parameterValues);  
   
  //   Call   the   overload   that   takes   an   array   of   OracleParameters  
  return   ExecuteNonQuery(connectionString,   CommandType.StoredProcedure,   spName,   commandParameters);  
  }  
  else    
  {  
  //   Otherwise   we   can   just   call   the   SP   without   params  
  return   ExecuteNonQuery(connectionString,   CommandType.StoredProcedure,   spName);  
  }  
  } 问题点数:50、回复次数:13Top

1 楼jackie615()回复于 2005-03-04 08:55:08 得分 0

upTop

2 楼wokagoka(碧海)回复于 2005-03-04 09:04:01 得分 0

public   static   int   ExecuteNonQuery(OracleConnection   connection,   CommandType   commandType,   string   commandText,   params   OracleParameter[]   commandParameters)  
  {  
  if(   connection   ==   null   )   throw   new   ArgumentNullException(   "connection"   );  
   
  //   Create   a   command   and   prepare   it   for   execution  
  OracleCommand   cmd   =   new   OracleCommand();  
  bool   mustCloseConnection   =   false;  
  PrepareCommand(cmd,   connection,   (OracleTransaction)null,   commandType,   commandText,   commandParameters,   out   mustCloseConnection   );  
           
  //   Finally,   execute   the   command  
   
  int   retval   =   cmd.ExecuteNonQuery();  
   
  //   Detach   the   OracleParameters   from   the   command   object,   so   they   can   be   used   again  
  cmd.Parameters.Clear();  
  if(   mustCloseConnection   )  
  connection.Close();  
  return   retval;  
  }  
  Top

3 楼seesea125(执著)回复于 2005-03-04 09:13:10 得分 50

你看看它自带的quickstart例子,引用一下  
  private   void   cmdSample3_Click(object   sender,   System.EventArgs   e)  
  {  
  //   SqlConnection   that   will   be   used   to   execute   the   sql   commands  
  SqlConnection   connection   =   null;  
   
  try  
  {  
  try  
  {  
  connection   =   GetConnection(txtConnectionString.Text);  
  }  
  catch  
  {  
  MessageBox.Show("The   connection   with   the   database   can磘   be   established",   "Application   Error",   MessageBoxButtons.OK,   MessageBoxIcon.Error);  
  return;  
  }  
   
  //   Set   up   parameters   (1   input   and   3   output)    
  SqlParameter   []   arParms   =   new   SqlParameter[4];  
   
  //   @ProductID   Input   Parameter    
  //   assign   value   =   1  
  arParms[0]   =   new   SqlParameter("@ProductID",   SqlDbType.Int   );    
  arParms[0].Value   =   1;  
   
  //   @ProductName   Output   Parameter  
  arParms[1]   =   new   SqlParameter("@ProductName",   SqlDbType.NVarChar,   40);  
  arParms[1].Direction   =   ParameterDirection.Output;  
   
  //   @UnitPrice   Output   Parameter  
  arParms[2]   =   new   SqlParameter("@UnitPrice",   SqlDbType.Money);  
  arParms[2].Direction   =   ParameterDirection.Output;  
   
  //   @QtyPerUnit   Output   Parameter  
  arParms[3]   =   new   SqlParameter("@QtyPerUnit",   SqlDbType.NVarChar,   20);  
  arParms[3].Direction   =   ParameterDirection.Output;  
   
  //   Call   ExecuteNonQuery   static   method   of   SqlHelper   class  
  //   We   pass   in   database   connection   string,   command   type,   stored   procedure   name   and   an   array   of   SqlParameter   objects  
  SqlHelper.ExecuteNonQuery(connection,   CommandType.StoredProcedure,   "getProductDetails",   arParms);  
   
  //   Display   results   in   text   box   using   the   values   of   output   parameters  
  txtResults.Text   =   arParms[1].Value     +   ",   "   +   arParms[2].Value     +   ",   "   +   arParms[3].Value;  
  }  
  catch(Exception   ex)  
  {  
  string   errMessage   =   "";  
  for(   Exception   tempException   =   ex;   tempException   !=   null   ;   tempException   =   tempException.InnerException   )  
  {  
  errMessage   +=   tempException.Message   +   Environment.NewLine   +   Environment.NewLine;  
  }  
   
  MessageBox.Show(   string.Format(   "There   are   some   problems   while   trying   to   use   the   Data   Access   Application   block,   please   check   the   following   error   messages:   {0}"  
  +   Environment.NewLine   +   "This   test   requires   some   modifications   to   the   Northwind   database.   Please   make   sure   the   database   has   been   initialized   using   the   SetUpDataBase.bat   database   script,   or   from   the   Install   Quickstart   option   on   the   Start   menu.",   errMessage   ),    
  "Application   error",   MessageBoxButtons.OK,   MessageBoxIcon.Error   );  
  }  
  finally  
  {  
  if(connection   !=   null)  
  connection.Dispose();  
  }  
  }  
  Top

4 楼amendajing(学习,是个漫长的旅途!)回复于 2005-03-04 09:24:24 得分 0

markTop

5 楼wokagoka(碧海)回复于 2005-03-04 10:14:04 得分 0

这段应该是偶要的  
  SqlParameter   objects  
  SqlHelper.ExecuteNonQuery(connection,   CommandType.StoredProcedure,   "getProductDetails",   arParms);  
   
  //   Display   results   in   text   box   using   the   values   of   output   parameters  
  txtResults.Text   =   arParms[1].Value   +   ",   "   +   arParms[2].Value   +   ",   "   +   arParms[3].Value;  
  不过请seesea125(一一:抢分惊动了党)老大解释一下,偶编译失败……Top

6 楼qzj(SoldierQ)回复于 2005-03-04 10:31:25 得分 0

既然叫   ExecuteNonQuery   當然不會有返回值。  
  要有返回值使用   ExecuteScalar   方法。Top

7 楼wokagoka(碧海)回复于 2005-03-04 10:38:00 得分 0

+ $exception {"未将对象引用设置到对象的实例。"   } System.NullReferenceException  
  Top

8 楼wokagoka(碧海)回复于 2005-03-04 10:40:07 得分 0

to   qzj(SoldierQ):偶返回的是存储过程的参数!!用ExecuteNonQuery   可以Top

9 楼wokagoka(碧海)回复于 2005-03-04 10:52:08 得分 0

这个方法对Oracle的返回值问题是不是有些问题!!??Top

10 楼brusehht(海豚宝宝)回复于 2005-03-04 11:24:59 得分 0

现在新的EDRA把所有的Application   Block都整合了,QuickStar中的例子都是WinForm的,我不知道怎么样把它们移植到WebForm上来,也就是怎么样把它原先用的Configuration模块设置的ConnectionString等数据转换成引用Web.Config文件里的信息,因为我在现在的DataApplicationBlock中没有找到可以带ConnectionString参数的函数,不知道有没有人知道Top

11 楼Runningboy007(菜鸟)回复于 2005-04-12 09:52:05 得分 0

我也遇到过这样的问题  
  SqlParameter[]   paras={  
    new   SqlParameter("@id",ID.Text.Trim()),  
    new   SqlParameter("@name",SqlDbType.Char,10)//存储过程的返回参数  
                        };  
  paras[1].Direction=ParameterDirection.Output;  
  SqlHelper.ExecuteNonQuery(myconn,CommandType.StoredProcedure   ,"search",paras);  
  //之前我没有加CommandType.StoredProcedure   就不能返回值,加了以后就可以了。  
  Pwd.Text=paras[1].Value.ToString();Top

12 楼shiro(比卡丘)回复于 2005-06-30 20:16:55 得分 0

 
  string   StoredProcName   =   HaooData.proc_Article_Process;  
   
  SqlParameter[]   commandParameters =   new   SqlParameter[40];  
   
  commandParameters[0] =   new   SqlParameter("@sqltype",   Art.SqlType   );  
  commandParameters[1] =   new   SqlParameter("@title",Art.Title);  
  commandParameters[2] =   new   SqlParameter("@url",Art.Url);  
  commandParameters[3] =   new   SqlParameter("@from",Art.From);  
  commandParameters[4] =   new   SqlParameter("@author",Art.Author);  
  commandParameters[5] =   new   SqlParameter("@abs",Art.Abs);  
   
  {  
   
  SqlDataReader   reader   =   SqlDataProvider.Get_Reader(   StoredProcName,   commandParameters   );  
   
  .....................  
   
   
  public   static   int   Get_NonQuery(   string   Conn,   string   StoredProcName,   SqlParameter[]   commandParameters   )  
  {  
   
  int   Result   =   -1;  
   
  try  
  {  
  Result   =   SqlHelper.ExecuteNonQuery(      
  Conn  
  ,   CommandType.StoredProcedure  
  ,   StoredProcName  
  ,   commandParameters  
  );  
  }  
  catch(   Exception   ex   )  
  {  
  Result   =   -1;  
                                                                            throw   new   Exception("查询出错:"   +   ex.Message);  
   
  }  
   
  return   Result;  
  }  
   
   
  ..........Top

13 楼shiro(比卡丘)回复于 2005-06-30 20:18:18 得分 0

sorry   ,reader的是这个,哈  
   
   
  #region   返回   SqlDataReader   实例   Get_Reader()   田  
  ///   <summary>  
  ///   返回   SqlDataReader   Get_Reader()  
  ///   </summary>  
  ///   <param   name="StoredProcName"></param>  
  ///   <param   name="commandParameters"></param>  
  ///   <returns></returns>  
  public   static   SqlDataReader   Get_Reader(   string   StoredProcName,   SqlParameter[]   commandParameters   )  
  {  
  return   Get_Reader(   Functions.GetConfig(),   StoredProcName,   commandParameters   );  
  }  
  public   static   SqlDataReader   Get_Reader(   string   Conn,   string   StoredProcName,   SqlParameter[]   commandParameters   )  
  {  
  SqlDataReader   reader   =   null;  
   
  try  
  {  
  reader   =   SqlHelper.ExecuteReader(      
  Conn  
  ,   CommandType.StoredProcedure  
  ,   StoredProcName  
  ,   commandParameters  
  );  
   
  }  
  catch(   Exception   ex   )  
  {  
  reader   =   null;  
  throw   new   Exception("查询出错:"+ex.Message);  
  }  
   
  return   reader;  
   
  }  
  #endregionTop

相关问题

  • 数据访问问题
  • 多数据访问问题
  • MFC ODBC 数据访问问题
  • 数据访问技术--值得看看
  • 数据访问控件讨论
  • 数据访问菜鸟问题
  • 远程数据访问问题
  • 不同网段间的数据访问
  • 关于asp数据访问问题
  • 如何设计数据访问层?

关键词

  • 存储过程
  • null
  • commandparameters
  • storedprocname
  • sqlparameter
  • reader
  • executenonquery
  • 返回值
  • connectionstring
  • sqldatareader

得分解答快速导航

  • 帖主:wokagoka
  • seesea125

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo