前幾天一個學員在學習C#與數(shù)據(jù)庫交互時,也不知道數(shù)據(jù)庫可以用來做什么 。下面我們就詳細講講C# 和數(shù)據(jù)庫交互的相關(guān)知識。
C#是一種面向?qū)ο蟮木幊陶Z言,它可以與各種類型的數(shù)據(jù)庫進行交互,包括關(guān)系型數(shù)據(jù)庫和非關(guān)系型數(shù)據(jù)庫。在本文中,我們將介紹如何使用C#與數(shù)據(jù)庫進行讀取交互。
一、連接數(shù)據(jù)庫
在使用C#與數(shù)據(jù)庫進行交互之前,我們需要先連接到數(shù)據(jù)庫。對于關(guān)系型數(shù)據(jù)庫,我們可以使用ADO.NET提供的類庫來連接到數(shù)據(jù)庫。以下是一個示例,演示了如何連接到Microsoft SQL Server數(shù)據(jù)庫:
using System.Data.SqlClient;
public void ConnectToSqlServer()
string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
// Do something with the connection...
connection.Close();
在上面的代碼中,我們創(chuàng)建了一個SqlConnection對象,并使用Open方法打開了與數(shù)據(jù)庫的連接。接著,我們可以執(zhí)行SQL查詢語句或其他操作,并在最后使用Close方法關(guān)閉連接。
對于非關(guān)系型數(shù)據(jù)庫,我們需要使用相應的客戶端庫來連接到數(shù)據(jù)庫。例如,如果我們要連接到MongoDB數(shù)據(jù)庫,我們可以使用MongoDB.Driver NuGet包提供的類庫。以下是一個示例:
using MongoDB.Driver;
public void ConnectToMongoDB()
string connectionString = "mongodb://localhost:27017";
MongoClient client = new MongoClient(connectionString);
IMongoDatabase database = client.GetDatabase("myDatabase");
// Do something with the database...
在上面的代碼中,我們創(chuàng)建了一個MongoClient對象,并使用GetDatabase方法獲取了指定名稱的數(shù)據(jù)庫。接著,我們可以執(zhí)行MongoDB查詢語句或其他操作。
二、讀取數(shù)據(jù)
在連接到數(shù)據(jù)庫之后,我們可以使用C#來讀取數(shù)據(jù)庫中的數(shù)據(jù)。以下是一個示例,演示了如何從Microsoft SQL Server數(shù)據(jù)庫中讀取數(shù)據(jù):
using System.Data.SqlClient;
public void ReadDataFromSqlServer()
string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
string query = "SELECT * FROM myTable";
SqlCommand command = new SqlCommand(query, connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
string column1Value = reader.GetString(0);
int column2Value = reader.GetInt32(1);
// Do something with the data...
reader.Close();
connection.Close();
在上面的代碼中,我們創(chuàng)建了一個SqlCommand對象,并使用ExecuteReader方法執(zhí)行了指定的SQL查詢語句。接著,我們使用Read方法遍歷了查詢結(jié)果集,并使用GetString和GetInt32方法讀取了每列的值。最后,我們關(guān)閉了SqlDataReader對象和SqlConnection對象。
對于MongoDB數(shù)據(jù)庫,我們需要使用MongoDB.Driver NuGet包提供的類庫來讀取數(shù)據(jù)。以下是一個示例:
using MongoDB.Bson;
using MongoDB.Driver;
public void ReadDataFromMongoDB()
MongoClient client = new MongoClient("mongodb://localhost:27017");
IMongoDatabase database = client.GetDatabase("myDatabase");
IMongoCollection collection = database.GetCollection("myCollection");
var filter = Builders.Filter.Empty;
var cursor = collection.Find(filter);
foreach (var document in cursor.ToEnumerable())
string column1Value = document.GetValue("column1").AsString;
int column2Value = document.GetValue("column2").AsInt32;
// Do something with the data...
在上面的代碼中,我們創(chuàng)建了一個IMongoCollection對象,并使用Find方法執(zhí)行了指定的查詢。接著,我們使用foreach循環(huán)遍歷查詢結(jié)果集,并使用GetValue方法讀取了每列的值。
三、寫入數(shù)據(jù)
除了讀取數(shù)據(jù),C#還可以用于向數(shù)據(jù)庫中寫入數(shù)據(jù)。以下是一個示例,演示了如何向Microsoft SQL Server數(shù)據(jù)庫中寫入數(shù)據(jù):
using System.Data.SqlClient;
public void WriteDataToSqlServer()
string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
string query = "INSERT INTO myTable (column1, column2) VALUES (@value1, @value2)";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@value1", "some value");
command.Parameters.AddWithValue("@value2", 123);
command.ExecuteNonQuery();
connection.Close();
在上面的代碼中,我們創(chuàng)建了一個SqlCommand對象,并使用AddWithValue方法設(shè)置了兩個參數(shù)的值。接著,我們使用ExecuteNonQuery方法執(zhí)行了指定的SQL查詢語句,并將新數(shù)據(jù)插入到數(shù)據(jù)庫中。
對于MongoDB數(shù)據(jù)庫,我們可以使用IMongoCollection對象提供的InsertOne和InsertMany方法來寫入數(shù)據(jù)。以下是一個示例:
using MongoDB.Bson;
using MongoDB.Driver;
public void WriteDataToMongoDB()
MongoClient client = new MongoClient("mongodb://localhost:27017");
IMongoDatabase database = client.GetDatabase("myDatabase");
IMongoCollection collection = database.GetCollection("myCollection");
var document = new BsonDocument
{ "column1", "some value" },
{ "column2", 123 }
collection.InsertOne(document);
在上面的代碼中,我們創(chuàng)建了一個BsonDocument對象,并設(shè)置了兩個字段的值。接著,我們使用InsertOne方法將新數(shù)據(jù)插入到指定的集合中。
。。
部分項目圖片:
總結(jié)
本文介紹了如何使用C#與數(shù)據(jù)庫進行讀寫交互。我們可以使用ADO.NET提供的類庫連接到關(guān)系型數(shù)據(jù)庫,也可以使用第三方庫連接到非關(guān)系型數(shù)據(jù)庫。無論是使用哪種方法,我們都需要注意數(shù)據(jù)庫的類型和版本,以確保讀寫操作的正確性。。
特別聲明:以上內(nèi)容(如有圖片或視頻亦包括在內(nèi))為自媒體平臺“網(wǎng)易號”用戶上傳并發(fā)布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.