Database CURD Based on ADODBX and database CURD Based on ADODBX

Source: Internet
Author: User

Database CURD Based on ADODBX and database CURD Based on ADODBX

I have been learning asp.net for more than a week. I didn't know anything about it before, and I don't know how to find some relevant materials to learn it. If I don't understand it, I will ask others how to do it and how to write it, if it weren't for the foundation of jsp and php, it would take a long time to learn. Record what you write, a simple CURD.

Index. aspx:

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "index. aspx. cs" Inherits = "CURD. index" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>

</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<% Response. Write (this. ClientHTML); %>
</Div>
</Form>
</Body>
</Html>

 

Reference ADODBX before connecting to the database;

Index. aspx. cs

 

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using ADODBX;


Namespace CURD
{
Public partial class index: System. Web. UI. Page
{
Private string SysClientHTML = "";
Public string ClientHTML {get {return SysClientHTML ;}}

Protected void Page_Load (object sender, EventArgs e)
{
ADODBX. Connection objConn = new Connection ();
ADODBX. Recordset objRs = new Recordset ();

// SysClientHTML + = "<script type = \" text/javascript \ "> ";
// SysClientHTML + = "function confirmDel (){";
// SysClientHTML + = "if (confirm (\" are you sure you want to delete it? \")){";
// SysClientHTML + = "Response. Redirect (\" DelInfo. aspx? Id = <% = objRs. Fields ('number') %> \");";
// SysClientHTML + = "}";
// SysClientHTML + = "}";
// SysClientHTML + = "</script> ";


SysClientHTML + = "<table border = 1 style = \" margin: 0 auto; width: 600px; text-align: center; \ "> ";
SysClientHTML + = "<tr> ";
SysClientHTML + = "<td height = \" 25px; \ "> information number </td> ";
SysClientHTML + = "<td height = \" 25px; \ "> User Name </td> ";
SysClientHTML + = "<td height = \" 25px; \ "> User age </td> ";
SysClientHTML + = "<td height = \" 25px; \ "> User email </td> ";
SysClientHTML + = "<td height = \" 25px; \ "> Data Operations </td> ";
SysClientHTML + = "</tr> ";

ObjConn. Open ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + Server. MapPath ("~ /App_data/") +" CRUDTest. mdb ");
ObjRs. Open ("select * from [person]", objConn, 3 );

While (! ObjRs. EOF)
{
SysClientHTML + = "<tr> ";
SysClientHTML + = "<td height = \" 25px; \ ">" + objRs. Fields ("no.") + "</td> ";
SysClientHTML + = "<td height = \" 25px; \ ">" + objRs. Fields ("username") + "</td> ";
SysClientHTML + = "<td height = \" 25px; \ ">" + objRs. Fields ("age") + "</td> ";
SysClientHTML + = "<td height = \" 25px; \ ">" + objRs. Fields ("Email") + "</td> ";
SysClientHTML + = "<td height = \" 25px; \ "> ";
// SysClientHTML + = "<a href = \" AddInfo. aspx \ "> Add </a> & nbsp; <a href = \" JavaScript: confirmDel (); \ "> Delete </a> & nbsp; <a href = \ "# \"> modify </a> ";
SysClientHTML + = "<a href = \" AddInfo. aspx \ "> Add </a> & nbsp; <a href = \" DelInfo. aspx? Id = "+ objRs. Fields (" no. ") +" \ "> Delete </a> & nbsp; <a href = \" ChangeInfo. aspx? Id = "+ objRs. Fields (" no. ") +" \ "> modify </a> ";

SysClientHTML + = "</td> ";
SysClientHTML + = "</tr> ";
ObjRs. MoveNext ();
}
ObjRs. Close ();
ObjConn. Close ();
ObjRs = null;
ObjConn = null;

SysClientHTML + = "</table> ";
}
}
}

The query is complete, and the next step is to add data to the database.

AddInfo. aspx

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "AddInfo. aspx. cs" Inherits = "CURD. AddInfo" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> </title>
</Head>
<Body>
<% If (Request ["action"] = null | Request ["action"] = "") {%>
<Form id = "form1" runat = "server">
<Script type = "text/javascript">
Function selfSubmit (){
Var form1 = document. getElementById ("form1 ");
Form1.action = "AddInfo. aspx? Action = savedata ";
Form1.submit ();
}
</Script>
<Table border = "1" id = "table1" style = "margin: 0 auto; width: 400px;">
<Tr>
<Td width = "200"> user nickname </td>
<Td width = "200"> <asp: TextBox runat = "server" ID = "username"> </asp: TextBox> </td>
</Tr>
<Tr>
<Td width = "200"> User age </td>
<Td width = "200"> <asp: TextBox runat = "server" ID = "age"> </asp: TextBox> </td>
</Tr>
<Tr>
<Td width = "200"> User email </td>
<Td width = "200"> <asp: TextBox runat = "server" ID = "Email"> </asp: TextBox> </td>
</Tr>
<Tr>
<Td width = "200" colspan = "2" style = "text-align: center;">
<Input type = "button" value = "Submit" onclick = "javascript: selfSubmit ();"/>
<Input type = "reset" value = "reset"/>
</Td>
</Tr>
</Table>
</Form>
<% }%>


<% If (Request ["action"] = "savedata "){
// Data output after data return
} %>
</Body>
</Html>

 

