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

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.