程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> ASP.NET(C#)中操作SQLite數據庫實例

ASP.NET(C#)中操作SQLite數據庫實例

編輯:ASP.NET基礎
要想在ASP.NET項目中使用SQLite數據庫,先需下載一個ADO.NET 2.0 SQLite Data Provider,下載地址為:http://sourceforge.net/project/showfiles.php?group_id=132486&package_id=145568,下載後安裝完畢後,該安裝程序自動在在系統注冊(即可在"添加引用"中看到所安裝的Provider).    

    

     然後,在項目中添加上圖所選項即可.

     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("打開數據庫成功~~<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("單個值查詢結果為:" + 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("更新數據庫中的數據成功.");
Response.Write("以下結果為查詢從數據庫中經過編輯過後的數據項<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();
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved