文章转载自:https://wobushixiaohai.blog.csdn.net/article/details/117804179?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-3-117804179-blog-103633083.235%5Ev38%5Epc_relevant_anti_vip_base&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-3-117804179-blog-103633083.235%5Ev38%5Epc_relevant_anti_vip_base&utm_relevant_index=4
以下代码都是经过我测试可用的;
一 一个控制台示例
using System;
using System.Data.SQLite;
namespace SQLiteSamples
{
class Program
{
//数据库连接
SQLiteConnection m_dbConnection;
static void Main(string[] args)
{
Program p = new Program();
}
public Program()
{
createNewDatabase();
connectToDatabase();
createTable();
fillTable();
printHighscores();
}
//创建一个空的数据库
void createNewDatabase()
{
SQLiteConnection.CreateFile("MyDatabase.sqlite");
}
//创建一个连接到指定数据库
void connectToDatabase()
{
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
}
//在指定数据库中创建一个table
void createTable()
{
string sql = "create table highscores (name varchar(20), score int)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
//插入一些数据
void fillTable()
{
string sql = "insert into highscores (name, score) values ('Me', 3000)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = "insert into highscores (name, score) values ('Myself', 6000)";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = "insert into highscores (name, score) values ('And I', 9001)";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
//使用sql查询语句,并显示结果
void printHighscores()
{
string sql = "select * from highscores order by score desc";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
Console.WriteLine("Name: " + reader["name"] + "\tScore: " + reader["score"]);
Console.ReadLine();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
二 完整的增删改查代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SQLite;
namespace jyyggl
{
public partial class Form1 : Form
{
SQLiteConnection m_dbConnection;
bool isupdate;
public Form1()
{
InitializeComponent();
m_dbConnection = new SQLiteConnection("Data Source=jyyggl.sqlite;Version=3;");
m_dbConnection.Open();
isupdate = false;
}
//添加
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" && textBox2.Text == "" && textBox3.Text == "" && textBox4.Text == "" && textBox5.Text == "" && textBox6.Text == "" && textBox7.Text == "" && textBox8.Text == "")
{
MessageBox.Show("没有要添加的内容", "员工添加");
return;
}
else
{
string sql = "insert into yggl values (null,'" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "')";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
databind();
}
}
private void databind()
{
DataTable dt = new DataTable();
SQLiteDataAdapter slda = new SQLiteDataAdapter("select * from yggl", m_dbConnection);
DataSet ds = new DataSet();
slda.Fill(ds);
dt = ds.Tables[0];
dataGridView1.DataSource = dt;
}
// 浏览
private void button1_Click(object sender, EventArgs e)
{
databind();
}
// 查询
private void button5_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SQLiteDataAdapter slda = new SQLiteDataAdapter("select * from yggl where name="+"'"+textBox9.Text+"'", m_dbConnection);
DataSet ds = new DataSet();
slda.Fill(ds);
dt = ds.Tables[0];
dataGridView1.DataSource = dt;
}
//删除
private void button3_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count < 1 || dataGridView1.SelectedRows[0].Cells[1].Value == null)
{
MessageBox.Show("没有选中行。", "员工管理");
}
else
{
object oid = dataGridView1.SelectedRows[0].Cells[0].Value;
if (DialogResult.No == MessageBox.Show("将删除第 " + (dataGridView1.CurrentCell.RowIndex + 1).ToString() + " 行,确定?", "员工管理", MessageBoxButtons.YesNo))
{
return;
}
else
{
string sql = "delete from yggl where id=" + oid;
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
databind();
}
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (isupdate == true && dataGridView1.SelectedRows.Count>=1)
{
textBox1.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
textBox2.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
textBox3.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
textBox4.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
textBox5.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
textBox6.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
textBox7.Text = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
textBox8.Text = dataGridView1.SelectedRows[0].Cells[8].Value.ToString();
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
databind();
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[1].HeaderCell.Value = "姓名";
dataGridView1.Columns[2].HeaderCell.Value = "部门";
dataGridView1.Columns[3].HeaderCell.Value = "职务";
dataGridView1.Columns[4].HeaderCell.Value = "性别";
dataGridView1.Columns[5].HeaderCell.Value = "身份证号";
dataGridView1.Columns[6].HeaderCell.Value = "学历";
dataGridView1.Columns[7].HeaderCell.Value = "手机";
dataGridView1.Columns[8].HeaderCell.Value = "备注";
}
// 开始更新
private void button6_Click(object sender, EventArgs e)
{
isupdate = true;
button4.Enabled = true;
button7.Enabled = true;
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button6.Enabled = false;
if (dataGridView1.SelectedRows.Count >0)
{
dataGridView1.SelectedRows[0].Selected = false;
}
}
// 结束更新
private void button7_Click(object sender, EventArgs e)
{
isupdate = false;
button4.Enabled = false;
button7.Enabled = false;
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button6.Enabled = true;
}
// 更新
private void button4_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count < 1 || dataGridView1.SelectedRows[0].Cells[1].Value==null)
{
MessageBox.Show("没有选中行。", "员工管理");
}
else
{
UPDATE Person SET Address = 'Zhongshan 23', City = 'Nanjing'WHERE LastName = 'Wilson'
object oid = dataGridView1.SelectedRows[0].Cells[0].Value;
string sql = "update yggl set name = '" + textBox1.Text + "',part='" + textBox2.Text + "',zhiwu='" + textBox3.Text + "',sex='" + textBox4.Text +
"',shfzhhao='" + textBox5.Text + "',xueli='" + textBox6.Text + "',shouji='" + textBox7.Text + "',demo='" + textBox8.Text + "'" +
"where id=" + oid;
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
databind();
}
}
}
}
<pre data-index="0" class="prettyprint"><code class="prism language-cpp has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">using</span> System<span class="token punctuation">;</span><span class="token keyword">using</span> System<span class="token punctuation">.</span>Data<span class="token punctuation">.</span>SQLite<span class="token punctuation">;</span>
<span class="token keyword">namespace</span> SQLiteSamples
<span class="token punctuation">{<!-- --></span>
<span class="token keyword">class</span> <span class="token class-name">Program</span>
<span class="token punctuation">{<!-- --></span>
<span class="token comment">//数据库连接</span>
SQLiteConnection m_dbConnection<span class="token punctuation">;</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">Main</span><span class="token punctuation">(</span>string<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
Program p <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">Program</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token function">Program</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
<span class="token function">createNewDatabase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">connectToDatabase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">createTable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fillTable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">printHighscores</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//创建一个空的数据库</span>
<span class="token keyword">void</span> <span class="token function">createNewDatabase</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
SQLiteConnection<span class="token punctuation">.</span><span class="token function">CreateFile</span><span class="token punctuation">(</span><span class="token string">"MyDatabase.sqlite"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//创建一个连接到指定数据库</span>
<span class="token keyword">void</span> <span class="token function">connectToDatabase</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
m_dbConnection <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">SQLiteConnection</span><span class="token punctuation">(</span><span class="token string">"Data Source=MyDatabase.sqlite;Version=3;"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
m_dbConnection<span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//在指定数据库中创建一个table</span>
<span class="token keyword">void</span> <span class="token function">createTable</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
string sql <span class="token operator">=</span> <span class="token string">"create table highscores (name varchar(20), score int)"</span><span class="token punctuation">;</span>
SQLiteCommand command <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">SQLiteCommand</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> m_dbConnection<span class="token punctuation">)</span><span class="token punctuation">;</span>
command<span class="token punctuation">.</span><span class="token function">ExecuteNonQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//插入一些数据</span>
<span class="token keyword">void</span> <span class="token function">fillTable</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
string sql <span class="token operator">=</span> <span class="token string">"insert into highscores (name, score) values ('Me', 3000)"</span><span class="token punctuation">;</span>
SQLiteCommand command <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">SQLiteCommand</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> m_dbConnection<span class="token punctuation">)</span><span class="token punctuation">;</span>
command<span class="token punctuation">.</span><span class="token function">ExecuteNonQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
sql <span class="token operator">=</span> <span class="token string">"insert into highscores (name, score) values ('Myself', 6000)"</span><span class="token punctuation">;</span>
command <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">SQLiteCommand</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> m_dbConnection<span class="token punctuation">)</span><span class="token punctuation">;</span>
command<span class="token punctuation">.</span><span class="token function">ExecuteNonQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
sql <span class="token operator">=</span> <span class="token string">"insert into highscores (name, score) values ('And I', 9001)"</span><span class="token punctuation">;</span>
command <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">SQLiteCommand</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> m_dbConnection<span class="token punctuation">)</span><span class="token punctuation">;</span>
command<span class="token punctuation">.</span><span class="token function">ExecuteNonQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//使用sql查询语句,并显示结果</span>
<span class="token keyword">void</span> <span class="token function">printHighscores</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
string sql <span class="token operator">=</span> <span class="token string">"select * from highscores order by score desc"</span><span class="token punctuation">;</span>
SQLiteCommand command <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">SQLiteCommand</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> m_dbConnection<span class="token punctuation">)</span><span class="token punctuation">;</span>
SQLiteDataReader reader <span class="token operator">=</span> command<span class="token punctuation">.</span><span class="token function">ExecuteReader</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span>reader<span class="token punctuation">.</span><span class="token function">Read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token string">"Name: "</span> <span class="token operator">+</span> reader<span class="token punctuation">[</span><span class="token string">"name"</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">"\tScore: "</span> <span class="token operator">+</span> reader<span class="token punctuation">[</span><span class="token string">"score"</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Console<span class="token punctuation">.</span><span class="token function">ReadLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li><li style="color: rgb(153, 153, 153);">31</li><li style="color: rgb(153, 153, 153);">32</li><li style="color: rgb(153, 153, 153);">33</li><li style="color: rgb(153, 153, 153);">34</li><li style="color: rgb(153, 153, 153);">35</li><li style="color: rgb(153, 153, 153);">36</li><li style="color: rgb(153, 153, 153);">37</li><li style="color: rgb(153, 153, 153);">38</li><li style="color: rgb(153, 153, 153);">39</li><li style="color: rgb(153, 153, 153);">40</li><li style="color: rgb(153, 153, 153);">41</li><li style="color: rgb(153, 153, 153);">42</li><li style="color: rgb(153, 153, 153);">43</li><li style="color: rgb(153, 153, 153);">44</li><li style="color: rgb(153, 153, 153);">45</li><li style="color: rgb(153, 153, 153);">46</li><li style="color: rgb(153, 153, 153);">47</li><li style="color: rgb(153, 153, 153);">48</li><li style="color: rgb(153, 153, 153);">49</li><li style="color: rgb(153, 153, 153);">50</li><li style="color: rgb(153, 153, 153);">51</li><li style="color: rgb(153, 153, 153);">52</li><li style="color: rgb(153, 153, 153);">53</li><li style="color: rgb(153, 153, 153);">54</li><li style="color: rgb(153, 153, 153);">55</li><li style="color: rgb(153, 153, 153);">56</li><li style="color: rgb(153, 153, 153);">57</li><li style="color: rgb(153, 153, 153);">58</li><li style="color: rgb(153, 153, 153);">59</li><li style="color: rgb(153, 153, 153);">60</li><li style="color: rgb(153, 153, 153);">61</li><li style="color: rgb(153, 153, 153);">62</li><li style="color: rgb(153, 153, 153);">63</li><li style="color: rgb(153, 153, 153);">64</li><li style="color: rgb(153, 153, 153);">65</li><li style="color: rgb(153, 153, 153);">66</li><li style="color: rgb(153, 153, 153);">67</li><li style="color: rgb(153, 153, 153);">68</li><li style="color: rgb(153, 153, 153);">69</li><li style="color: rgb(153, 153, 153);">70</li><li style="color: rgb(153, 153, 153);">71</li><li style="color: rgb(153, 153, 153);">72</li><li style="color: rgb(153, 153, 153);">73</li></ul></pre> 标签:SQLite,dbConnection,C#,Text,改查,command,dataGridView1,sql,new From: https://www.cnblogs.com/CZYE/p/17672557.html