How do I implement VB code for Data Adapter & Adapter.Fill() using C#?

Josh S 26 Reputation points
2024-02-19T07:47:53.5166667+00:00
Dim con As New OleDb.OleDbConnection 
Dim dA As OleDb.OleDbDataAdapter 
Dim dS As New DataSet
Dim selectCommand As String 
Dim dT As New DataTable 
selectCommand = "SELECT * FROM tblA" dA = New OleDb.OleDbDataAdapter(selectCommand, con) dA.Fill(dS) dA.Fill(dT) 'get error here as I try to code it in c# 
Dim strSelecTblA As String 
strSelecTblA = “Select * from tbl_A” 
Dim dAdpt As New OleDbDataAdapter (strSelecTblA, Connection) 
dAdpt.SelectCommand = new OleDbCommand (strSelecTblA, Connection) 
dAdpt.TableMapping.Add(“tbl_A”, “tbl_A”)
Dim test_build As OleDbCommand 
test_build = new OldDbConnecection(dAdpt) 
dAdpt 
InsertCommand = test_build.GetInsertCommand() 
Dim myDs As New DataSet() 
dAdpt.Fill(myDs, “tlb_A”) 
myDs.Tables.Item(0).TableName = “tble_A” 
Dim myDt As New DataTable() 
dAdpt.Fill(myDt) 'get error in this line as I try to code it in c# 
Dim InfoRow As DataRow
For each thisRow In myDt 
RecNumber = myDt.Row.IndexOf (thisRow) + 1 
New tbl_A_Row = myDs.Tables(“tbl_A”).NewRow() 
tbl_A_Row.BeginEdit()
tbl_A_Row.Item(“tbl_A.RecNumber) 
tbl_A_Row.Item(“tbl_A.OrderNum)

 ... ... 

tbl_A_Row.EndEdit() 
myDt.Tables(“tbl_A”).Rows.Add(tbl_A_Row)

The above code I am trying to change from VB to C# but it is giving me error at the following lines :

dA.Fill(dT)

New tbl_A_Row = myDs.Tables(“tbl_A”).NewRow()

I can use your help how I can implement this using C#

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,572 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,573 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,642 questions
Access Development
Access Development
Access: A family of Microsoft relational database management systems designed for ease of use.Development: The process of researching, productizing, and refining new or existing technologies.
849 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,306 Reputation points
    2024-02-19T16:57:56.7966667+00:00

    Hi,
    to get DataTable you can use this code in C#:

    using System.Data.OleDb;
    
    
    		internal DataTable LoadData()
    		{
    			OleDbConnection con = new OleDbConnection("[My ConnectionString]");
    			DataTable myDt = new DataTable();
    			string strSelecTblA = "Select* from tbl_A";
    			using (OleDbDataAdapter dA = new OleDbDataAdapter(strSelecTblA, con)) dA.Fill(myDt);
    			return myDt;
    		}
    
    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Jiachen Li-MSFT 28,076 Reputation points Microsoft Vendor
    2024-02-21T07:43:10.7733333+00:00

    Hi @Josh S ,

    You can refer to the following code.

    using System.Data;
    using System.Data.OleDb;
    
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = "your_connection_string_here";
            string selectCommand = "SELECT * FROM tblA";
    
            using (OleDbConnection con = new OleDbConnection(connectionString))
            {
                using (OleDbDataAdapter dA = new OleDbDataAdapter(selectCommand, con))
                {
                    DataSet dS = new DataSet();
                    DataTable dT = new DataTable();
    
                    // Fill the DataSet and DataTable
                    dA.Fill(dS);
                    dA.Fill(dT);
    
                    // Create a new select command
                    string strSelectTblA = "SELECT * FROM tbl_A";
                    OleDbDataAdapter dAdpt = new OleDbDataAdapter(strSelectTblA, con);
                    dAdpt.SelectCommand = new OleDbCommand(strSelectTblA, con);
    
                    // Add table mapping
                    dAdpt.TableMappings.Add("tbl_A", "tbl_A");
    
                    // Get insert command
                    OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(dAdpt);
                    OleDbCommand insertCommand = cmdBuilder.GetInsertCommand();
    
                    // Fill DataSet and DataTable
                    DataSet myDs = new DataSet();
                    dAdpt.Fill(myDs, "tbl_A");
    
                    DataTable myDt = new DataTable();
                    dAdpt.Fill(myDt);
    
                    // Add rows to DataSet
                    foreach (DataRow thisRow in myDt.Rows)
                    {
                        int RecNumber = myDt.Rows.IndexOf(thisRow) + 1;
                        DataRow tbl_A_Row = myDs.Tables["tbl_A"].NewRow();
                        tbl_A_Row.BeginEdit();
                        tbl_A_Row["RecNumber"] = RecNumber;
                        // Set other column values here
                        tbl_A_Row.EndEdit();
                        myDs.Tables["tbl_A"].Rows.Add(tbl_A_Row);
                    }
                }
            }
        }
    }
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments