This article illustrates the method of C # operation SQLite database. Share to everyone for your reference, specific as follows:
This shows the reading and writing of the database and displaying its data in the form, in the following ways:
Read:
Database (SQLite)-> DataAdapter-> DataSet-> DataGridView
Write:
Database (SQLite) <-DataAdapter <-DataSet <-DataGridView
1, assuming the existing database table student, its fields are as follows:
ID (self-added field, primary key) |
Number |
Name |
Grade |
1 |
20120001 |
Jackey |
1 |
2. Datagrideview Controls and DataSet controls
Drag and drop a Datagrideview control on the form (note: You do not need to specify a data source (DataSource), but only the DataSource member amplitude of the DataGridView object in your code) , and then drag and drop a DataSet control (this control is not displayed on the form).
3, read and displayed in the Datagrideview
Mdbconn = new Sqliteconnection ("Data source=sqlite.student.db");
Mdbconn.open ();
DataAdapter = new Sqlitedataadapter ("SELECT * from Student;", mdbconn)//Read database
Dataadapter.fillschema (DataSet1, SchemaType.Source, "student")//fills the schema information for the database table student (this is the primary KEY constraint) into the DATASET1 student table DataAdapter.Fill
(DataSet1, " Student "); Fill the dataset control
Datagridview1.datasource = dataset1.tables[" table "];//note that the data tables in the dataset are table in turn, Table1 , Table2
... Mdbconn.close ();
Attention:
Dataadapter.fillschema (DataSet1, SchemaType.Source, "student");
Populates the schema information for the database table student (at this time the primary KEY constraint) into the DATASET1 student table
4, write and update the Datagrideview
Mdbconn.open ();
DataRow DataRow = dataset1.tables["Student"]. NewRow ();
datarow["number"] = "20120010";
datarow["name"] = "dick";
Datarow["grade"] = "2";
dataset1.tables["Table"]. Rows.Add (DataRow);
Datagridview1.invalidate ()//real-time update dataGridView1
dataadapter.insertcommand = new Sqlitecommand ("INSERT into Student (number, name, grade) VALUES (' "+ datarow[" number "] +" ', ' "+ datarow[" name "+" ', ' "+ datarow[" grade "] +") ", MD Bconn);
DataAdapter.Update (DataSet1, "student");
Mdbconn.close ();
Parameter literature
Http://msdn.microsoft.com/zh-cn/library/49z48hxc (v=vs.90). aspx
Http://msdn.microsoft.com/zh-cn/library/879f39d8 (v=vs.80). aspx
Http://msdn.microsoft.com/zh-cn/library/879f39d8 (v=vs.100). aspx
Read more about C # Interested readers can view the site topics: "C # Programming Thread Usage Tips summary", "C # Operation Excel Skills Summary", "C # XML file Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # tutorial on data structure and algorithms, summary of C # array manipulation techniques, and an introductory course on C # object-oriented programming
I hope this article will help you with C # programming.