ASP.NET(C#)中操作SQLite數(shù)據(jù)庫(kù)實(shí)例
然后,在項(xiàng)目中添加上圖所選項(xiàng)即可.
aspx頁(yè)面僅包含一按鈕btnTest,在頁(yè)面aspx.cs頁(yè)面中,引入命名空間,貼入以下類似代碼即可.
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("打開(kāi)數(shù)據(jù)庫(kù)成功~~<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ù)庫(kù)中的數(shù)據(jù)成功.");
Response.Write("以下結(jié)果為查詢從數(shù)據(jù)庫(kù)中經(jīng)過(guò)編輯過(guò)后的數(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ù)庫(kù)入門(mén)使用說(shuō)明
- C#連接加密的Sqlite數(shù)據(jù)庫(kù)的方法
- C#操作SQLite數(shù)據(jù)庫(kù)幫助類詳解
- C#操作SQLite數(shù)據(jù)庫(kù)之讀寫(xiě)數(shù)據(jù)庫(kù)的方法
- C#操作SQLite數(shù)據(jù)庫(kù)方法小結(jié)(創(chuàng)建,連接,插入,查詢,刪除等)
- C#簡(jiǎn)單查詢SQLite數(shù)據(jù)庫(kù)是否存在數(shù)據(jù)的方法
- C#簡(jiǎn)單訪問(wèn)SQLite數(shù)據(jù)庫(kù)的方法(安裝,連接,查詢等)
- C#中使用SQLite數(shù)據(jù)庫(kù)的方法介紹
- C#操作SQLite數(shù)據(jù)庫(kù)方法小結(jié)
相關(guān)文章
ASP.NET MVC5網(wǎng)站開(kāi)發(fā)修改及刪除文章(十)
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC5網(wǎng)站開(kāi)發(fā)修改及刪除文章,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09
Grid或者DataTable中數(shù)據(jù)導(dǎo)出為Excel原來(lái)這么簡(jiǎn)單
以前一直認(rèn)為,將Grid 或者DataTable中的數(shù)據(jù)導(dǎo)出到Excel功能實(shí)現(xiàn)會(huì)非常復(fù)雜,可能會(huì)想用什么類庫(kù)什么的或者實(shí)在太難就用csv算了,沒(méi)想到真的很簡(jiǎn)單,需要了解的朋友可以參考下2012-12-12
安裝.NET Framework進(jìn)度條卡住不動(dòng)的解決方案(推薦)
VS在安裝之前需要安裝.NET Framework,我安裝的是4.0版本。但是安裝進(jìn)度條到一半左右時(shí)就卡住不動(dòng)了。前前后后重試多次,還有幾次重新開(kāi)機(jī),但都沒(méi)用,怎么解決呢,下面給大家分享下解決方案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
ADO.NET通用數(shù)據(jù)庫(kù)訪問(wèn)類
這篇文章主要為大家介紹了ADO.NET通用數(shù)據(jù)庫(kù)訪問(wèn)類,利用ADO.NET的體系架構(gòu)打造通用的數(shù)據(jù)庫(kù)訪問(wèn)通用類,感興趣的小伙伴們可以參考一下2016-03-03

