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 package is downloaded and installed Program It 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 followingCodeYou can.
Copy code The 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 ();
}
}