ASP.NET(C#)中操作SQLite數(shù)據(jù)庫實(shí)例
更新時(shí)間:2009年12月24日 01:26:57 作者:
最近項(xiàng)目中有使用到SQLite數(shù)據(jù)庫,于是查找資料,編寫了一個(gè)ASP.NET基于C#語言的SQLite數(shù)據(jù)庫操作實(shí)例.大家看代碼就可以看懂了,和以往使用ADO.NET操作SQL數(shù)據(jù)庫類似.
要想在ASP.NET項(xiàng)目中使用SQLite數(shù)據(jù)庫,先需下載一個(gè)ADO.NET 2.0 SQLite Data Provider,下載地址為:http://sourceforge.net/project/showfiles.php?group_id=132486&package_id=145568,下載后安裝完畢后,該安裝程序自動(dòng)在在系統(tǒng)注冊(即可在"添加引用"中看到所安裝的Provider).
using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Data.SQLite;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnTest_Click(object sender, EventArgs e)
{
SQLiteConnection.ClearAllPools();
SQLiteConnection.CreateFile(Server.MapPath("~") + "/UserData.dbx");
SQLiteConnection conn = new SQLiteConnection("Data Source=" + Server.MapPath("~" + "/UserData.dbx"));
conn.Open();
Response.Write("打開數(shù)據(jù)庫成功~~<br />");
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = "create table Users (UserID int primary key,UserName varchar(100) not null,UserPassword varchar(100) not null)";
cmd.Connection = conn;
cmd.ExecuteNonQuery();
for (int i = 0; i < 100;i++ )
{
cmd.CommandText = "insert into Users (UserID,UserName,UserPassword) values (" + i + ",'TestUser_" + i + "','" + DateTime.Now.ToString().Replace(" ", "-").Replace(":", "-") + "')";
cmd.ExecuteNonQuery();
}
Response.Write("插入成功~~<br />");
cmd.CommandText = "select Username from Users where UserID=1";
cmd.Connection = conn;
string tempUserName = cmd.ExecuteScalar().ToString();
Response.Write("單個(gè)值查詢結(jié)果為:" + tempUserName + "<br /><br />");
cmd.CommandText = "select * from Users ";
cmd.Connection = conn;
SQLiteDataReader sdrInfo = cmd.ExecuteReader();
if (sdrInfo!= null)
{
int userID = 0;
string userName = string.Empty;
string userPassword = string.Empty;
while(sdrInfo.Read())
{
userID = Convert.ToInt32(sdrInfo["UserID"]);
userName = sdrInfo["UserName"].ToString();
userPassword = sdrInfo["UserPassword"].ToString();
Response.Write("UserID:"+userID+"<br />");
Response.Write("UserName:" + userName+ "<br />");
Response.Write("UserPassword:" + userPassword + "<br />");
Response.Write("<br />");
}
sdrInfo.Close();
sdrInfo.Dispose();
}
cmd.CommandText = "update Users set UserPassword='linxiang'";
cmd.Connection = conn;
cmd.ExecuteNonQuery();
Response.Write("更新數(shù)據(jù)庫中的數(shù)據(jù)成功.");
Response.Write("以下結(jié)果為查詢從數(shù)據(jù)庫中經(jīng)過編輯過后的數(shù)據(jù)項(xiàng)<br /><br />");
cmd.CommandText = "select * from Users ";
cmd.Connection = conn;
sdrInfo = cmd.ExecuteReader();
if (sdrInfo != null)
{
int userID = 0;
string userName = string.Empty;
string userPassword = string.Empty;
while (sdrInfo.Read())
{
userID = Convert.ToInt32(sdrInfo["UserID"]);
userName = sdrInfo["UserName"].ToString();
userPassword = sdrInfo["UserPassword"].ToString();
Response.Write("UserID:" + userID + "<br />");
Response.Write("UserName:" + userName + "<br />");
Response.Write("UserPassword:" + userPassword + "<br />");
Response.Write("<br />");
}
sdrInfo.Close();
sdrInfo.Dispose();
}
conn.Clone();
conn.Dispose();
}
}
然后,在項(xiàng)目中添加上圖所選項(xiàng)即可.
aspx頁面僅包含一按鈕btnTest,在頁面aspx.cs頁面中,引入命名空間,貼入以下類似代碼即可.
復(fù)制代碼 代碼如下:
using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Data.SQLite;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnTest_Click(object sender, EventArgs e)
{
SQLiteConnection.ClearAllPools();
SQLiteConnection.CreateFile(Server.MapPath("~") + "/UserData.dbx");
SQLiteConnection conn = new SQLiteConnection("Data Source=" + Server.MapPath("~" + "/UserData.dbx"));
conn.Open();
Response.Write("打開數(shù)據(jù)庫成功~~<br />");
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = "create table Users (UserID int primary key,UserName varchar(100) not null,UserPassword varchar(100) not null)";
cmd.Connection = conn;
cmd.ExecuteNonQuery();
for (int i = 0; i < 100;i++ )
{
cmd.CommandText = "insert into Users (UserID,UserName,UserPassword) values (" + i + ",'TestUser_" + i + "','" + DateTime.Now.ToString().Replace(" ", "-").Replace(":", "-") + "')";
cmd.ExecuteNonQuery();
}
Response.Write("插入成功~~<br />");
cmd.CommandText = "select Username from Users where UserID=1";
cmd.Connection = conn;
string tempUserName = cmd.ExecuteScalar().ToString();
Response.Write("單個(gè)值查詢結(jié)果為:" + tempUserName + "<br /><br />");
cmd.CommandText = "select * from Users ";
cmd.Connection = conn;
SQLiteDataReader sdrInfo = cmd.ExecuteReader();
if (sdrInfo!= null)
{
int userID = 0;
string userName = string.Empty;
string userPassword = string.Empty;
while(sdrInfo.Read())
{
userID = Convert.ToInt32(sdrInfo["UserID"]);
userName = sdrInfo["UserName"].ToString();
userPassword = sdrInfo["UserPassword"].ToString();
Response.Write("UserID:"+userID+"<br />");
Response.Write("UserName:" + userName+ "<br />");
Response.Write("UserPassword:" + userPassword + "<br />");
Response.Write("<br />");
}
sdrInfo.Close();
sdrInfo.Dispose();
}
cmd.CommandText = "update Users set UserPassword='linxiang'";
cmd.Connection = conn;
cmd.ExecuteNonQuery();
Response.Write("更新數(shù)據(jù)庫中的數(shù)據(jù)成功.");
Response.Write("以下結(jié)果為查詢從數(shù)據(jù)庫中經(jīng)過編輯過后的數(shù)據(jù)項(xiàng)<br /><br />");
cmd.CommandText = "select * from Users ";
cmd.Connection = conn;
sdrInfo = cmd.ExecuteReader();
if (sdrInfo != null)
{
int userID = 0;
string userName = string.Empty;
string userPassword = string.Empty;
while (sdrInfo.Read())
{
userID = Convert.ToInt32(sdrInfo["UserID"]);
userName = sdrInfo["UserName"].ToString();
userPassword = sdrInfo["UserPassword"].ToString();
Response.Write("UserID:" + userID + "<br />");
Response.Write("UserName:" + userName + "<br />");
Response.Write("UserPassword:" + userPassword + "<br />");
Response.Write("<br />");
}
sdrInfo.Close();
sdrInfo.Dispose();
}
conn.Clone();
conn.Dispose();
}
}
您可能感興趣的文章:
- C# SQLite數(shù)據(jù)庫入門使用說明
- C#連接加密的Sqlite數(shù)據(jù)庫的方法
- C#操作SQLite數(shù)據(jù)庫幫助類詳解
- C#操作SQLite數(shù)據(jù)庫之讀寫數(shù)據(jù)庫的方法
- C#操作SQLite數(shù)據(jù)庫方法小結(jié)(創(chuàng)建,連接,插入,查詢,刪除等)
- C#簡單查詢SQLite數(shù)據(jù)庫是否存在數(shù)據(jù)的方法
- C#簡單訪問SQLite數(shù)據(jù)庫的方法(安裝,連接,查詢等)
- C#中使用SQLite數(shù)據(jù)庫的方法介紹
- C#操作SQLite數(shù)據(jù)庫方法小結(jié)
相關(guān)文章
ASP.NET MVC5網(wǎng)站開發(fā)修改及刪除文章(十)
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC5網(wǎng)站開發(fā)修改及刪除文章,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09
Grid或者DataTable中數(shù)據(jù)導(dǎo)出為Excel原來這么簡單
以前一直認(rèn)為,將Grid 或者DataTable中的數(shù)據(jù)導(dǎo)出到Excel功能實(shí)現(xiàn)會(huì)非常復(fù)雜,可能會(huì)想用什么類庫什么的或者實(shí)在太難就用csv算了,沒想到真的很簡單,需要了解的朋友可以參考下2012-12-12
安裝.NET Framework進(jìn)度條卡住不動(dòng)的解決方案(推薦)
VS在安裝之前需要安裝.NET Framework,我安裝的是4.0版本。但是安裝進(jìn)度條到一半左右時(shí)就卡住不動(dòng)了。前前后后重試多次,還有幾次重新開機(jī),但都沒用,怎么解決呢,下面給大家分享下解決方案2016-12-12
.NET?CPU爆高事故事故分析某供應(yīng)鏈WEB網(wǎng)站
這篇文章主要為大家介紹了.NET?CPU爆高事故事故分析某供應(yīng)鏈WEB網(wǎng)站,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Visual Studio中調(diào)試 .NET源代碼的實(shí)現(xiàn)步驟
在調(diào)試 .NET 應(yīng)用程序時(shí),有時(shí)你可能需要查看其他人的源代碼,本文主要介紹了Visual Studio中調(diào)試 .NET源代碼的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03

