您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

C# SQLite3增删改查/新建数据库/新建表/新建字段

bubuko 2022/1/25 19:01:15 dotnet 字数 21290 阅读 1258 来源 http://www.bubuko.com/infolist-5-1.html

1 //连接数据库 2 string dbPath = @"Data Source=D:\sqlliteDb\Test.db;Version=3"; 3 string strSql = "select * from aaa"; 4 SQLiteConnection Conn = new SQLite ...

技术分享图片

 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             }

 

C# SQLite3增删改查/新建数据库/新建表/新建字段

原文:https://www.cnblogs.com/Fpack/p/14966894.html


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