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)设置数据桥
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 总结
以上是内存溢出为你收集整理的SQLite在.net的应用全部内容,希望文章能够帮你解决SQLite在.net的应用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)