How to Connect SQL Server Database With C#

In this CSHARP (C #) tutorial, I will discuss the Basic of database-based programming, namely the Easy Way to Connect SQL Server Database with C #.

As I explained above, the basis for creating a database-based application or program is that we must be able to make a database connection with the language we use, the example below is I will use the C # database, and after you have successfully created a database connection then you can continue with Input, Edit and Delete data into the database.


To be able to follow the tutorial below, you must have installed it.
1. SQL Server database
2. Visual Studio in which there is C #

If so, you can follow the steps below.
On this occasion, I used Visual Studio 2010 Ultimate Version

1. Creating Databases and Tables With SQL Server

Before creating forms and coding in C #, you must create a database first, because later the database will be connected to the C # language
Create a database with the name DB_JNM on SQL Server
Then create a table with the name: TBL_BARANG
The TBL_BARANG design is like the image below:



If you have created a Database, Table and Entered Sample data in TBL_BARANG
Then you can continue in stage 2, which is to create a project with C # below

2. Creating a Project and Creating a Form 1

Please Click Start On Your Computer
Search for Visual Studio
Click File - New - Project
Cara Koneksi Database SQL Server Dengan C# - Belajar C SHARP
See the picture above, make sure you select Visual C # - Windows Form Application
Then enter the data below:
Name: JNM application
Location: D: \
Solution Name: JNM Application

If you have click OK
If so, Form1 will appear, for the name of the form, it doesn't need to be changed, just Form1
Then input DataGridView on Form1
The results are as follows:
Cara Koneksi Database SQL Server Dengan C# - Belajar C SHARP
If you have made it like the image above, please enter the coding in Form1

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace AplikasiJNM
{
public partial class Form1 : Form
{
SqlConnection SqlConnection;
string ConnectionString;
public Form1()
{
InitializeComponent();
ConnectionString = @"Server = .\;" +
"Database = DB_JNM; integrated security = true";
SqlConnection = new SqlConnection(ConnectionString);
//Menampilkan Data Kedalam Datagrid
SqlCommand command = new SqlCommand("Select * From TBL_BARANG", SqlConnection);

SqlConnection.Open();
SqlDataReader reader = command.ExecuteReader();
DataTable MyTable = new DataTable();

MyTable.Load(reader);
SqlConnection.Close();
dataGridView1.DataSource = MyTable;
}
}
}


Finally, please press F5 or click Run so that your C # project is Running.
The Data Grid should appear from the TBL_BARANG table as shown below.

Cara Koneksi Database SQL Server Dengan C# - Belajar C SHARP

This is the way to connect SQL Server database with C # (CSHARP), hope it is useful for you :)

Apart from being a programming information medium, we also share articles related to Android trick tips.

Subscribe to receive free email updates:

0 Response to "How to Connect SQL Server Database With C#"

Post a Comment