C # methods to manipulate SQLite database read-write database _c# tutorial

Source: Internet
Author: User
Tags sqlite sqlite database

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.