ASP.NET(C\#)에서 SQLite 데이터 베 이 스 를 조작 하 는 실례

4237 단어 ASP.NETSQLite
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();
}
}

좋은 웹페이지 즐겨찾기