關閉。此問題不符合Stack Overflow 準則。它目前不接受答案。
想改善這個問題嗎?更新問題,使其成為 Stack Overflow的主題。
6 天前關閉。
改進這個問題題目:模擬一個資料庫管理系統的資料結構和演算法請求:
- 資料存盤
- 查詢:支持選擇、插入、更新查詢
我打算在winform上做,有創建表,選擇,洗掉,更新命令,就像sql一樣。但我不知道如何用 C# 撰寫這些函式。有沒有人有關于 SQL 的那些基本功能的檔案?請幫我。任何了解此主題的人都可以逐步指導我嗎?我真的不知道該怎么辦
uj5u.com熱心網友回復:
在現代編程中,傾向于將資料(= 模型)與資料的顯示方式(= 視圖)分開。要將視圖連接到模型,我們需要一個配接器類:視圖模型。這三個類一起縮寫為 MVVM。考慮閱讀有關此內容的內容。
模型和視圖分離的好處是,你可以將模型重用于其他視圖,你可以改變模型而不改變視圖,或者改變視圖而不改變模型。最后,在不需要表單的情況下對模型類進行單元測驗要容易得多。
我提到 MVVM,因為您嘗試將資料庫處理與 WinForms 混合使用。資料庫處理(模型)與您如何顯示獲取的資料無關。
回到你的問題
顯然,您顯示獲取的資料沒有問題(還)。因此,讓我們關注如何在 C# 中獲取資料庫資料。
有兩種主要方法:一種是通過 DbConnection 和 DbCommand 使用 SQL。另一種常用的方法是使用實??體框架和 LINQ。
您提到您已經擁有用于獲取資料的 SQL。所以我將重點介紹該方法。如果您對物體框架感興趣,可以考慮先閱讀物體框架代碼并在電視上沒有任何內容時進行一些試驗。
您需要一種創建表的方法。由于表是一起作業的,我的建議是不要創建一種創建一個表的方法,而是創建一種在一次呼叫中創建資料庫所有表的方法。
public void CreateAllTables()
{
const string sqlText = "Create if not exists TableCustomers (
Id int,
FirstName Varchar(30),
...
確切的語法在一定程度上取決于您使用的 SQL 方言:MicroSoft?資料庫?簡單的SQL?好吧,我認為您比我更了解 Sql 文本。關于如何將其發送到資料庫:
using (var dbConnection = new DbConnection())
{
using (DbCommand dbCommand = dbConnection.CreateCommand())
{
dbCommand.CommandText = sqlText;
現在有幾種方法可以執行 dbCommand:
- 檢索專案序列:
DbCommand.ExecuteReader - 只檢索一個物件:
DbCommand.ExecuteScalar - 只執行,不回傳資料:
DbCommand.ExecuteNonQuery
在這種情況下:我們只執行命令。在與資料庫通信之前,我們必須打開 dbConnection:
dbConnection.Open();
dbCommand.ExecuteNonQuery();
}
}
}
DbCommand.ExecuteNonQuery有一個回傳值:受影響的行數。有時檢查它以查看是否發生錯誤可能很方便。
現在獲取一些資料
public IEnumerable<Product> FetchStudentsByLastName(string lastName)
{
const string sqlText = "Select Id, FirstName, LastName, ...
From tableStudents
Where LastName = @LastName;"
請注意,我的 SQL 具有@LastName作為引數!
開頭與 CreateTables 中的相同:
using (var dbConnection = new DbConnection())
{
using (DbCommand dbCommand = dbConnection.CreateCommand())
{
dbCommand.CommandText = sqlText;
填寫引數:
// associate the parameter in the SQL text with the value:
dbCommand.AddWithValue("@LastName", lastName);
現在我們準備打開 dbConnection 并執行查詢。我們希望獲取多行,所以我們必須使用DbCommand.ExecuteReader:
dbConnection.Open();
using (DbDataReader dbReader = dbCommand.ExecuteReader())
{
The dbDataReader contains the fetched data. use DbDataReader.Read to find out if there are still rows to read. If so, use GetInt32, GetString etc to read the cells in each row:
while (dbReader.Read())
{
Apparently there is a row. We selected Id as column 0, FirstName as column 1, LastName as column 2, etc:
Student fetchedStudent = new Student
{
Id = dbReader.GetInt32(0), // column 0 is an Int32
FirstName = dbReader.GetString(1), // column 1 is a string
LastName = dbReader.GetString(2),
...
}
yield return fetchedStudent;
}
}
}
}
To add a student we do someting similar: Create the DbConnection and the DbCommand. Fill the DbCommand with the SQL text and AddWithParameter.
The difference is: we don't expect several rows as return, we only want the Id of the added Student.
How to return the Id in the command depends on the SQL dialect that you are using. In SQLLight it would be something like this:
const string sqlText = "INSERT INTO tableStudent (
FirstName, LastName, ...) Values(@FirstName, @LastName, ...);
SELECT last_insert_rowid();"
After adding the new student, this SQL text will return the Id of the student.
Create the DbConnection and the DbCommand. Then:
dbCommand.CommandText = sqlText;
dbCommand.AddWithValue("@FirstName, firstName);
dbCommand.AddWithValue("@LastName, lastName);
...
Open the database, and execute the query with only one result: ExecuteScalar, return the returned Id as an int:
dbConnection.Open();
return (int)dbCommand.ExecuteScalar();
Resume
I've shown you how to communicate with a database using DbConnection and DbCommand.
I showed how to add parameters to your SQL text.
To fetch rows of data use ExecuteReader and Read row by row, using DbReader.Read, and DbReader.GetString(colum number) etc.
I showed how to execute without returning value: DbCommand.ExecuteNonQuery. Used in create table, delete table, update row in table, delete row from table.
And finally, I showed how to return one value: DbCommand.ExecuteScalar. Used to return the Id when adding a new row.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/332914.html
