0% found this document useful (0 votes)
54 views

Connect SQL Database To Your C# Program: Domainaced by

This document provides steps to connect a C# program to an SQL database. It explains what MySQL and C# are, how to create a project in C#, choose to connect to an SQL server, and add utility classes to handle the connection. Code samples are given for the utility classes to get a database connection. The document ends by providing a code example to test the connection.

Uploaded by

Rebwar Khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Connect SQL Database To Your C# Program: Domainaced by

This document provides steps to connect a C# program to an SQL database. It explains what MySQL and C# are, how to create a project in C#, choose to connect to an SQL server, and add utility classes to handle the connection. Code samples are given for the utility classes to get a database connection. The document ends by providing a code example to test the connection.

Uploaded by

Rebwar Khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Connect SQL Database to your C# program

Domainaced by : Prepared by :
Mhamad Kamal
Rebwar khalid Shkar Ismael
Rahel Akbar
1/7/2022 1
First we need know

1-What is MY SQL
Databace

2-What is c#

Answer to question (1)

MySQL, the most popular Open Source


SQL database management system, is
developed, distributed, and supported
by Oracle Corporation.

Answer to question (2)

C# is a general object-oriented
programming (OOP) language for
networking and Web development

1/7/2022 2
First we should Create a
Project, and *named
"ConnectSQLServer"

*you can named anything you like be

1/7/2022 3
Project was created.
Now we need chouse
:connect sql server

1/7/2022 4
You need some utility classes which help
to connect to SQL Server database.

1- DBSQLServerUtils.cs

2- DBUtils.cs

1/7/2022 5
select (DBSQLServerUtils.cs) and write this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace Tutorial.SqlConn { class DBSQLServerUtils {
public static SqlConnection GetDBConnection(string
datasource, string database, string username, string password)
{string connString = @"Data Source="+datasource+";Initial
Catalog=" +database+";Persist Security Info=True;User
ID="+username+";Password="+password; SqlConnection conn =
new SqlConnection(connString); return conn; } } }

1/7/2022 6
select (DBUtils.cs) and write this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace Tutorial.SqlConn {
class DBUtils { public static SqlConnection
GetDBConnection() {
string datasource = @"tran-vmware\SQLEXPRESS"; string
database = "simplehr"; string username = "sa"; string
password = "1234"; return
DBSQLServerUtils.GetDBConnection(datasource, database,
username, password); } } }

1/7/2022 7
And finally try this code to test connection:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tutorial.SqlConn;
using System.Data.SqlClient;
namespace ConnectSQLServer {
class Program {
static void Main(string[] args) {
Console.WriteLine("Getting Connection ...");
SqlConnection conn = DBUtils.GetDBConnection();
try { Console.WriteLine("Openning Connection ...");
conn.Open();
Console.WriteLine("Connection successful!");
}
catch (Exception e) {
Console.WriteLine("Error: " + e.Message);
}
Console.Read(); } } }

Out put

Getting Connection ...


Openning Connection ...
Connection successful!

1/7/2022 8
Thank you

1/7/2022 9

You might also like