实例介绍
【实例简介】如果你的操作系统是 win64位系统,那么请按照以下提示操作:
首先在这里下载 最新版的 sqlite dll文件,引用到项目中
http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
记住是这个版本:Precompiled Binaries for 64-bit Windows 以及对应上你的 .net framework环境即可
另外程序的版本修改成 x64, 右键项目>>生成>>目标平台选择 x64 即可
至此即可使用
据说:从 System.Data.SQLite, Version=1.0.94.0 这个版本以后 不需要引用SQLite.Interop.dll 这个了,包含到 system.data.sqlite.dll中了
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using MachineManage.dal;
using System.Data.SQLite;
namespace MachineManage
{
public partial class FrmMain : Form
{
//[DllImport("sqlite3.dll", EntryPoint = "PCFileCopyToDev")]//把计算机文件拷贝到设备
//public static extern short PCFileCopyToDev(
// string PCFileName,
// string DevFileName,
// bool IsDispProgress,
// string Message);
//private static readonly SQLiteConnection _conn = new SQLiteConnection("Data Source=" frm登录窗体.g_程序目录 "\\Data\\db.dat");
//private string pathDb = System.Windows.Forms.Application.StartupPath "\\SQLiteSpy.db3";
//private string connStringDb = "Provider=System.Data.SQLite;Data Source=SQLiteSpy.db3;Pooling=true;FailIfMissing=false;";
//private static string s1 = @"Provider=System.Data.SQLite;Data Source=";
//private static string s2 = Application.StartupPath "\\SQLiteSpy.db3" ";";
//private static string s3 = "Pooling=true;FailIfMissing=false;";
//public static string StrConn = s1 s2 s3;
//private static string s1 = @"Data Source=";
//private static string s2 = Application.StartupPath "\\SQLiteSpy.db3;" ;// ";"
//private static string s3 = "";//;Pooling=true;FailIfMissing=false
//public static string StrConn =s1 s2 s3;
public String strConn;
SQLiteConnection connection = new SQLiteConnection();
SQLiteCommand command = new SQLiteCommand();
public FrmMain()
{
InitializeComponent();
}
private void FrmMain_Load(object sender, EventArgs e)
{
//SQLiteHelper sqlobj = new SQLiteHelper(StrConn);
//MessageBox.Show(sqlobj.ConnectionString);
//MessageBox.Show(Convert.ToString( sqlobj.Execute("select * from Keys")));
strConn = "test.db3";
File.Delete("test.db3");
try
{
SQLiteConnection.CreateFile("test.db3");//创建数据库
// SQLiteConnection connection = new SQLiteConnection("Data Source=test.db3");//创建一个对test.db3的连接
connection.ConnectionString = "Data Source=" strConn;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
///<summary>
///依据连接串名字connectionName返回数据连接字符串 [读取connectionStrings配置节]
///</summary>
///<param ></param>
///<returns></returns>
private static string GetConnectionStringsConfig(string connectionName)
{
string connectionString = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString.ToString();
Console.WriteLine(connectionString);
return connectionString;
}
///<summary>
///更新连接字符串 [更新connectionStrings配置节]
///</summary>
///<param >连接字符串名称</param>
///<param >连接字符串内容</param>
///<param >数据提供程序名称</param>
private static void UpdateConnectionStringsConfig(string newName, string newConString, string newProviderName)
{
bool isModified = false; //记录该连接串是否已经存在
//如果要更改的连接串已经存在
if (ConfigurationManager.ConnectionStrings[newName] != null)
{
isModified = true;
}
//新建一个连接字符串实例
ConnectionStringSettings mySettings =
new ConnectionStringSettings(newName, newConString, newProviderName);
// 打开可执行的配置文件*.exe.config
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// 如果连接串已存在,首先删除它
if (isModified)
{
config.ConnectionStrings.ConnectionStrings.Remove(newName);
}
// 将新的连接串添加到配置文件中.
config.ConnectionStrings.ConnectionStrings.Add(mySettings);
// 保存对配置文件所作的更改
config.Save(ConfigurationSaveMode.Modified);
// 强制重新载入配置文件的ConnectionStrings配置节
ConfigurationManager.RefreshSection("ConnectionStrings");
}
///<summary>
///返回*.exe.config文件中appSettings配置节的value项 [读取appStrings配置节]
///</summary>
///<param ></param>
///<returns></returns>
private static string GetAppConfig(string strKey)
{
foreach (string key in ConfigurationManager.AppSettings)
{
if (key == strKey)
{
return ConfigurationManager.AppSettings[strKey];
}
}
return null;
}
///<summary>
///在*.exe.config文件中appSettings配置节增加一对键、值对 [更新appStrings配置节]
///</summary>
///<param ></param>
///<param ></param>
private static void UpdateAppConfig(string newKey, string newValue)
{
bool isModified = false;
foreach (string key in ConfigurationManager.AppSettings)
{
if(key==newKey)
{
isModified = true;
}
}
// Open App.Config of executable
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (isModified)// You need to remove the old settings object before you can replace it
{
config.AppSettings.Settings.Remove(newKey);
}
config.AppSettings.Settings.Add(newKey,newValue); // Add an Application Setting.
config.Save(ConfigurationSaveMode.Modified); // Save the changes in App.config file.
ConfigurationManager.RefreshSection("appSettings"); // Force a reload of a changed section.
}
//链接数据库
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "连接数据库")
{
try
{
connection.Open();//连接数据库(无法找到066.dll出错的解决方法:将066.dll拷到wince中sqlite程序目录下)
MessageBox.Show("数据库连接成功");
}
catch (Exception ep)
{
MessageBox.Show(ep.ToString());
}
button1.Text = "关闭数据库";
}
else
{
try
{
connection.Close();//连接数据库(无法找到066.dll出错的解决方法:将066.dll拷到wince中sqlite程序目录下)
MessageBox.Show("数据库断开连接");
}
catch (Exception ep)
{
MessageBox.Show(ep.ToString());
}
button1.Text = "连接数据库";
}
}
//添加数据表
private void button2_Click(object sender, EventArgs e)
{
// connection.Open();
try
{
command.Connection = connection;
command.CommandText = "CREATE TABLE [admin] ([ID] VARCHAR(50),[TEL] VARCHAR(50),[Password] VARCHAR(50));";
int x = command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//插入数据
private void button3_Click(object sender, EventArgs e)
{
SQLiteTransaction ta = connection.BeginTransaction();
try
{
for (int i = 0; i < 100; i )
{
command.CommandText = "insert into admin(ID,TEL,Password) VALUES('3','4','5')";
command.ExecuteNonQuery();
}
/* command.CommandText = "insert into admin(ID,TEL,Password) VALUES('333','444','555')";
int x = command.ExecuteNonQuery();*/
ta.Commit();
}
catch (Exception ex)
{
ta.Rollback();
MessageBox.Show(ex.ToString());
}
}
//查找或者显示
private void button4_Click(object sender, EventArgs e)
{
string id, tel, pword = "";
textBox1.Text = "";
try
{
command.CommandText = "select * from admin";
// int x = command.ExecuteNonQuery();
SQLiteDataReader reader = command.ExecuteReader();//把与command的对应的reader对象转递给reader。
while (reader.Read())
{
id = reader.GetValue(0).ToString();
tel = reader.GetValue(1).ToString();
pword = reader.GetValue(2).ToString();
textBox1.Text = id "---" tel "---" pword "\r\n";
}
reader.Close();
reader.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论