65.9K
CodeProject is changing. Read more.
Home

Step by step introduction to Database programing in C# using controls

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.44/5 (22 votes)

Sep 4, 2003

2 min read

viewsIcon

54695

This article gives an introduction to adding Database support in a C# Windows program by using the available Data controls.

Introduction

Database support can be achieved in a c# program through different ways.The simplest way is to use the data controls available for the database operations.This article covers the usage of some of the data controls to attain Database connectivity.This will be of use for the Beginners.

Step 1

Start a new project.Select Windows application as the type.

Step 2

Select the server Explorer from the "View" menu item.It displays all the existing database connections.Select the Database Provider and  provide the connection parameters like userid,password etc.Select the required database connection and drag and drop that into the Form.Now the form will have a "OlEDbConnection1" object present at the bottom. 

Step 3

The second step is to insert a OleDbDataAdapter object.Select the OleDbDataAdapter control from the "Data" controls.Drag and Drop it in the form.It will promt you for some information  like

     1.Data connection to use -- > use the connection object that we created

    2.In the second screen select the "use sql statements" option
  
    3.In the next step select the Query Builder option.It will prompt a screen that includes the table  names.Select the required table and click on the required fields.This will generate the query for us.

  At the end of these steps we will be able to see the "oleDBDataAdapter1" object present in our form.

Step 4

The next Step is to Generate a Dataset object.On the properties of the OleDbDataAdapter1,there is an option to Generate Dataset.Create a new dataset using that option.After this a "dataset1" control will be present in our form.

Step 5

Now we will add a datagrid to our Form so that we will be able to see the contents of our table.We will also add a button "Load" which will Load the data into the Grid.The code that has to be added for binding the dataset with the datagrid has to be written in the "Load" button click .

  private void btnload_Click(object sender, System.EventArgs e)
  {
   oleDbDataAdapter1.Fill(dataSet11,"tablename"); /*Here the table name has to be given*/
   dataGrid1.SetDataBinding(dataSet11,"tablename");
  }   

Testing

The data from the table will be populated in the datagrid on the click of the Load button.The Data Can be edited and updated back to the table using the following code.

 

  private void btnUpdate_Click(object sender, System.EventArgs e)
  {
   oleDbDataAdapter1.Update(dataSet11,"tablename");
  }   

Some other tips

To find the current selected item in the Grid the following code can be used

  int iRowNum = dataGrid1.CurrentCell.RowNumber;
  int iColNum = dataGrid1.CurrentCell.ColumnNumber;
  object cellValue = dataGrid1[iRowNum, iColNum]; 
  string str = string.Format( "The content in the cell is {0}",cellValue);
Navigation of the grid can be done manually using some other buttons by using the following code.
  this.BindingContext[dataSet11, "tablename"].Position += 1; //move up
  this.BindingContext[dataSet11, "tablename"].Position -= 1; //move down
  

Conclusion

This is just an introduction to the database programming.There are much more to probe and find out!!!