ahmedilyas
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