因儲存過程引數型別不匹配而造成OleDbCommand的不可用(一) (轉)

amyz發表於2007-08-14
因儲存過程引數型別不匹配而造成OleDbCommand的不可用(一) (轉)[@more@]

因過程引數型別不匹配而造成OleCommand的不可用:namespace prefix = o ns = "urn:schemas--com::office" />

Key s:

  Stored Procedure/OleDbCommand//Parameter/Type/儲存過程/引數/型別

  在討論OleDbCommand出錯之前,還是想了解以下OleDbCommand一些執行機制。在裡面有如下儲存過程:

ALTER PROCEDURE dbo.Test

@Name  nvarchar(50),

@Age  smallint

AS

@Name, @Age

使用以下程式碼,並用Watch監視OleDbCommand例項的狀況:

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=()11;

    comm.ExecuteNonQuery();  //斷點一,此處記憶體狀況見表一

  }

  catch(Exception err)

  {

  Console.WriteLine(

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

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

  err.GetType().ToString());

  }

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

  }

  }

}

表格一,斷點一處的記憶體狀況

-

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

null

System.Data.Common.UnsafeNativeMethods.ICommandText

handle_Accessor

0

int

commandBehavior

Default

System.Data.CommandBehavior

dbBindings

null

System.Data.OleDb.DBBindings

canceling

FALSE

bool

isPrepared

FALSE

bool

executeQuery

FALSE

bool

computedParameters

FALSE

bool

designTimeVisible

FALSE

bool

cmdState

0

int

recordsAffected

0

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

 

餘下內容見 ASP?id=18427">因儲存過程引數型別不匹配而造成OleDbCommand的不可用(二)


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-956640/,如需轉載,請註明出處,否則將追究法律責任。

相關文章