To use the SQLite database in an ASP. NET project, download an ADO. NET 2.0 SQLite Data Provider, which is: http://sourceforge.net/project/showfiles.php? Group_id = 132486 & package_id = 145568. After the installation is completed, the installer is automatically registered in the system (you can see the installed Provider in "add reference ).
Then, add the option to the project.
The aspx page contains only one btnTest button. On the aspx. cs page, introduce the namespace and paste the following similar code.
Copy codeThe Code is as follows:
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 ("database opened successfully ~~ <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 ("insert successful ~~ <Br/> ");
Cmd. CommandText = "select Username from Users where UserID = 1 ";
Cmd. Connection = conn;
String tempUserName = cmd. ExecuteScalar (). ToString ();
Response. Write ("the query result for a single value is:" + tempUserName + "<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 ("the data in the database has been updated successfully .");
Response. Write ("the following results are edited data items from the database <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 ();
}
}