//搜集整理了一下网上的代码.找了半天居然找不到一条插入语句.好郁闷的
//sqlite使用办法.直接COPYDLL文件System.Data.sqlite.DLL到应用程序DEBUG目录下。 然后在项目中添加引用,找到这个文件即可
//添加引用
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.Net;
using System.Data.sqlite;
private static string FavoriteDbPath = System.Environment.CurrentDirectory + "//Favorite.db";
private bool CreatesqliteDb()
{
try
{
System.Data.sqlite.sqliteConnection.CreateFile(FavoriteDbPath);
System.Data.sqlite.sqliteConnection Conn = new System.Data.sqlite.sqliteConnection();
System.Data.sqlite.sqliteConnectionStringBuilder ConnStr = new System.Data.sqlite.sqliteConnectionStringBuilder();
ConnStr.DataSource = FavoriteDbPath;
ConnStr.Password = "Admin";//设置密码,sqlite ADO.NET实现了数据库密码保护
Conn.ConnectionString = ConnStr.ToString();
Conn.Open();
System.Data.sqlite.sqliteCommand cmd = new System.Data.sqlite.sqliteCommand();
string sql = "CREATE TABLE Admin(UserName varchar(20),UserPass varchar(20))";
cmd.CommandText = sql;
cmd.Connection = Conn;
cmd.ExecuteNonQuery();
Conn.Dispose();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
public bool CreateLinkDataTable()
{
try
{
System.Data.sqlite.sqliteConnection Conn = new System.Data.sqlite.sqliteConnection();
System.Data.sqlite.sqliteConnectionStringBuilder ConnStr = new System.Data.sqlite.sqliteConnectionStringBuilder();
ConnStr.DataSource = FavoriteDbPath;
ConnStr.Password = "Admin";//设置密码,sqlite ADO.NET实现了数据库密码保护
Conn.ConnectionString = ConnStr.ToString();
Conn.Open();
System.Data.sqlite.sqliteCommand cmd = new System.Data.sqlite.sqliteCommand();
string sql = "CREATE TABLE [FavoriteList] ([ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,[LinkText] NVARCHAR(256) NULL,[LinkUrl] NVARCHAR(1000),[AddTime] TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL)";
cmd.CommandText = sql;
cmd.Connection = Conn;
cmd.ExecuteNonQuery();
Conn.Dispose();
return true;
}
catch (Exception)
{
return false;
}
}
public bool InsertLinkDataTable(string strLinkText,string strLinkUrl,DateTime dtFileTime)
{
try
{
System.Data.sqlite.sqliteConnection Conn = new System.Data.sqlite.sqliteConnection();
System.Data.sqlite.sqliteConnectionStringBuilder ConnStr = new System.Data.sqlite.sqliteConnectionStringBuilder();
ConnStr.DataSource = FavoriteDbPath;
ConnStr.Password = "Admin";//设置密码,sqlite ADO.NET实现了数据库密码保护
Conn.ConnectionString = ConnStr.ToString();
Conn.Open();
System.Data.sqlite.sqliteCommand cmd = new System.Data.sqlite.sqliteCommand();
string strInsertsql = "INSERT INTO [FavoriteList] (LinkText,LinkUrl,AddTime) VALUES('" + strLinkText + "','" + strLinkUrl + "','" + dtFileTime + "');";
Console.WriteLine(strInsertsql);
cmd.CommandText = strInsertsql;
cmd.Connection = Conn;
cmd.ExecuteNonQuery();
Conn.Dispose();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
public bool GetLinkDataTableRecordList()
{
try
{
System.Data.sqlite.sqliteConnection Conn = new System.Data.sqlite.sqliteConnection();
System.Data.sqlite.sqliteConnectionStringBuilder ConnStr = new System.Data.sqlite.sqliteConnectionStringBuilder();
ConnStr.DataSource = FavoriteDbPath;
ConnStr.Password = "Admin";//设置密码,sqlite ADO.NET实现了数据库密码保护
Conn.ConnectionString = ConnStr.ToString();
Conn.Open();
System.Data.sqlite.sqliteCommand cmd = new System.Data.sqlite.sqliteCommand();
cmd.Connection = Conn;
cmd.CommandText = "select * from [FavoriteList]";
sqliteDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Console.WriteLine("编号 " + System.Convert.ToString(dr["id"]) + " 文本 " + (string)dr["Linktext"] + " 地址 " + (string)dr["LinkUrl"]);
}
Conn.Dispose();
return true;
}
catch (Exception)
{
return false;
}
}
private void 创建数据库文件ToolStripMenuItem_Click(object sender,EventArgs e)
{
try
{
if (System.IO.File.Exists(FavoriteDbPath))
{
System.IO.File.Delete(FavoriteDbPath);
}
if (CreatesqliteDb())
{
MessageBox.Show("数据库创建成功");
}
else
{
MessageBox.Show("数据库创建失败");
}
}
catch (Exception ex)
{
MessageBox.Show("创建数据库文件出错" + ex.Message);
}
}
private void 创建收藏夹数据表ToolStripMenuItem_Click(object sender,EventArgs e)
{
try
{
if (CreateLinkDataTable())
{
MessageBox.Show("创建收藏夹数据表成功");
}
else
{
MessageBox.Show("创建收藏夹数据表失败");
}
}
catch (Exception ex)
{
MessageBox.Show("创建收藏夹数据表出错" + ex.Message);
}
}
private void 查询收藏夹数据表ToolStripMenuItem_Click(object sender,EventArgs e)
{
try
{
if (GetLinkDataTableRecordList())
{
MessageBox.Show("查询收藏夹数据表成功");
}
else
{
MessageBox.Show("查询收藏夹数据表失败");
}
}
catch (Exception ex)
{
MessageBox.Show("查询收藏夹数据表出错" + ex.Message);
}
}
private void 插入新的数据记录ToolStripMenuItem_Click(object sender,EventArgs e) { try { if (InsertLinkDataTable("GJKHKJHK","HLKJLJL",DateTime.Now)) { MessageBox.Show("插入新的数据记录成功"); } else { MessageBox.Show("插入新的数据记录失败"); } } catch (Exception ex) { MessageBox.Show("插入新的数据记录出错" + ex.Message); } }