C# SQLite3增删改查/新建数据库/新建表/新建字段
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C# SQLite3增删改查/新建数据库/新建表/新建字段,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3324字,纯文字阅读大概需要5分钟。
内容图文
1 // 连接数据库 2 string dbPath = @"Data Source=D:\sqlliteDb\Test.db;Version=3"; 3string 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); 1011 dataGridView1.DataSource = rs; 12 Conn.Close();
1 // 按字段号添加数据,这个非常有用,网上的都是很一长串添加,此方法可以单个字段添加数据。 2 string dbPath = @"Data Source=D:\sqlliteDb\Test.db;Version=3"; 3string 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); 1112if (textBox1.Text == "" || textBox2.Text == "") return; 1314 DataRow dr = rs.NewRow(); 15 rs.Rows.Add(dr); 1617 dr["Name"] = Convert.ToString(this.textBox1.Text); 18 dr["Tel"] = Convert.ToString(this.textBox2.Text); 1920 mAdapter.Update(rs); 2122 Conn.Close();
1 // 按条件删除行 2 string dbPath = @"Data Source=D:\sqlliteDb\Test.db;Version=3"; 3string 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(); 910 Conn.Close();
1 // 修改数据,相对而言,删除和修改数据比较简单。 2 string dbPath = @"Data Source=D:\sqlliteDb\Test.db;Version=3"; 3string 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(); 910 Conn.Close();
1 // 新建数据库 2 string dbPath = @"D:\sqlliteDb \\" + Convert.ToString(textBox7.Text) + ".db"; 3if (!File.Exists(dbPath)) 4 { 5 SQLiteConnection.CreateFile(dbPath); 6 } 7try 8 { 9 SQLiteConnection Conn = new SQLiteConnection("Data Source=" + dbPath + ";Version=3;"); 10 Conn.Open(); 11 DataSet rs = new DataSet(); 1213//this.button6_Click(sender, e);//调用Click事件1415 dataGridView1.DataSource = rs; 16 Conn.Close(); 17 } 18catch (Exception ex) 19 { 20thrownew Exception("打开数据库:" + dbPath + "的连接失败:" + ex.Message); 21 }
1 // 新建表 2 try 3 { 4 string dbPath = @"Data Source=D:\sqlliteDb\123.db;Version=3"; 5string 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 } 13catch (Exception ex) 14 { 15thrownew Exception("创建数据表" + textBox2.Text + "失败:" + ex.Message); 16 }
1 // 新建字段 2 try 3 { 4 string dbPath = @"Data Source=D:\sqlliteDb\123.db;Version=3"; 5string 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 } 13catch (Exception ex) 14 { 15thrownew Exception("创建数据表" + textBox2.Text + "失败:" + ex.Message); 16 }
原文:https://www.cnblogs.com/Fpack/p/14966894.html
内容总结
以上是互联网集市为您收集整理的C# SQLite3增删改查/新建数据库/新建表/新建字段全部内容,希望文章能够帮你解决C# SQLite3增删改查/新建数据库/新建表/新建字段所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。