create a winform app that communicates with the database using
TRANSCRIPT
![Page 1: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/1.jpg)
WinForm/C# Database App
Hans-‐Pe7er Halvorsen, M.Sc.
using ADO.NET
![Page 2: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/2.jpg)
2
Database Tables
![Page 3: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/3.jpg)
3
Execute the different Scripts inside SQL Server Management Studio
![Page 4: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/4.jpg)
4
This Example shows how to create a WinForm App that gets Data from a Database into a DataGridView
![Page 5: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/5.jpg)
5
Add a WinForm Project
![Page 6: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/6.jpg)
6
Add a New Class to the Project (“StudentData.cs”)
“StudentData.cs”
![Page 7: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/7.jpg)
7
Create the Code, e.g., like this (“StudentData.cs”):
Improvements: Use Try... Catch ...
A View that collects data from several tables
SQL Query
![Page 8: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/8.jpg)
8
Add a New Class (“StudentWinForm.cs”)
![Page 9: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/9.jpg)
9
Add Code in Class
Add a Reference to the Assembly in the Logic Tier
![Page 10: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/10.jpg)
10
Code for Class “StudentWinForm.cs”
using System.Data; using Tuc.School.LogicTier; namespace Tuc.School.WinFormApp { class StudentWinForm { public DataSet GetStudent(string connectionString) { StudentData studentData = new StudentData(); return studentData.GetStudentDB(connectionString); } } }
Since we are using the DataSet Class
![Page 11: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/11.jpg)
11
Create Form
Label
DataGridView
![Page 12: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/12.jpg)
12
Create Form Code
![Page 13: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/13.jpg)
13
using System.Configuration; using Tuc.School.WinFormApp; namespace WinFormApp { public partial class Form1 : Form { private string connectionString =
ConfigurationManager.ConnectionStrings["SCHOOLConnectionString"].ConnectionString; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { FillStudentGrid(); } private void FillStudentGrid() { DataSet ds = new DataSet(); StudentWinForm studentList = new StudentWinForm(); ds = studentList.GetStudent(connectionString); dataGridViewStudentInformation.DataSource = ds.Tables[0]; } } }
WinForm Code Note!
ConnecZonString is stored in App.config
![Page 14: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/14.jpg)
14
Note! Add “System.ConfiguraZon” Reference
![Page 15: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/15.jpg)
15
Create DB ConnecZonString in App.config <?xml version="1.0" encoding="utf-‐8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <connectionStrings> <add name="SCHOOLConnectionString" connectionString="Data Source=macwin8;Initial Catalog=SCHOOL;Persist Security Info=True;User ID=sa;Password=xxxxxx" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>
![Page 16: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/16.jpg)
16
Test it
It works!!!
![Page 17: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/17.jpg)
Recommended Li7erature
• Tutorial: IntroducZon to Database Systems h7p://home.hit.no/~hansha/?tutorial=database
• Tutorial: Structured Query Language (SQL) h7p://home.hit.no/~hansha/?tutorial=sql
• Tutorial: Using SQL Server in C# • Tutorial: IntroducZon to Visual Studio and C# h7p://home.hit.no/~hansha/?tutorial=csharp
17
![Page 18: Create a WinForm App that Communicates with the Database using](https://reader033.vdocuments.site/reader033/viewer/2022052405/5868ba0a1a28abdd708b4c98/html5/thumbnails/18.jpg)
Hans-‐PeKer Halvorsen, M.Sc. Telemark University College Faculty of Technology Department of Electrical Engineering, InformaVon Technology and CyberneVcs
E-‐mail: [email protected] Blog: hKp://home.hit.no/~hansha/
18