基本信息
源码名称:<免安装版>sqlite 数据库操作 实例源码下载
源码大小:1.13M
文件格式:.zip
开发语言:C#
更新时间:2015-04-12
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
如果你的操作系统是 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());
}
}
}
}