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

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

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

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

1. 引入必要的命名空間

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

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

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

就是這張圖片的內容,里面的連接字符串

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

示例 1:使用 Windows 身份驗證

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

示例 2:使用 SQL Server 身份驗證

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

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

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

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

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ù)庫連接
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                Console.WriteLine("數(shù)據(jù)庫連接成功!");
?
                // 定義SQL查詢命令
                string sql = "SELECT CustomerID, CompanyName FROM Customers";
?
                // 創(chuàng)建SqlCommand對象
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    // 執(zhí)行查詢并獲取數(shù)據(jù)讀取器
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        // 讀取查詢結果
                        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", "新產品");
                        insertCommand.Parameters.AddWithValue("@UnitPrice", 9.99);
?
                        // 執(zhí)行非查詢命令(如INSERT、UPDATE、DELETE)
                        int rowsAffected = insertCommand.ExecuteNonQuery();
                        Console.WriteLine($"插入了{rowsAffected}行數(shù)據(jù)。");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("數(shù)據(jù)庫操作出錯: " + ex.Message);
        }
    }
}

4. 關鍵步驟說明

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

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

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

  • 關閉連接:使用using語句能確保連接資源被正確釋放,無需手動調用Close()Dispose()方法。

5. 其他注意事項

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

  • 異常處理:數(shù)據(jù)庫操作可能會因為網(wǎng)絡問題、權限不足等原因失敗,所以必須進行異常處理。

  • 連接池:.NET 會自動管理連接池,一般情況下無需手動配置。

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

相關文章

最新評論