AddInfo. aspx. cs

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using ADODBX;
Namespace CURD
{
Public partial class AddInfo: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (Request ["action"] = "savedata") savedata ();
}
Public void savedata (){
String Suser = Request ["username"];
String Sage = Request ["age"];
String SEmail = Request ["Email"];
// Create an object
ADODBX. Connection objConn = new Connection ();
ADODBX. Recordset objRs = new Recordset ();

// Link to the database
ObjConn. Open ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + Server. MapPath ("~ /App_data/") +" CRUDTest. mdb ");
ObjRs. Open ("Select * from [person]", objConn, 2 );
ObjRs. AddNew ();
ObjRs. Fields ("username", Suser );
ObjRs. Fields ("age", Sage );
ObjRs. Fields ("Email", SEmail );
ObjRs. Update ();
// Server. Execute ("index. aspx ");
Response. Redirect ("~ /Index. aspx ");
ObjRs. Close ();
ObjConn. Close ();
ObjRs = null;
ObjConn = null;

}
}
}

SaveInfo. aspx

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "SaveInfo. aspx. cs" Inherits = "CURD. SaveInfo" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>

</Div>
</Form>
</Body>
</Html>

 

SaveInfo. aspx. cs

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using ADODBX;

Namespace CURD
{
Public partial class SaveInfo: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
String Suser = Request ["username"];
// String sUserName
// All obtained by ruquest are string types for type conversion.
Int Sage = Convert. ToInt32 (Request ["age"]);
String SEmail = Request ["Email"];
// Create an object
ADODBX. Connection objConn = new Connection ();
ADODBX. Recordset objRs = new Recordset ();

// Link to the database
ObjConn. Open ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + Server. MapPath ("~ /App_data/") +" CRUDTest. mdb ");
ObjRs. Open ("select * form [use]", objConn, 2 );
ObjRs. AddNew ();
ObjRs. Fields ("username", Suser );
ObjRs. Fields ("age", Convert. ToString (Sage ));
ObjRs. Fields ("Email", SEmail );
ObjRs. Update ();
ObjRs. Close ();
ObjConn. Close ();
ObjRs = null;
ObjConn = null;
}
}
}

After adding the data, the following part is the modification part. In the modification part, pay attention to the id transmission problem.

 

ChangeInfo. aspx

<% @ Page Language = "C #" EnableViewStateMac = "false" AutoEventWireup = "true" CodeBehind = "ChangeInfo. aspx. cs" Inherits = "CURD. changeInfo" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server" action = "UpdateInfo. aspx" method = "post">
<% -- <Asp: TextBox runat = "server" ID = "hidden" name = "id" Visible = "False"> </asp: TextBox> -- %>
<Input type = "hidden" id = "ID" name = "hidden" runat = "server"/>
<Table border = 1 style = "margin: 0 auto; width: 400px;">
<Tr>
<Td width = "200px;"> User Name </td>
<Td width = "200px;"> <asp: TextBox ID = "username" runat = "server"> </asp: TextBox> </td>
</Tr>
<Tr>
<Td width = "200px;"> User age </td>
<Td width = "200px;"> <asp: TextBox runat = "server" ID = "age"> </asp: TextBox> </td>
</Tr>
<Tr>
<Td width = "200px;"> User email </td>
<Td width = "200px;"> <asp: TextBox runat = "server" ID = "Email"> </asp: TextBox> </td>
</Tr>
<Tr>
<Td style = "text-align: center" colspan = "2">
<Input type = "submit" value = "submit"/>
<Input type = "reset" value = "reset"/>
</Td>
</Tr>
</Table>
</Form>
</Body>
</Html>

 

 

ChangeInfo. aspx. cs

 

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using ADODBX;

Namespace CURD
{
Public partial class changeInfo: System. Web. UI. Page
{
Private string SysClientHTML = "";
Public string ClientHTML {get {return SysClientHTML ;}}

Protected void Page_Load (object sender, EventArgs e)
{
Int id = Convert. ToInt32 (Request ["id"]);
ADODBX. Connection objConn = new Connection ();
ADODBX. Recordset objRs = new Recordset ();
ObjConn. Open ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + Server. MapPath ("~ /App_data/") +" CRUDTest. mdb ");
ObjRs. Open ("select * from person where no. =" + id, objConn, 2 );

Username. Text = objRs. Fields ("username ");
Age. Text = objRs. Fields ("age ");
Email. Text = objRs. Fields ("Email ");
ID. Value = objRs. Fields ("no ");

ObjRs. Close ();
ObjConn. Close ();
ObjConn = null;
ObjRs = null;
}
}
}

 

 

UpdateInfo. aspx

 

<% @ Page Language = "C #" EnableViewStateMac = "false" AutoEventWireup = "true" CodeBehind = "UpdateInfo. aspx. cs" Inherits = "CURD. UpdateInfo" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>

</Div>
</Form>
</Body>
</Html>

 

UpdateInfo. aspx. cs

 

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using ADODBX;

Namespace CURD
{
Public partial class UpdateInfo: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
String Sid = Request ["ID"];
String Suser = Request ["username"];
Response. Write (Suser );
String Sage = Request ["age"];
String SEmail = Request ["Email"];
// Create an object
ADODBX. Connection objConn = new Connection ();
ADODBX. Recordset objRs = new Recordset ();

// Link to the database
ObjConn. Open ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + Server. MapPath ("~ /App_data/") +" CRUDTest. mdb ");
ObjRs. Open ("Select * from [person] where id =" + Sid, objConn, 2 );
ObjRs. Fields ("username", Suser );
ObjRs. Fields ("age", Sage );
ObjRs. Fields ("Email", SEmail );
ObjRs. Update ();
// Server. Execute ("index. aspx ");
Response. Redirect ("index. aspx ");
ObjRs. Close ();
ObjConn. Close ();
ObjRs = null;
ObjConn = null;
}
}
}

 

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.