要與SQL Server連接,必須將SQL Server安裝在系統(tǒng)中?,F(xiàn)在使用Microsoft SQL Server管理工具來連接SQL Server??梢允褂眠@個工具來處理數(shù)據(jù)庫。現(xiàn)在,按照以下步驟連接SQL Server。
第1步:打開Microsoft SQL Server管理工具,它會提示進行數(shù)據(jù)庫連接,提供服務器名稱和身份驗證。如下所示 -

連接成功后,顯示如下窗口。如下所示 -

第2步:創(chuàng)建數(shù)據(jù)庫
現(xiàn)在,通過選擇數(shù)據(jù)庫選項創(chuàng)建數(shù)據(jù)庫,然后右鍵單擊它。 它彈出一個選項菜單,并提供了幾個選項。

點擊新建數(shù)據(jù)庫,然后它要求填寫要創(chuàng)建數(shù)據(jù)庫名稱。 在這里,我們創(chuàng)建了一個名稱為:student 的數(shù)據(jù)庫。如下圖所示 -

點擊確定(Ok)按鈕,它會創(chuàng)建一個數(shù)據(jù)庫,可以在下面的截圖中的左側窗口看到如下所示。

第3步:建立連接并創(chuàng)建一個表
現(xiàn)在創(chuàng)建數(shù)據(jù)庫之后,使用下面的C#代碼創(chuàng)建一個表。在這個源代碼中,使用創(chuàng)建的學生數(shù)據(jù)庫進行連接。
在Visual Studio 2017中,我們創(chuàng)建了一個包含以下C#代碼的.NET控制臺應用程序項目(名稱:AdoNetConsoleApplication) -

參考以下實現(xiàn)代碼(Programe.cs) -
using System;
using System.Data.SqlClient;
namespace Programe
{
class Program
{
static void Main(string[] args)
{
new Program().CreateTable();
}
public void CreateTable()
{
SqlConnection con = null;
try
{
// Creating Connection
con = new SqlConnection("data source=.; database=student; integrated security=SSPI");
// writing sql query
SqlCommand cm = new SqlCommand("create table student_info(id int not null,name varchar(100), email varchar(50), join_date date)", con);
// Opening Connection
con.Open();
// Executing the SQL query
cm.ExecuteNonQuery();
// Displaying a message
Console.WriteLine("Table created Successfully");
}
catch (Exception e)
{
Console.WriteLine("OOPs, something went wrong." + e);
}
// Closing the connection
finally
{
con.Close();
}
}
}
}
使用Ctrl + F5 執(zhí)行此代碼。執(zhí)行完成后,它將顯示一條消息給控制臺,如下所示 -

也可以在Microsoft SQL Server Management Studio中看到創(chuàng)建的表。如下所示所示創(chuàng)建成功的表:student_info。
可以看到,在這里有一張表:student_info。但此時,這張表是空的,所以需要插入一些數(shù)據(jù)。
第4步: 將數(shù)據(jù)插入到表中,參考以下實現(xiàn)代碼(AdoNetInsert.cs) -
using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication
{
class AdoNetInsert
{
static void Main(string[] args)
{
new AdoNetInsert().InsertTable();
}
public void InsertTable()
{
SqlConnection con = null;
try
{
// Creating Connection
con = new SqlConnection("data source=.; database=student; integrated security=SSPI");
// writing sql query
String sql = "insert into student_info(id, name, email, join_date)values('101', 'Hiniu Su', 'hinew.su@example.com', '2017-11-18')";
SqlCommand cm = new SqlCommand(sql, con);
// Opening Connection
con.Open();
// Executing the SQL query
cm.ExecuteNonQuery();
// Displaying a message
Console.WriteLine("插入數(shù)據(jù)記錄成功~!");
}
catch (Exception e)
{
Console.WriteLine("OOPs, something went wrong." + e);
}
// Closing the connection
finally
{
con.Close();
}
}
}
}
使用Ctrl + F5 執(zhí)行此代碼。 執(zhí)行完成后,它將顯示一條消息給控制臺,如下所示 -

第5步:查詢/檢索記錄
在這里,將查詢/檢索上一步中插入的數(shù)據(jù)。參考下面的C#實現(xiàn)代碼(AdoNetSelect.cs)-
using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication
{
class AdoNetSelect
{
static void Main(string[] args)
{
new AdoNetSelect().SelectTable();
}
public void SelectTable()
{
SqlConnection con = null;
try
{
// Creating Connection
con = new SqlConnection("data source=.; database=student; integrated security=SSPI");
// writing sql query
SqlCommand cm = new SqlCommand("SELECT * FROM student_info", con);
// Opening Connection
con.Open();
// Executing the SQL query
SqlDataReader sdr = cm.ExecuteReader();
Console.WriteLine("當前 student_info 表中的記錄為:" );
// Iterating Data
while (sdr.Read())
{
Console.WriteLine(sdr["id"] + " " + sdr["name"] + " " + sdr["email"]); // Displaying Record
}
}
catch (Exception e)
{
Console.WriteLine("OOPs, something went wrong.\n" + e);
}
// Closing the connection
finally
{
con.Close();
}
}
}
}
通過Ctrl + F5 執(zhí)行此代碼,它將產生以下結果。這顯示兩條記錄,一條是手動插入的數(shù)據(jù)記錄。輸出結果如下圖所示:

第6步: 刪除記錄
經過前面5步,現(xiàn)在student_info表中有兩個記錄。以下C#代碼從表中刪除一行。參考代碼 (AdoNetDelete.cs)-
using System;
using System.Collections.Generic;
using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication
{
class AdoNetDelete
{
static void Main(string[] args)
{
new AdoNetDelete().DeleteFromTable();
}
public void DeleteFromTable()
{
SqlConnection con = null;
try
{
// Creating Connection
con = new SqlConnection("data source=.; database=student; integrated security=SSPI");
// writing sql query
SqlCommand cm = new SqlCommand("delete from student_info where id = '101'", con);
// Opening Connection
con.Open();
// Executing the SQL query
cm.ExecuteNonQuery();
Console.WriteLine("已經成功地刪除了編號為:101 的學生數(shù)據(jù)信息~!");
// 重新查詢數(shù)據(jù)庫中的記錄信息
SqlCommand cm2 = new SqlCommand("SELECT * FROM student_info", con);
// Executing the SQL query
SqlDataReader sdr = cm2.ExecuteReader();
Console.WriteLine("當前 student_info 表中的記錄為:");
// Iterating Data
while (sdr.Read())
{
Console.WriteLine(sdr["id"] + " " + sdr["name"] + " " + sdr["email"]); // Displaying Record
}
}
catch (Exception e)
{
Console.WriteLine("OOPs, something went wrong.\n" + e);
}
// Closing the connection
finally
{
con.Close();
}
}
}
}
通過Ctrl + F5 執(zhí)行此代碼,它將產生以下結果(ID是111的這一條已經被刪除了,這里只顯示編號為102的記錄信息)。
