![]()
1 //连接数据库
2 string dbPath = @"Data Source=D:\sqlliteDb\Test.db;Version=3";
3 string strSql = "select * from aaa";
4 SQLiteConnection Conn = new SQLiteConnection(dbPath);
5 Conn.Open();
6
7 SQLiteDataAdapter mAdapter = new SQLiteDataAdapter(strSql, Conn);
8 DataTable rs = new DataTable();
9 mAdapter.Fill(rs);
10
11 dataGridView1.DataSource = rs;
12 Conn.Close();
1 //按字段号添加数据,这个非常有用,网上的都是很一长串添加,此方法可以单个字段添加数据。
2 string dbPath = @"Data Source=D:\sqlliteDb\Test.db;Version=3";
3 string strSql = "select * from aaa";
4 SQLiteConnection Conn = new SQLiteConnection(dbPath);
5 Conn.Open();
6
7 SQLiteDataAdapter mAdapter = new SQLiteDataAdapter(strSql, Conn);
8 SQLiteCommandBuilder builder = new SQLiteCommandBuilder(mAdapter);
9 DataTable rs = new DataTable();
10 mAdapter.Fill(rs);
11
12 if (textBox1.Text == "" || textBox2.Text == "") return;
13
14 DataRow dr = rs.NewRow();
15 rs.Rows.Add(dr);
16
17 dr["Name"] = Convert.ToString(this.textBox1.Text);
18 dr["Tel"] = Convert.ToString(this.textBox2.Text);
19
20 mAdapter.Update(rs);
21
22 Conn.Close();
1 //按条件删除行
2 string dbPath = @"Data Source=D:\sqlliteDb\Test.db;Version=3";
3 string strSql = "delete from aaa where Name='" + textBox4.Text + "'";
4 SQLiteConnection Conn = new SQLiteConnection(dbPath);
5 Conn.Open();
6
7 SQLiteCommand command = new SQLiteCommand(strSql, Conn);
8 command.ExecuteNonQuery();
9
10 Conn.Close();
1 //修改数据,相对而言,删除和修改数据比较简单。
2 string dbPath = @"Data Source=D:\sqlliteDb\Test.db;Version=3";
3 string strSql = "update aaa set Tel = " + Convert.ToString(textBox3.Text) + " where Name='" + Convert.ToString(textBox5.Text) + "'";
4 SQLiteConnection Conn = new SQLiteConnection(dbPath);
5 Conn.Open();
6
7 SQLiteCommand command = new SQLiteCommand(strSql, Conn);
8 command.ExecuteNonQuery();
9
10 Conn.Close();
1 //新建数据库
2 string dbPath = @"D:\sqlliteDb \\" + Convert.ToString(textBox7.Text) + ".db";
3 if (!File.Exists(dbPath))
4 {
5 SQLiteConnection.CreateFile(dbPath);
6 }
7 try
8 {
9 SQLiteConnection Conn = new SQLiteConnection("Data Source=" + dbPath + ";Version=3;");
10 Conn.Open();
11 DataSet rs = new DataSet();
12
13 //this.button6_Click(sender, e);//调用Click事件
14
15 dataGridView1.DataSource = rs;
16 Conn.Close();
17 }
18 catch (Exception ex)
19 {
20 throw new Exception("打开数据库:" + dbPath + "的连接失败:" + ex.Message);
21 }
1 //新建表
2 try
3 {
4 string dbPath = @"Data Source=D:\sqlliteDb\123.db;Version=3";
5 string strSql = "create table " + Convert.ToString(textBox8.Text) + " (Id text(255) not null)";//primary key 主键
6 SQLiteConnection Conn = new SQLiteConnection(dbPath);
7 Conn.Open();
8
9 SQLiteCommand command = new SQLiteCommand(strSql, Conn);
10 command.ExecuteNonQuery();
11 Conn.Close();
12 }
13 catch (Exception ex)
14 {
15 throw new Exception("创建数据表" + textBox2.Text + "失败:" + ex.Message);
16 }
1 //新建字段
2 try
3 {
4 string dbPath = @"Data Source=D:\sqlliteDb\123.db;Version=3";
5 string strSql = "ALTER TABLE " + Convert.ToString(textBox9.Text) + " ADD COLUMN 生产单号 Text(20)";//插入新的字段
6 SQLiteConnection Conn = new SQLiteConnection(dbPath);
7 Conn.Open();
8
9 SQLiteCommand command = new SQLiteCommand(strSql, Conn);
10 command.ExecuteNonQuery();
11 Conn.Close();
12 }
13 catch (Exception ex)
14 {
15 throw new Exception("创建数据表" + textBox2.Text + "失败:" + ex.Message);
16 }