SQLite在.net的应用
来源:互联网 发布:windows远程ubuntu 编辑:程序博客网 时间:2024/05/05 20:27
IDE: VS2005
语言: C#
组件: sqlite-netFx20-setup-bundle-x86-2005-1.0.92.0
简介:
本示例使用SQLite的密码、外键功能。以及关闭临时文件(journal mode=Off)。
通过查询和插入(修改、删除)来演示C#使用SQLite的实用性。
一、设计窗体
拖拽2个TextBox、2个Button、1个DataGridView、1个SQLiteConnection、1个SQLiteCommand
设置SQLiteCommand的Connection属性为SQLiteConnection
完整的窗体:
二、功能实现
1. 查询
(1)设置数据桥
dataAdapter = new SQLiteDataAdapter(sqLiteCommand1);
(2)设置缓存器
cmdBuilder = new SQLiteCommandBuilder(dataAdapter);
(3)建立数据表
table = new DataTable(tblName);
(4)连接数据表
dataAdapter.Fill(table);
(5)指定数据源
dataGridView1.DataSource = table;
为了定位表的名称,我们使用查找特定的关键字:from。
int iPos = statementStr.LastIndexOf("from") + 5; String tblName = statementStr.Substring(iPos, (statementStr.Length - 1) - iPos);
2. 插入
按照行来分解Insert语句;也可以执行Update、Delete等语句。
int i = edtStmt.Lines.Length; for (int j = 0; j < i; j++) { String strSta = edtStmt.Lines[j].ToString(); sqLiteCommand1.CommandText = strSta; sqLiteCommand1.ExecuteNonQuery(); }
三、源代码
Form1.cs
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;using System.Data.SQLite;namespace testSQLite{ public partial class Form1 : Form { private DataTable table; private SQLiteDataAdapter dataAdapter; private SQLiteCommandBuilder cmdBuilder; private String strFmt, connStr; private String currDir; public Form1() { InitializeComponent(); currDir = Directory.GetCurrentDirectory(); strFmt = "data source=\"" + currDir + "/{0}\"" + ";page size=32768;" + "cache size=0;" + "Password=20130321;" + "foreign keys=True;" + "journal mode=Off"; } private void btnExec_Click(object sender, EventArgs e) { if (edtStmt.Text.ToLower().IndexOf("insert") < 0) return; connStr = string.Format(strFmt, edtDB.Text); // sqLiteConnection1.Close(); sqLiteConnection1.ConnectionString = connStr; sqLiteConnection1.Open(); int i = edtStmt.Lines.Length; for (int j = 0; j < i; j++) { String strSta = edtStmt.Lines[j].ToString(); sqLiteCommand1.CommandText = strSta; sqLiteCommand1.ExecuteNonQuery(); } MessageBox.Show("Insert Done"); } private void btnSearch_Click(object sender, EventArgs e) { if (edtStmt.Text.IndexOf(";") < 0) { MessageBox.Show("Please enter a SQL statement terminator - ';'"); return; } connStr = string.Format(strFmt, edtDB.Text); String statementStr = edtStmt.Text.ToLower(); int iPos = statementStr.LastIndexOf("from") + 5; String tblName = statementStr.Substring(iPos, (statementStr.Length - 1) - iPos); sqLiteConnection1.Close(); sqLiteConnection1.ConnectionString = connStr; sqLiteConnection1.Open(); sqLiteCommand1.CommandText = edtStmt.Text; dataAdapter = new SQLiteDataAdapter(sqLiteCommand1); cmdBuilder = new SQLiteCommandBuilder(dataAdapter); table = new DataTable(tblName); dataAdapter.Fill(table); dataGridView1.DataSource = table; } }}
参考文档:
SQLite.NET.chm
0 0
- SQLite在.net的应用
- sqlite在.NET中的应用
- SQLITE在ASP.NET中的应用
- Sqlite在.NET下的使用和Sqlite数据库清理
- SQLite 与 Access在Asp.Net中的应用比较
- SQLite在ASP.NET 应用中部署问题
- SQLite 与 Access在Asp.Net中的应用比较
- SQLite在多线程环境下的应用
- sqlite在多线程下的应用
- SQLite在多线程环境下的应用
- sqlite在多线程下的应用
- SQLite在多线程环境下的应用
- SQLite在多线程环境下的应用
- 数据库 Sqlite在多线程下的应用
- SQLite在多线程环境下的应用
- SQLite在多线程环境下的应用
- SQLite在多线程环境下的应用
- SQLite在多线程环境下的应用
- 编译原理实验一 词法分析程序
- 使用mongodb实现前缀搜索提示功能
- Cxgrid获取选中行列,排序规则,当前正在编辑的单元格内的值
- 你可以不坚强
- SOJ 1001 Alphacode
- SQLite在.net的应用
- OpenCV2马拉松第10圈——直方图反向投影(back project)
- 以位为单位存储标志-共用体-union
- Algorithm学习笔记 --- 敌兵布阵
- bsh for android : HTML parser
- cocos2d-x项目101次相遇:使用触摸事件移动 精灵
- 构建基于CXF的WebService服务(2)-- 利用CXF提供的wsdl2java工具创建客户端
- 数据库查询获取结果集
- kettle 列转行的demo