fill vb dataset in sharpdevelop

I am writing Visual Basic code in Sharpdevelop. how do I connect   a dataset to a database?
fblack61Asked:
Who is Participating?
 
Meir RivkinFull stack Software EngineerCommented:
please disregard my previous post.

here's the code u requested in vb.net.
GetDataSet method accept the accdb file path and the table name and return dataset with the requested table:
Imports System
Imports System.Data
Imports System.Data.OleDb

Namespace test1
    Class OleDbTest
        Public Shared Function GetDataSet(ByVal dbFile As String, ByVal table As String) As DataSet
            'Create a new dataset with a table: Contacts
            Dim myDataSet As New DataSet("Contacts")
            
            Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbFile
            Dim conn As New OleDbConnection(connectionString)
            'create a new OleDB connection
            Try
                conn.Open()
                ' Open the connection
                'pass the Select statement and connection information to the initializxation constructor for the OleDBDataAdapter
                Dim myDataAdapter As New OleDbDataAdapter("SELECT * FROM " & table, conn)
                
                'Fill the dataset and table with the data retrieved by the select command
                myDataAdapter.Fill(myDataSet, "Contacts")
                    ' Close connection
                conn.Close()
            Catch ex As Exception
                Console.WriteLine("Error in connection: " & ex.Message)
            Finally
                If conn IsNot Nothing Then
                    conn.Close()
                End If
            End Try
            ' dispose of open objects
                'return the new dataset created
            Return myDataSet
        End Function
    End Class
End Namespace

Open in new window

0
 
Meir RivkinFull stack Software EngineerCommented:
which database u wish to connect?
ru using vb.net?
0
 
fblack61Author Commented:
Access 2007
0
 
Meir RivkinFull stack Software EngineerCommented:
to work with access 2007 u need the following connection string format:
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" + @"Data Source=c:\temp\contacts.accdb";

here's a sample code which query access db table called contacts:

using System;
using System.Data.OleDb;

namespace test1
{
	class OleDbTest
	{
		public static void Run()
		{
			string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" + @"Data Source=c:\temp\contacts.accdb";

			//create the database connection
			OleDbConnection aConnection = new OleDbConnection(connectionString);
			
			//create the command object and store the sql query
			OleDbCommand aCommand = new OleDbCommand("select * from contacts", aConnection);
			try
			{
				aConnection.Open();
				
				//create the datareader object to connect to table
				OleDbDataReader aReader = aCommand.ExecuteReader();
				Console.WriteLine("This is the returned data from contacts table");
				
				//Iterate throuth the database
				while(aReader.Read())
				{
					Console.WriteLine(aReader.GetInt32(0).ToString());
				}
				
				//close the reader 
				aReader.Close();
				
				//close the connection Its important.
				aConnection.Close();
			}			
			//Some usual exception handling
			catch(OleDbException e)
			{
				Console.WriteLine("Error: {0}", e.Errors[0].Message);
			}
		}
	}
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.