Operating SQLite database instances in ASP. NET (C #)

Source: Internet
Author: User
Tags dbx

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 ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.