天天看点

因存储过程参数类型不匹配而造成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

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();  // </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>