Csharp Connect To SQL Server PDF
Csharp Connect To SQL Server PDF
You can connect your C# application to data in a SQL Server database using
the .NET Framework Data Provider for SQL Server. We can have to
approaches to connect to SQL server database.
Select your windows form application and give appropriate name for
your project. Please don’t leave it empty or ambiguous name like
WindowsFormsApp12. Look My project name:
1
DMU C# HKA
2. Create your database being on the visual studio interface, and create
your tables as appropriate. On my case I created a database named
Accounts and a table login.
A. Right Click on your project name (“Connect Local Database”) and add
new item
2
DMU C# HKA
C. Now you have already created your database. Look your solution
explorer and your new database Accounts.mdf is added.
D. Now you are supposed to create your tables this is done on the Server
Explorer. To access the Server Explorer either double click on the
database name that you have seen above on the solution explorer or
3
DMU C# HKA
The next step take some time according to the speed of your computer this is
because SQL server to be integrated locally may take some minutes according
to the speed and performance of your computer. (Be Patient until SQL Server
loads)
4
DMU C# HKA
Right click on your table (Login) and click on show table data. This is
similar to selecting top 1000rwos in SQL server for editing
To design queries for your database right click on your table and click
on New Query
5
DMU C# HKA
b. Selecting records
6
DMU C# HKA
Now return back to your visual studio interface and design the buttons
for navigation or drag and drop the textboxes from your data source.
Two Labels
Two Textboxes: for username and Password
Four Buttons for Navigation
One Data Grid View for looking all you records
7
DMU C# HKA
Then follow the wizard to connect your SQL database you have created earlier
to your interface designed
8
DMU C# HKA
Now your databinding is connected to your interface so, return back and click
ok. On the data sources you can see your accounts Dataset you have created
earlier
10
DMU C# HKA
You can do two things now. Both ways work perfect. The first
approaches takes time to provide data sources. While the second
approach automatically put linked textboxes on the form.
o On the property window either set your textbox1 and
textbox2 with proper Data Source giving the text as follows. On
the DataBindings UserName textbox1 and Password for
textbox2
Or
o You can drag and drop the details on your form if you have not
put textbox1 and textbox2 earlier
11
DMU C# HKA
Then Drag and drop your login table with the fields on the form. It will
automatically put the label and text boxes on the form.
You can see your data grid view is also set with data source
(bindingSource1) and data member (Login).
Now let’s start testing if it works. Run your project and all of the three sources
should be filled with your database data. If so successful you have populated
your interface with values from the local database.
Number 1 is designed textboxes where we provide the data source
Number 2 is the drag and dropped from our data source
Number 3 is the data grid view where the data source and member
are set above
12
DMU C# HKA
13
DMU C# HKA
14
DMU C# HKA
Exercise
10. Adding new record (already done above), Updating and Deleting,
searching records should be added on the above button.
15
DMU C# HKA
Or open you project and copy paste the Debug folder to the place you
want.
Therefore it is easy to move your database and project together. You can
easily take it to another computer without worrying about your SQL server
database.
16
DMU C# HKA
17
DMU C# HKA
4. Add a data grid view to your form. This is from your toolbox and
Data group. The data grid view is used to see the full table with its
records.
Also Add different buttons to Manipulate records (Adding, viewing,
deleting updating, searching etc..)
Also Add different Textboxes to submit text values
18
DMU C# HKA
d. After selecting SQL server and the Data provider: is SQL server
too, click OK
19
DMU C# HKA
6. While you look your server Explorer your stud database should be
populated with department Table too.
20
DMU C# HKA
7. Double click on you visual studio form and write the following. To get
your SqlConnection click on your database and copy the path from your
Connection String on the property windows (circle No 2 on the diagram
below)
Now Start your project after building the DataGridView1 should be filled
with all records from your department Table.
21
DMU C# HKA
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace connect_SQL_Server
{
public partial class Form1 : Form
{
SqlConnection sqlcn = new SqlConnection("Data Source=user;Initial
Catalog=stud;Integrated Security=True");
public Form1()
{
InitializeComponent();
}
22
DMU C# HKA
cmd.Connection = sqlcn;
cmd.ExecuteNonQuery();
sqlcn.Close();
MessageBox.Show("Record inserted");
}
private void btnDelete_Click(object sender, EventArgs e)
{
MessageBox.Show("Do you want to Delete ?", "Delete Record",
MessageBoxButtons.OKCancel);
if (true)
{
cn.Open();
SqlCommand cmd = cn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "delete from dbo.department where
dname=('"+textBox1.Text+"')";
cmd.Connection = cn;
cmd.ExecuteNonQuery();
cn.Close();
MessageBox.Show("Record deleted ");
}
else
{
MessageBox.Show("Not Deleted");
}
}
}
}
However
Whether we publish our project to be an .EXE file or
Copy the Debug folder like we did on local database we cannot
easily move this project to other machines because the database
is still attached to the SQL server. You can look these two
different folders below. One already has the database but this
project doesn’t include the database file.
23
DMU C# HKA
=/=
24