Click here to Skip to main content
16,016,781 members
Articles / Programming Languages / C#
Article

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

Rate me:
Please Sign up or sign in to vote.
2.44/5 (22 votes)
3 Sep 20032 min read 54.6K   22   3
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!!!

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
I am Sanjeev Krishnan.

Comments and Discussions

 
GeneralStep by step introduction to Database programming in C# (by san77in - Sanjeev Krishnan) Pin
jane jacobe5-Dec-07 4:10
sussjane jacobe5-Dec-07 4:10 
GeneralGlad its new to you Pin
Anonymous4-Sep-03 7:11
Anonymous4-Sep-03 7:11 
GeneralThis sucks Pin
dog_spawn4-Sep-03 13:01
dog_spawn4-Sep-03 13:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.