欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#連接SQL server數(shù)據(jù)庫(kù)命令的基本步驟

 更新時(shí)間:2025年07月11日 15:05:00   作者:張謹(jǐn)?shù)W  
文章講解了連接SQL Server數(shù)據(jù)庫(kù)的步驟,包括引入命名空間、構(gòu)建連接字符串、使用SqlConnection和SqlCommand執(zhí)行SQL操作,并強(qiáng)調(diào)參數(shù)化查詢、異常處理及連接池的重要性,感興趣的朋友跟隨小編一起看看吧

建議配合使用:如何下載和安裝SQL server數(shù)據(jù)庫(kù)-CSDN博客

1. 引入必要的命名空間

若要連接 SQL Server 數(shù)據(jù)庫(kù),需引入System.Data.SqlClient命名空間。

2. 構(gòu)建數(shù)據(jù)庫(kù)連接字符串

連接字符串包含數(shù)據(jù)庫(kù)服務(wù)器地址、數(shù)據(jù)庫(kù)名稱、認(rèn)證方式等連接所需信息。

就是這張圖片的內(nèi)容,里面的連接字符串

下面是幾種常見的連接字符串示例:

示例 1:使用 Windows 身份驗(yàn)證

string connectionString = "Data Source=服務(wù)器名;Initial Catalog=數(shù)據(jù)庫(kù)名;Integrated Security=True";

示例 2:使用 SQL Server 身份驗(yàn)證

要選擇混合驗(yàn)證,不然無法使用SQL Server 身份驗(yàn)證,根據(jù)自己的設(shè)置輸入

string connectionString = "Data Source=服務(wù)器名;Initial Catalog=數(shù)據(jù)庫(kù)名;User ID=用戶名;Password=密碼";

3. 連接數(shù)據(jù)庫(kù)并執(zhí)行 SQL 命令

可以借助SqlConnectionSqlCommand對(duì)象來連接數(shù)據(jù)庫(kù)并執(zhí)行 SQL 命令。以下是一個(gè)完整的示例:

using System;
using System.Data.SqlClient;
?
class Program
{
    static void Main()
    {
        // 定義連接字符串
        string connectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True";
?
        try
        {
            // 創(chuàng)建并打開數(shù)據(jù)庫(kù)連接
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                Console.WriteLine("數(shù)據(jù)庫(kù)連接成功!");
?
                // 定義SQL查詢命令
                string sql = "SELECT CustomerID, CompanyName FROM Customers";
?
                // 創(chuàng)建SqlCommand對(duì)象
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    // 執(zhí)行查詢并獲取數(shù)據(jù)讀取器
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        // 讀取查詢結(jié)果
                        while (reader.Read())
                        {
                            Console.WriteLine($"客戶ID: {reader["CustomerID"]}, 公司名稱: {reader["CompanyName"]}");
                        }
                    }
?
                    // 執(zhí)行插入命令示例
                    string insertSql = "INSERT INTO Products (ProductName, UnitPrice) VALUES (@ProductName, @UnitPrice)";
                    using (SqlCommand insertCommand = new SqlCommand(insertSql, connection))
                    {
                        // 添加參數(shù)以防止SQL注入
                        insertCommand.Parameters.AddWithValue("@ProductName", "新產(chǎn)品");
                        insertCommand.Parameters.AddWithValue("@UnitPrice", 9.99);
?
                        // 執(zhí)行非查詢命令(如INSERT、UPDATE、DELETE)
                        int rowsAffected = insertCommand.ExecuteNonQuery();
                        Console.WriteLine($"插入了{(lán)rowsAffected}行數(shù)據(jù)。");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("數(shù)據(jù)庫(kù)操作出錯(cuò): " + ex.Message);
        }
    }
}

4. 關(guān)鍵步驟說明

  • 創(chuàng)建連接對(duì)象:通過SqlConnection類創(chuàng)建數(shù)據(jù)庫(kù)連接對(duì)象,構(gòu)造函數(shù)的參數(shù)為連接字符串。

  • 打開連接:調(diào)用Open()方法開啟數(shù)據(jù)庫(kù)連接,此操作應(yīng)包含在try-catch塊中,以便捕獲可能出現(xiàn)的異常。

  • 執(zhí)行命令:利用SqlCommand類執(zhí)行 SQL 命令,可通過ExecuteReader()方法執(zhí)行查詢命令,獲取查詢結(jié)果;也可使用ExecuteNonQuery()方法執(zhí)行插入、更新、刪除等操作。

  • 關(guān)閉連接:使用using語句能確保連接資源被正確釋放,無需手動(dòng)調(diào)用Close()Dispose()方法。

5. 其他注意事項(xiàng)

  • 參數(shù)化查詢:在 SQL 命令中使用參數(shù)(如@ParameterName),可以有效防止 SQL 注入攻擊。

  • 異常處理:數(shù)據(jù)庫(kù)操作可能會(huì)因?yàn)榫W(wǎng)絡(luò)問題、權(quán)限不足等原因失敗,所以必須進(jìn)行異常處理。

  • 連接池:.NET 會(huì)自動(dòng)管理連接池,一般情況下無需手動(dòng)配置。

到此這篇關(guān)于C#連接SQL server數(shù)據(jù)庫(kù)命令的基本步驟的文章就介紹到這了,更多相關(guān)C#連接SQL server數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論