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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.