SoFunction
Updated on 2025-03-07

C# How to read and write databases in SQLite database

This article describes the method of C# operating the SQLite database to read and write the database. Share it for your reference, as follows:

Here is a demonstration of reading and writing a database and displaying its data in a form (Form), in the following way:

read:

Database(SQLite) -> DataAdapter -> DataSet -> DataGridView

Write:

Database(SQLite) <- DataAdapter <- DataSet <- DataGridView

1. Assume that the existing database table student has the fields as follows:

ID (self-increment field, primary key)

number

name

grade

1

20120001

jackey

1

2. DataGrideView control and DataSet control

Drag and drop a DataGrideView control on the Form (Note: you do not need to specify the data source (DataSource), but you only need to use the DataSource member magnitude of the DataGridView object in the code); then drag and drop a DataSet control (this control is not displayed on the form).

3. Read and display it in DataGrideView

mDbConn = new SQLiteConnection("Data Source=");
();
dataAdapter = new SQLiteDataAdapter("SELECT * FROM student;", mDbConn);//Read the database(dataSet1, , "student");//Fill the schema information of the database table student (this is the primary key constraint) into the student table of dataSet1(dataSet1, "student");//Fill the DataSet control = ["Table"];//Note that the data tables in the DataSet are Table, Table1, Table2...();

Notice:

(dataSet1, , "student");
Populate the schema information of the database table student (this is the primary key constraint) into the student table of dataSet1

4. Write and update the DataGrideView

();
DataRow dataRow = ["student"].NewRow();
dataRow["number"] = "20120010";
dataRow["name"] = "Li Si";
dataRow["grade"] = "2";
["Table"].(dataRow);
();//Real-time update of dataGridView1 = new SQLiteCommand("INSERT INTO student(number, name, grade) VALUES('" + dataRow["number"] + "','" + dataRow["name"] + "','" + dataRow["grade"] + "')", mDbConn);
(dataSet1, "student"");
();

Parameter literature

/zh-cn/library/49z48hxc(v=vs.90).aspx
/zh-cn/library/879f39d8(v=vs.80).aspx
/zh-cn/library/879f39d8(v=vs.100).aspx

For more information about C# related content, please check out the topic of this site:Summary of thread usage techniques for C# programming》、《Summary of C# operation skills》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial

I hope this article will be helpful to everyone's C# programming.