Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


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


Thanks
Posted
Comments
Nish Nishant 24-Oct-10 12:43pm    
Post both your C# and VB versions here with details on what you get with each. That way someone may be able to tell you what you are doing differently.
Abhinav S 24-Oct-10 12:48pm    
Good point - both versions are needed here.

Instead of MissingValue, replace them with Nothing. C# doesn't support optional values (until 4.0 at least).
 
Share this answer
 
Comments
Dalek Dave 25-Oct-10 4:06am    
Good Call!
Thanks for help, but nothing didn't change anything :(
 
Share this answer
 
Comments
Nish Nishant 25-Oct-10 8:21am    
Yet you marked it as answered. If your problem is still unsolved don't mark it as answered, although you may still 5 vote good responses.
C# code

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 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
 
Share this answer
 
Comments
Dave Kreskowiak 25-Oct-10 7:54am    
OK, curiosity is getting to me. Why even bother to do this?? The old ADODB stuff is so VB6'ish, it borders on obsolete. Why not just use an ADO.NET DataTable??
because in my company the system made from vb6 and now i'm making some web services to call it from vb6, most of this services function return dataset and vb6 didnt use it, so i made this component to convert dataset to recordset :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900