本文介绍了RecordSet.Open的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在使用代码将数据集转换为记录集,我想知道这行的错误是什么,我运行它后记录集仍然关闭,但是如果我在C#中进行了相同的代码,它将打开记录集. >

Hi all,
i''m using code to convert dataset to recordset and i want to know what is the error at this line, after i running it the recordset still closed, but if i make same code in c#, it open the recordset.


ADODB.Recordset.Open(Reflection.Missing.Value, Reflection.Missing.Value, ADODB.CursorTypeEnum.adOpenStatic,
                                ADODB.LockTypeEnum.adLockOptimistic, 0)



谢谢



Thanks

推荐答案



public ADODB.Recordset ConvertToRecordset(string XMLDataset)
        {
            DataTable inTable;
            string FileName;
            FileName = "c:\\temp\\result" + DateTime.Now.Millisecond + ".xml";
            ADODB.Recordset result = new ADODB.Recordset();
             XmlTextWriter xwriter = new XmlTextWriter(FileName, System.Text.Encoding.Default);
            DataSet ds = new  DataSet();
            StringReader xmlSR = new StringReader(XMLDataset);
            ds.ReadXml(xmlSR);
            inTable = ds.Tables[0];
            result.CursorLocation = ADODB.CursorLocationEnum.adUseClient;

            ADODB.Fields resultFields = result.Fields;
            System.Data.DataColumnCollection inColumns = inTable.Columns;

            foreach (DataColumn inColumn in inColumns)
            {
                resultFields.Append(inColumn.ColumnName
               , TranslateType(inColumn.DataType)
               , inColumn.MaxLength
               , inColumn.AllowDBNull ? ADODB.FieldAttributeEnum.adFldIsNullable :
               ADODB.FieldAttributeEnum.adFldUnspecified
              , null);
            }

            result.Open(System.Reflection.Missing.Value
           , System.Reflection.Missing.Value
           , ADODB.CursorTypeEnum.adOpenStatic
           , ADODB.LockTypeEnum.adLockOptimistic, 0);
            
            foreach (DataRow dr in inTable.Rows)
            {
                result.AddNew(System.Reflection.Missing.Value,
                System.Reflection.Missing.Value);

                for (int columnIndex = 0; columnIndex < inColumns.Count; columnIndex++)
                {
                    resultFields[columnIndex].Value = dr[columnIndex];
                }
            }

            return result;
        }


        ADODB.DataTypeEnum TranslateType(Type columnType)
        {
            switch (columnType.UnderlyingSystemType.ToString())
            {
                case "System.Boolean":
                    return ADODB.DataTypeEnum.adBoolean;

                case "System.Byte":
                    return ADODB.DataTypeEnum.adUnsignedTinyInt;

                case "System.Char":
                    return ADODB.DataTypeEnum.adChar;

                case "System.DateTime":
                    return ADODB.DataTypeEnum.adDate;

                case "System.Decimal":
                    return ADODB.DataTypeEnum.adCurrency;

                case "System.Double":
                    return ADODB.DataTypeEnum.adDouble;

                case "System.Int16":
                    return ADODB.DataTypeEnum.adSmallInt;

                case "System.Int32":
                    return ADODB.DataTypeEnum.adInteger;

                case "System.Int64":
                    return ADODB.DataTypeEnum.adBigInt;

                case "System.SByte":
                    return ADODB.DataTypeEnum.adTinyInt;

                case "System.Single":
                    return ADODB.DataTypeEnum.adSingle;

                case "System.UInt16":
                    return ADODB.DataTypeEnum.adUnsignedSmallInt;

                case "System.UInt32":
                    return ADODB.DataTypeEnum.adUnsignedInt;

                case "System.UInt64":
                    return ADODB.DataTypeEnum.adUnsignedBigInt;

                case "System.String":
                default:
                    return ADODB.DataTypeEnum.adVarChar;
            }
        }



vb.net代码



vb.net code

Public Function ConvertToRecordset(ByVal XMLDataset As String) As ADODB.Recordset

       Dim FileName As String = ""
       ConvertToRecordset = New ADODB.Recordset
       FileName = "c:\temp\result" & Now.TimeOfDay().Milliseconds & ".xml"
       Dim inTable As New DataTable
       Dim xwriter As New XmlTextWriter(FileName, System.Text.Encoding.Default)
       Dim ds As New DataSet
       Dim xmlSR As StringReader = New StringReader(XMLDataset)
       Dim result As ADODB.Recordset = New ADODB.Recordset
       ds.ReadXml(xmlSR)
       inTable = ds.Tables(0)
       result.CursorLocation = ADODB.CursorLocationEnum.adUseClient
       Dim resultFields As ADODB.Fields = ConvertToRecordset.Fields
       Dim inColumns As DataColumnCollection = inTable.Columns
       For Each inColumn As DataColumn In inColumns
           On Error Resume Next

           resultFields.Append(inColumn.ColumnName, TranslateType(inColumn.DataType.ToString), inColumn.MaxLength, _
                                 IIf(inColumn.AllowDBNull, ADODB.FieldAttributeEnum.adFldIsNullable, ADODB.FieldAttributeEnum.adFldUnspecified), Null)
           ''resultFields.Append("test", ADODB.DataTypeEnum.adVarChar, -1, ADODB.FieldAttributeEnum.adFldUnspecified, Null)
       Next
       result.Open(Nothing, Nothing, ADODB.CursorTypeEnum.adOpenStatic, _
                               ADODB.LockTypeEnum.adLockOptimistic, 0)

       For Each dr As DataRow In inTable.Rows
           result.AddNew(System.Reflection.Missing.Value, System.Reflection.Missing.Value)
           For columnIndex As Integer = 0 To inColumns.Count
               resultFields(columnIndex).Value = dr(columnIndex)
           Next
       Next
       ''ConvertToRecordset.Open()
       Return result
   End Function

   Private Function TranslateType(ByVal dtype As String) As String
       Select Case (dtype)
           Case "System.Int32"
               Return "int"
           Case "System.DateTime"
               Return "dateTime"
       End Select
   End Function


这篇关于RecordSet.Open的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 12:07