关闭

C#调用ADO的RecordSet.AddNew方法.

791人阅读 评论(0) 收藏 举报
 

HTML Tags and JavaScript tutorial



C#调用ADO的RecordSet.AddNew方法.




项目中使用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.
 


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:152584次
    • 积分:4043
    • 等级:
    • 排名:第7961名
    • 原创:242篇
    • 转载:0篇
    • 译文:0篇
    • 评论:16条
    文章存档
    最新评论