Lab8 - Accessing Data in C#
Lab8 - Accessing Data in C#
Most applications revolve around reading and updating information in databases. To enable data
integration in distributed, scalable applications, Visual Studio provides support for integrating
data into your applications.
Example:
Create the following windows form in visual studio containing the given labels, buttons and
DataGridView.
1
• Deleting data inside a database.
• Viewing data from one or more tables inside a database.
How to:
1- Insert New Records into a Database
To insert new records into a database, you can use command objects to interact and
insert new records in your database (for example, SqlCommand).
Example:
Through this example we will do a windows form for course registeration as shown
below and save the course details into table “Course” which is in database
“MiniUniversity”
Double click on the “Insert” button to start writing in it’s event handler then write your
code
a- You should add the following 2 liberiries “using System.Data;” and “using
System.Data.SqlClient;”
2
Note : you should write your server name in “Data source” and database name in “Initial
Catalog”
3
In the above example we updated the name of an existing course using it’s code.
The follwing example deletes an existing course from table “course” in database
“MiniUniversity”.
con.Open();
SqlCommand myCommand = new SqlCommand("delete from Course where CrsCode= '" + textBox1.Text + "'",
con);
myCommand.ExecuteNonQuery();
con.Close();
}
4
4. Enter the server information ( Name-Userid "sa") and then choose your database then click
finish
5. The connection you made will now appear, from it expand your database and then add the
tables you want by clicking the > Button
6. Then add the fields that will be displayed in the report
7. The next three windows are optional if you want to add chart or filter the fields To be
displayed or adding a header style to you report
8. From File menu choose Add New Item and choose from templates "Windows Form"
9. From the ToolBox add from Window Forms tab add CrystalReportViewer
10. In the Form_load Write the following code
CrystalReport1 c=new CrystalReport1();
crystalReportViewer1.ReportSource=c;
b- Second way:
In the following example we will implement a master detail form for displaying the
categories and its products.
1. from the data menu, click on add data source and continue the wizard by selecting the
wanted database and needed tables ( in this example select table categories and table
products)
2. from the data sources window you can choose either to select simple or complex data
binding( details for simple data binding and DatagridView for complex)
5
3. for the table categories select details and drag it on your form
4. then add group box to your form and drag the products below the categories to the group
box
5. this by default will add the components mentioned above such as dataset and binding
navigator
6. run and test the application
7. to add new parameterized query, right click on the “categoriesTableAdapter” → Add
Query
8. start new query and open the query builder then add filter on categoryId column named
@categoryID
6
9. This will add tool strip contain textbox and button, change their display text as shown