Solved

Converting some vb types to C#/.Net

Posted on 2016-08-03
3
59 Views
Last Modified: 2016-08-10
I'm converting a vb class to C#/.Net. I can't find equivalents for the following:
RecordSet
FieldAttributeEnum
DataTypeEnum

I think RecordSet can be replaced with DataSet, but I don't know about the other two. Any ideas?

Some example code:

    Private Function CreateRecordsetFromDataGrid(ByVal DGV As DataGridView) As Recordset
        Dim rs As New Recordset
        'Create columns in ADODB.Recordset
        Dim FieldAttr As FieldAttributeEnum
        FieldAttr = FieldAttributeEnum.adFldIsNullable Or FieldAttributeEnum.adFldIsNullable Or
                    FieldAttributeEnum.adFldUpdatable
        For Each iColumn As DataGridViewColumn In DGV.Columns
            'only add Visible columns
            If iColumn.Visible = True Then
                Dim FieldType As DataTypeEnum
                'select dataType
                If iColumn.ValueType Is GetType(Boolean) Then
                    FieldType = DataTypeEnum.adBoolean

Open in new window

0
Comment
Question by:allelopath
3 Comments
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 41740804
You need to add the COM reference of Microsoft ActiveX Data Objects 2.X Library.

Create ADO Connection and Recordset Objects in Visual C# .NET

Create a new Visual C# .NET Windows application project.
On the Project menu, click Add Reference.
Click the COM tab. Click Microsoft ActiveX Data Objects 2.X Library.

        // You must first load the COM reference Microsoft ActiveX Data Objects 2.X Library

        private ADODB.Recordset CreateRecordsetFromDataGrid(DataGridView DGV)
        {
            ADODB.Recordset rs = new ADODB.Recordset();

            try
            {
                ADODB.FieldAttributeEnum FieldAttr = ADODB.FieldAttributeEnum.adFldIsNullable | ADODB.FieldAttributeEnum.adFldUpdatable;

                foreach (DataGridViewColumn iColumn in DGV.Columns)
                {
                    if (iColumn.Visible)
                    {
                        ADODB.DataTypeEnum FieldType;

                        if (iColumn.ValueType == typeof(Boolean))
                        {
                            FieldType = ADODB.DataTypeEnum.adBoolean;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // Don't leave this blank!
            }

            return rs;
        }

Open in new window

0
 
LVL 1

Author Comment

by:allelopath
ID: 41740839
My apologies on not being clear in my question. The goal is to move away from ADODB.
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 41740896
Recordet would be quivalent of DataSet/Datatable


I'd suggest something like this:


private DataTable CreateRecordsetFromDataGrid(DataGridView dgv)
{
    Datatable dt = new DataTable();

    Array.ForEach(
    dgv.Columns.Cast<DataGridViewColumn>().ToArray(), 
    arg => dt.Columns.Add(arg.HeaderText, arg.ValueType));
    Array.ForEach(
    dgv.Rows.Cast<DataGridViewRow>().ToArray(), 
    arg => dt.Rows.Add(arg.Cells.Cast<DataGridViewCell>().Select(cell => cell.Value)));

    return dt;
}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now