ITPub博客

首页 > 数据库 > 数据库开发技术 > 因存储过程参数类型不匹配而造成OleDbCommand的不可用(二) (转)

因存储过程参数类型不匹配而造成OleDbCommand的不可用(二) (转)

原创 数据库开发技术 作者:amyz 时间:2007-08-14 18:49:33 0 删除 编辑
因存储过程参数类型不匹配而造成OleDbCommand的不可用(二) (转)[@more@]

(续 .NET/develop/read_article.ASP?id=18426">因存储过程参数类型不匹配而造成OledbCommand的不可用一)

 

表格二,断点二处的内存状况

-XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

comm

{System.Data.OleDb.OleDbCommand}

System.Data.OleDb.OleDbCommand

transaction

null

System.Data.OleDb.OleDbTransaction

cmdText

Test

string

cmdType

StoredProcedure

System.Data.CommandType

updatedRowsource

Both

System.Data.UpdateRowSource

commandTimeout

30

int

+

icommandText

{System.__Comobject}

System.Data.Common.UnsafeNativeMethods.ICommandText

handle_Accessor

1

int

commandBehavior

Default

System.Data.CommandBehavior

+

dbBindings

{System.Data.OleDb.DBBindings}

System.Data.OleDb.DBBindings

canceling

FALSE

bool

isPrepared

FALSE

bool

executeQuery

FALSE

bool

computedParameters

FALSE

bool

designTimeVisible

FALSE

bool

cmdState

0

int

recordsAffected

-1

int

CommandText

Test

string

CommandTimeout

30

int

CommandType

StoredProcedure

System.Data.CommandType

DesignTimeVisible

TRUE

bool

IsClosed

TRUE

bool

Transaction

null

System.Data.OleDb.OleDbTransaction

UpdatedRowSource

Both

System.Data.UpdateRowSource

由上面表格一和表格二可发现,在OleDbCommand第一次执行ExecuteNoQuery()时,将改变其内部icommandText,dbBindings,handle_Accessor等3个私有属性。并且经过观察发现,只有CommandText被赋予与原来不同的值时才把以上3个私有属性的值恢复到原来的默认值(即icommandText=null,dbBindings=null,handle_Accessor=0)。

这时我们把上面的代码稍作修改,在第一个try块里给原本为存储过程参数Age赋与一个不能转换为整数值的字符串:

using System;

using System.Data;

using System.Data.OleDb;

namespace testCommand

{

  class Class1

  {

  [STAThread]

  static void Main(string[] args)

  {

  OleDbConnection conn=new OleDbConnection("xxx");

  conn.Open();

  OleDbCommand comm=new OleDbCommand("Test",conn);

  comm.CommandType=CommandType.StoredProcedure;

  OleDbCommandBuilder.DeriveParameters(comm);

  try

  {

  comm.Parameters["Name"].Value="my name";

  comm.Parameters["Age"].Value=(object)”aa”;

  comm.ExecuteNonQuery();  //断点三,此处内存状况见表一

  }

  catch(Exception err)

  {

    Console.WriteLine(

  err.TargetSite+" -- "+err.StackTrace+" -- "+

  err.Source+" -- "+err.Message+" -- "+

  err.GetType().ToString());

  }

 

  //TODO: 增加对OleDbCommand进行修复的语句

 

  try

  {

  comm.Parameters["Name"].Value="my name";

  comm.Parameters["Age"].Value=(object)11;

  comm.ExecuteNonQuery();  //断点四

  }

  catch(Exception err)

  {

  Console.WriteLine(

  err.TargetSite+" -- "+err.StackTrace+" -- "+

  err.Source+" -- "+err.Message+" -- "+

  err.GetType().ToString());

  }

  conn.Close();

  }

  }

}

这时,在程序执行到断点三时会抛出System.FormatException的例外,再观察断点四处的内存状况:

+

icommandText

{System.__ComObject}

System.Data.Common.UnsafeNativeMethods.ICommandText

handle_Accessor

0

int

dbBindings

null

System.Data.OleDb.DBBindings

CommandText

Test

string

CommandType

StoredProcedure

System.Data.CommandType

发现icommandText,dbBindings,handle_Accessor三个私有属性只有icommandText被修改。程序在往下运行,我们得到System.Data.OleDb.OleDbException例外,提示Command text was not set for the command object。但是此时的CommandText及CommandType都为有效值。可见ExecuteNoQuery()在执行命令时依赖于icommandText,dbBindings和handle_Accessor。而之前的第一个try块,由于存储过程参数类型的不匹配抛出异常,使ExecuteNoQuery()执行和只有icommandText一个私有属性发生改变,此时虽然CommandText和CommandType还是原来正确的值,但由于dbBindings和handle_Accessor 没有被正确赋值而抛出认为没有命令文本的例外。

于是我们可以假设OleDbCommand在执行时是判断icommandText属性是否为空值再根据命令文本生成私有属性的时候,如果是则分析CommandText的值修改icommandText,dbBindings,handle_Accessor等3个私有属性;否则直接时用icommandText,dbBindings,handle_Accessor已有的值。如果CommandText的值发生改变,OleDbCommand重设icommandText,dbBindings,handle_Accessor的值,在下一次执行ExecuteNoQuery() 时根据命令文本重新生成私有属性的值。

这样,由存储过程参数不匹配而引起的OleDbCommand不可用的问题就迎刃而解了。只需要在两个try块间的加入以下语句 即可:

comm.CommandText=””;

comm.CommandText=”Test”;

以上对于OleDbCommand的讨论居于.Net framework1.0

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10752019/viewspace-956641/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    3982
  • 访问量
    7895625