網(wǎng)站服務器在那里找/優(yōu)秀軟文范例200字
SQLite數(shù)據(jù)庫,小巧但功能強大;并且是基于文件型的數(shù)據(jù)庫,驅(qū)動庫就是一個dll文件,有些開發(fā)工具
甚至不需要帶這個dll,比如用Delphi開發(fā),用一些三方組件;數(shù)據(jù)庫也是一個文件,雖然是個文件,但卻
具有關(guān)系型數(shù)據(jù)庫的大多數(shù)特征,查詢語句也是長得基本一樣,所以對應學習數(shù)據(jù)庫操作很方便。
比起前微軟的Access數(shù)據(jù)庫那只能說,SQLite強大的太多。
在 Visual Studio 中,可以通過以下步驟安裝:
打開 Visual Studio,點擊 "工具" -> "NuGet 包管理器" -> "管理解決方案的 NuGet 包" - 在搜索框中輸入 "SQLite",然后安裝 "System.Data.SQLite" 包。
打開VS,新建一個.NET項目,選擇 C# Windows 桌面,Windows窗體應用(.NET Framework)
界面上拖放相應控件
關(guān)鍵事件代碼 如下
private void button8_Click(object sender, EventArgs e){string dataSource = "test.db"; // 數(shù)據(jù)庫文件名// 連接數(shù)據(jù)庫using (SQLiteConnection connection = new SQLiteConnection($"Data Source={dataSource};Version=3;")){connection.Open(); // 打開連接// 創(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語句}// 插入數(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語句}// 查詢數(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);}}
程序運行結(jié)果