Building and using CASE Tools Data Base with Microsoft SQL-Server and C#
common SQL keywords
Basic SELECT Query
. WHERE Clause
. WHERE Clause
INSERT Statement
UPDATE Statement
DELETE Statement
SQL-Server Database classes Namespace System.Data is the root namespace Namespace System.Data.SqlClient contains classes that are optimized to work with Microsoft SQL Server databases. An object of class SqlConnection (namespace System.Data.SqlClient) represents a connection to a data source specifically a SQL Server database. A SqlConnection object keeps track of the location of the data source and any settings that specify how the data source is to be accessed. An object of class SqlCommand (namespace System.Data.SqlClient) represents a SQL command that a DBMS can execute on a database. A program can use SqlCommand objects to manipulate a data source through a SqlConnection.
SQL-Server Database classes The program must open the connection to the data source before executing one or more SqlCommands and close the connection once no further access to the data source is required. Class DataTable (namespace System.Data) represents a table of data. A DataTable contains a collection of DataRows that represent the table's data. A DataTable also has a collection of DataColumns that describe the columns in a table. DataRow and DataColumn are both located in namespace System.Data. An object of class System.Data.DataSet, which consists of a set of DataTables and the relationships among them, represents a cache of data that a program stores temporarily in local memory. The structure of a DataSet mimics the structure of a relational database.
SQL server Database
Creating SQL Database file And Displaying a Database Table in a DataGridView
Create new windows application project
Click on add new data source option in data source window
In choose data source type dialog box select Dataset then click next
In choose your data connection dialog box click on new connection button
Select Microsoft SQL Server Database file option as a data source then click on continue button
In Add connection dialog box enter a name for the database file ex: database1 then click on ok button
A massage box will appear to tell you that this is a new database would you like to create it? Click on yes button
Now a new database is created with a specific connection string that will be used to connect the application to this database.
Click next
Click on NO button
Click on next
Click on previous button
Click on finish button
Open server explorer then click on database1 Open server explorer then click on database1.mdf then right click on Tables and choose Add new Table option
Add new filed "name" to the table as following
To save changes on the table click on update then Click on update database button
To change table name to "student"
Update the database then close the table
On data source window right click on database1Dataset then choose Configure data source with wizard then choose tables
Then choose table click finish
Drag and drop student table from data source window to the form
Run the program
Add new record then click save
Remove the record then click save
How Data Binding Works The technique through which GUI controls are connected to data sources is known as data binding. The IDE allows controls, such as a DataGridView, to be bound to a data source, such as a DataSet that represents a table in a database. Any changes you make through the application to the underlying data source will automatically be reflected in the way the data is presented in the data-bound control (e.g., the DataGridView). Likewise, modifying the data in the data-bound control and saving the changes updates the underlying data source.
Create new windows application then create SQL-server database with following tables Course int Cno Cmark stId Student int stId Nvarchar(50) stName
Drag and drop student table and Add a button “print names” to your form to print all student names as following
To print all student names in student table add the following code to the button “print names” event handler
Run the program and add the following students to student table
Then click the button the following message box will be shown with names of all students
Add three textboxes and three labels to the form as following then add new button “Add course” to add new course to table course in the database
In button “Add course” event handler add the following code
Add new button “print AVG” to the form to find the average for all marks in course table as following
Add the following code to button “print AVG” event handler
Add the following courses to course table then find the average of all marks
Click on print AVG button