The previous section, we are ready to a database file, now let's do a variety of packaged Microsoft Entity Framework,
The method they used based on database CRUD.
Preparation:
Engineering a new console, a section on the copy of the database to the project directory, copy local set to true,
The directory structure is substantially a long way:
Then add a nuget package, using various methods c # provided behind aspects:
Substantially common operation, the check data is a class, a class CRUD
Look at how search data:
// Query data using (var connection = new SQLiteConnection("data source=Student.db")) { connection.Open(); var command = new SQLiteCommand("select * from StudentInformation", connection); var adapter = new SQLiteDataAdapter(command); was dataset = new DataSet (); adapter.Fill(dataSet); var table = dataSet.Tables[0]; }
Show results:
The remaining additions and deletions to the principles are the same, are written sql statement, and then execute using the command above ExecuteNonQuery method
Increased data
// Add data using (var connection = new SQLiteConnection("data source=Student.db")) { connection.Open(); var command = new SQLiteCommand("insert into StudentInformation " + "values(\"王五\",22,\"陕西省西安市长安区\",\"看书,听音乐\",3)", connection); var result = command.ExecuteNonQuery(); }
效果:
执行前:
执行后:
删除数据
// 删除数据 using (var connection = new SQLiteConnection("data source=Student.db")) { connection.Open(); var command = new SQLiteCommand("delete from StudentInformation where Id = 2", connection); var result = command.ExecuteNonQuery(); }
效果:
执行前:
执行后:
修改数据
// 修改数据 using (var connection = new SQLiteConnection("data source=Student.db")) { connection.Open(); var command = new SQLiteCommand("update StudentInformation set Name = '张三New' where Id = 2", connection); var result = command.ExecuteNonQuery(); }
效果:
执行前:
执行后:
到此为止,我们已经能通过c#提供的方法 使用sql语句,对数据库文件进行增删改查了。