Solved

Converting some vb types to C#/.Net

Posted on 2016-08-03
3
96 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

636 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