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

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

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

 

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

-

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()时,将改变其内部icommandTextdbBindingshandle_Accessor3个私有属性。并且经过观察发现,只有CommandText被赋予与原来不同的值时才把以上3个私有属性的值恢复到原来的默认值(即icommandTextnulldbBindingsnullhandle_Accessor0)。

这时我们把上面的代码稍作修改,在第一个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();  // </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值