小型企業(yè)網(wǎng)站建設(shè)報(bào)告/北京seo排名服務(wù)
SQLite數(shù)據(jù)庫(kù),小巧但功能強(qiáng)大;并且是基于文件型的數(shù)據(jù)庫(kù),驅(qū)動(dòng)庫(kù)就是一個(gè)dll文件,有些開(kāi)發(fā)工具
甚至不需要帶這個(gè)dll,比如用Delphi開(kāi)發(fā),用一些三方組件;數(shù)據(jù)庫(kù)也是一個(gè)文件,雖然是個(gè)文件,但卻
具有關(guān)系型數(shù)據(jù)庫(kù)的大多數(shù)特征,查詢語(yǔ)句也是長(zhǎng)得基本一樣,所以對(duì)應(yīng)學(xué)習(xí)數(shù)據(jù)庫(kù)操作很方便。
比起前微軟的Access數(shù)據(jù)庫(kù)那只能說(shuō),SQLite強(qiáng)大的太多。
在 Visual Studio 中,可以通過(guò)以下步驟安裝:
打開(kāi) Visual Studio,點(diǎn)擊 "工具" -> "NuGet 包管理器" -> "管理解決方案的 NuGet 包" - 在搜索框中輸入 "SQLite",然后安裝 "System.Data.SQLite" 包。
打開(kāi)VS,新建一個(gè).NET項(xiàng)目,選擇 C# Windows 桌面,Windows窗體應(yīng)用(.NET Framework)
界面上拖放相應(yīng)控件
關(guān)鍵事件代碼 如下
private void button8_Click(object sender, EventArgs e){string dataSource = "test.db"; // 數(shù)據(jù)庫(kù)文件名// 連接數(shù)據(jù)庫(kù)using (SQLiteConnection connection = new SQLiteConnection($"Data Source={dataSource};Version=3;")){connection.Open(); // 打開(kāi)連接// 創(chuàng)建表string createTableQuery = "CREATE TABLE IF NOT EXISTS Customers (Id INTEGER PRIMARY KEY, Name TEXT, Age INT,Phone TEXT)";using (SQLiteCommand createTableCommand = new SQLiteCommand(createTableQuery, connection)){createTableCommand.ExecuteNonQuery(); // 執(zhí)行創(chuàng)建表的SQL語(yǔ)句}// 插入數(shù)據(jù)string insertDataQuery = "INSERT INTO Customers (Name, Age, Phone) VALUES (@Name, @Age, @Phone)";using (SQLiteCommand insertDataCommand = new SQLiteCommand(insertDataQuery, connection)){insertDataCommand.Parameters.AddWithValue("@Name", "John"); // 設(shè)置參數(shù)值,避免SQL注入insertDataCommand.Parameters.AddWithValue("@Age", 25);insertDataCommand.Parameters.AddWithValue("@Phone", "123456");insertDataCommand.ExecuteNonQuery(); // 執(zhí)行插入數(shù)據(jù)的SQL語(yǔ)句}// 查詢數(shù)據(jù)string selectDataQuery = "SELECT * FROM Customers";using (SQLiteCommand selectDataCommand = new SQLiteCommand(selectDataQuery, connection)){using (SQLiteDataReader reader = selectDataCommand.ExecuteReader()){while (reader.Read()){int id = Convert.ToInt32(reader["Id"]); string name = Convert.ToString(reader["Name"]);int age = Convert.ToInt32(reader["Age"]);string phone = Convert.ToString(reader["Phone"]);Console.WriteLine($"ID: {id}, Name: {name}, Age: {age}, Phone: {phone}");}}}SQLiteCommand sqlCommand = new SQLiteCommand("select * from Customers", connection);sqlCommand.ExecuteNonQuery();DataTable dataTable = new DataTable("Customers");SQLiteDataAdapter sqlAdapter = new SQLiteDataAdapter(sqlCommand);sqlAdapter.Fill(dataTable);dataGridView1.DataSource = dataTable.DefaultView;sqlAdapter.Update(dataTable);}}
程序運(yùn)行結(jié)果