(续 因存储过程参数类型不匹配而造成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 ()时,将改变其内部 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+"\r\n--\r\n"+err.StackTrace+"\r\n--\r\n"+
err.Source+"\r\n--\r\n"+err.Message+"\r\n--\r\n"+
err.GetType().ToString());
}
//TODO: 增加对 OleDbCommand 进行修复的语句
try
{
comm.Parameters["Name"].Value="my name";