項目中使用WebService,使用C#編寫,調用VB寫的組件,VB組件使用ADO,其中有的方法是需要傳遞Recordset的,但是在C#中的Recordset.AddNew的方法與VB中的不同,這是由於C#沒有預設的參數.
C# 中的AddNew的方法原型為:AddNew(Object FieldList,Object ValueList),開始時被其參數明型糊塗了,以為要用Object數組,但是參數又不是數組的形式,能過在網上查詢,終於找到瞭解決的辦法,實現了與VB中相類似的實現.代碼如下:
prjCommon.c_I_ReceiveGoods rev=new prjCommon.c_I_ReceiveGoodsClass();
object missing=System.Reflection.Missing.Value;
rev.get_ICommon_Data().AddNew(missing,missing);
rev.get_ICommon_Data().Fields[rev.ArticleNo].Value=rd.ArticleNo;
rev.get_ICommon_Data().Fields[rev.ClassID].Value=rd.ClassID;
rev.get_ICommon_Data().Fields[rev.DateTime].Value=rd.RevDateTime;
rev.get_ICommon_Data().Fields[rev.IsProcessing].Value=Convert.ToInt32(rd.Processing);
rev.get_ICommon_Data().Fields[rev.Length].Value=rd.Length;
rev.get_ICommon_Data().Fields[rev.OperatorID].Value=rd.OperatorID;
rev.get_ICommon_Data().Fields[rev.ORollNo].Value=rd.ORollNo;
rev.get_ICommon_Data().Fields[rev.POrderID].Value=rd.POrderID;
rev.get_ICommon_Data().Fields[rev.POrderItem].Value=rd.POrderItem;
rev.get_ICommon_Data().Fields[rev.Qty].Value=rd.Qty;
rev.get_ICommon_Data().Fields[rev.Width].Value=rd.Width;
rev.get_ICommon_Data().Update(missing,missing);
其中get_ICommon_Data()就是Recordset.
最重要的地方就是object missing=System.Reflection.Missing.Value;這句.
附上在網上找到的資料:
Comment from drichards
Date: 01/13/2005 07:30AM PST
Comment
It turns out that either of those methods works if you take out the "Mode=Read;" from the connection open statement in my first code sample. Otherwise the connection is read-only.
Here's the syntax for the various options:
object rv = System.Reflection.Missing.Value;
// Single insert
cxn.Execute("INSERT INTO Dimension (Dimension_Id, Dimension_Name) VALUES (22,'FIRST_NEW_DIM')", out ra, (int)ADODB.ExecuteOptionEnum.adExecuteNoRecords);
// Prepared Command with parameters - you can keep setting new values for the params and calling execute again
ADODB.Command cmd = new ADODB.CommandClass();
cmd.ActiveConnection = cxn;
string parmVal = "NEXT_NEW_DIM";
cmd.CommandText = "INSERT INTO Dimension (Dimension_Id, Dimension_Name) VALUES (?, ?)";
cmd.Prepared = true;
cmd.Parameters.Append(cmd.CreateParameter("?_1", ADODB.DataTypeEnum.adInteger, ADODB.ParameterDirectionEnum.adParamInput, 4, 32));
cmd.Parameters.Append(cmd.CreateParameter("?_2", ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, parmVal.Length, parmVal));
cmd.Execute(out ra, ref rv, (int)ADODB.CommandTypeEnum.adCmdText + (int)ADODB.ExecuteOptionEnum.adExecuteNoRecords);
// Use a Recordset
ADODB.Recordset rsta = new ADODB.RecordsetClass();
rsta.Open("Dimension", cxn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, (int)ADODB.CommandTypeEnum.adCmdTable);
object missing = System.Reflection.Missing.Value;
rsta.AddNew(missing, missing);
rsta.Fields[0].Value = 27;
rsta.Fields[1].Value = "NEW_DIM";
rsta.Update(missing, missing);
// Or use single Update from example in my previous post.