ASP.NET(C#)中操作SQLite數(shù)據(jù)庫實(shí)例
然后,在項(xiàng)目中添加上圖所選項(xiàng)即可.
aspx頁面僅包含一按鈕btnTest,在頁面aspx.cs頁面中,引入命名空間,貼入以下類似代碼即可.
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("單個值查詢結(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?Core中Options模式的使用及其源碼
這篇文章主要介紹了ASP.NET?Core中Options模式的使用及其源碼解析,在ASP.NET Core中引入了Options這一使用配置方式,其主要是為了解決依賴注入時需要傳遞指定數(shù)據(jù)問題(不是自行獲取,而是能集中配置),需要的朋友可以參考下2022-03-03ASP.NET Core擴(kuò)展庫之日志功能的使用詳解
這篇文章主要介紹了ASP.NET Core擴(kuò)展庫之日志功能的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用.NET技術(shù),感興趣的朋友可以了解下2021-03-03ASP.NET實(shí)現(xiàn)單點(diǎn)登陸(SSO)適用于多種情況
這篇文章主要介紹了ASP.NET在不同情況下實(shí)現(xiàn)單點(diǎn)登陸(SSO)的方法,在同主域但不同子域之間實(shí)現(xiàn)單點(diǎn)登陸等等2014-09-09Discuz!NT 3與asp.net 整合的實(shí)例教程
本次整合只針對NETSNS中的代碼做了少許修改,完成了基本的和論壇同步注冊,登陸和注銷,信息獲取,信息修改。為的是給各位Discuz!NT API愛好者做一個簡單的API事例,供大家參考。2009-11-11Entity Framework使用Code First模式管理事務(wù)
本文詳細(xì)講解了Entity Framework使用Code First模式管理事務(wù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03asp.net中對象失去焦點(diǎn)時自動提交數(shù)據(jù) V2
一年多前,Insus.NET有寫過一篇 《對象失去焦點(diǎn)時自己動提交數(shù)據(jù)》,那一篇是依賴Linkbutton來做隱藏提交。是否有不用依賴Linkbutton方法呢? 答案是肯定的2012-11-11asp.net顯示圖片到指定的Image控件中 具體實(shí)現(xiàn)
這篇文章介紹了asp.net顯示圖片到指定的Image控件中 具體實(shí)現(xiàn),有需要的朋友可以參考一下2013-11-11