populating an Access database using visual basic 2005
I am trying to create a new employee application that gets input from a user in text boxes and then take that information and populate a database in Access that will later be pulled to the Track IT! database on SQLserver2005. How do i do it?
Thank you.
there are a few ways of doing this but the easiest way probably would be to use a datagridview and dataAdapters.
the datagridview is a control which allows you to databind values to it, and you can view/delete/edit/add records to the control (similar type of layout as when you view a Table in MS Access)
using a DataAdapter allows you to easily "fill" a dataset with the query given and being able to allow you to also update the datasource (database). There are many topics about this on these forums, if you do a forum search you will find some hits and a good few examples :-)
Typical example:
we have an MS Access database/file. We want to get all the records from a specific table and show it to the user.
import the System.Data.OleDb namespace at the top of your class file:
imports System.Data.OleDb
drag and drop a datagridview on the form in designer view. Change the properties of it if you like to whatever you need it to be changed to.
next up, say if we have a button which goes and "fetches" the records, we need to code on how to do this. So, there are a couple of things that need to be done.
Create an OleDbCommand and OleDbDataAdapter as well as the dataset (which contains our records) globally so you can access these from any where within the current class:
Dim theOleDbCommand as new OleDbCommand()
Dim theOleDbDataAdapter as new OleDbDataAdapter(theOleDbCommand)
Dim theDataSet as new DataSet()
Now, double click on the button to create a button click event so on this we would code on retrieving the records. The code would look something like this:
| |
Me.theOleDbCommand = new OleDbCommand("SELECT * FROM table", new OleDbConnection(ConnectionString)) Me.theOleDbDataAdapter = new OleDbDataAdapter(Me.theOleDbCommand) Me.theDataSet = new DataSet() Me.theOleDbCommand.Connection.Open() Me.theOleDbDataAdapter.Fill(Me.theDataSet) Me.theOleDbCommand.Connection.Close() Me.theDataGridView.DataSource = Me.theDataSet.Tables(0).DefaultView
|
the connection string looks like this, but you need to modify it appropriately (Changing the path to the file for example)
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\somepath\mydb.mdb;User Id=;Password=;"
So whats happening above is, we create a query (SELECT * FROM table) to be executed, we then create a dataAdapter (which does all the hard work for us) and "fill" the dataset with records returned from that query.
Next we then "bind" the dataset (which has the records) to the datagridview control so the users can see the results.
That's all there is to it for this part!
The user can update the records if they like from the datagridview, the changes then are committed to the dataset so when we do an Update() on the data adapter, the changes are then committed to the database itself. The code would go something like this, but not guarenteed to work as I am writing from mind.
Place an "Update" button (drag and drop button on form and double click it) and then do this:
Me.theOleDbCommand.Connection.Open()
Me.theOleDbDataAdapter.Update(Me.theDataSet)
Me.theOleDbCommand.Connection.Close()
this should then update the database. Of course be sure to catch errors etc... but for now, lets just think there are no errors just to make it simple.
As for inserting the data from the textboxes to the database, the above solution would be best simply because you don't need to worry about what fields exist in the database so you can generate the INSERT command (as you need to know the fields to insert the data to)
However if you need to then the above still applies but with a couple of changes. No need for the data adapter or the dataset.
| |
'Insert record Me.theOleDbCommand = new OleDbCommand("INSERT INTO [tableName] (field1, field2) VALUES (?, ?)") Me.theOleDbCommand.Parameters.Add(new OleDbParameter("@p1", OleDbType.fieldType).Value = Me.theTextBox1.Text)) Me.theOleDbCommand.Parameters.Add(new OleDbParameter("@p2", OleDbType.fieldType).Value = Me.theTextBox2.Text)) Me.theOleDbCommand.Connection.Open() Me.theOleDbCommand.ExecuteNonQuery() Me.theOleDbCommand.Connection.Close()
|
Now, we create an insert command to insert the data/record. We need to know the field names and then supply it the "parameters" or values to insert into those fields.
We then create a parameter and add it into the OleDbCommand object, so when it executes the command it can take those parameters and insert them correctly.
You also generally need to know about the data field type of that fields (if its an Integer, NvarChar, Binary etc...) as otherwise the insertion may fail.
The values are then given for that parameter with the values entered from the textbox. The code above is generally how it works but not guarenteed to work 100%
does this help/shed some light?
a) the OleDbCommand is a command object in order for you to do some things with the database, such as executing queries (insert/delete/select/delete). Here is some documentation about it:
http://msdn2.microsoft.com/en-us/library/system.data.oledb.oledbcommand.aspx
b) the code is up to you where you are wanting to place it - it depends what you want to do really and what the "code" does. In my first example I am selecting all records from a table in the database so it would be good to place it on a button. 99% of the time, its best not to instantly connect to the database to do your task - slow start ups, establishing connections to databases and doing database related stuff can be expensive so its best to do it "on demand" meaning, when the user likes to - generally this is the case.
inserting data would be the same thing also - on a button for example, or perhaps everytime the user enters details/modifies details in the datagridview - it depends on the code you have. When you modify data in the datagridview, it automatically updates the dataset its bound to then to finally commit changes to the datasource (database) then you call the Update(theDataSet) command of the dataAdapter (this is an example)
if you are inserting data say from textboxes into the database, then yes, it should be done on the click of a button (example in my second example). There are many ways of going about doing this, and depends on what is happening and what you want to achieve :-)
c) Nvarchar is a database data type - basically in a sense, a string. Read about data types:
http://msdn2.microsoft.com/en-us/library/system.data.oledb.oledbtype.aspx
I hope this helps you in some way or gives you some more/better understanding
Me.theOleDbCommand = new OleDbCommand("INSERT INTO [tableName] (field1, field2) VALUES (?, ?)")
Me.theOleDbCommand.Parameters.Add(new OleDbParameter("@p1", OleDbType.fieldType).Value = Me.theTextBox1.Text))
Me.theOleDbCommand.Parameters.Add(new OleDbParameter("@p2", OleDbType.fieldType).Value = Me.theTextBox2.Text))
a couple questions about this part of the code.
A) i realize the [tablename] is the name of the database... my question is with the fields and values... its like normal arguments? do they line up?
For example
Me.theoledbcommand = new oledbcommand("INSERT INTO [newemployee.db] (date, employee name) VALUES (9/24/06, IGiberson)
B) can you explain the lines under that to me or point me in the direction that i can read about what that is exactly and how it works? :(