I wrote half of it yesterday, but I never figured out when the ACCESS database is connected. Code When writing a class, how to write the path? After half a day, the absolute path is used. It seems that server. mappath cannot be used in CS files.
The functions to be implemented are as follows:
Try to use the class idea to insert data. Because this example is simple, I will not talk much about it. You can read the code yourself and communicate with the Forum if you don't understand it.
1. First, the ACCESS database design, database name: mydata, table name: Student
Field Name Data Type
Sid automatic ID
Sname text
Score number
2. Create the inserted page default. aspx. The Code is as follows:
<% @ Page Language = "C #" DEBUG = "true" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<! 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> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: Label id = "label1" runat = "server" text = "name"> </ASP: Label>
<Asp: textbox id = "tbxname" runat = "server"> </ASP: textbox> <br/>
<Br/>
<Asp: Label id = "label2" runat = "server" text = "score"> </ASP: Label>
<Asp: textbox id = "tbxscore" runat = "server"> </ASP: textbox> <br/>
<Br/>
<Asp: button id = "btninsert" runat = "server" onclick = "btninsert_click" text = "insert data"/>
</Div>
</Form>
</Body>
</Html>
3. Double-click default. aspx to enter default. aspx. cs. The Code is as follows:
The main code of default. aspx. CS is as follows:
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
Protected void btninsert_click (Object sender, eventargs E)
{
Student mystu = new student ();
Mystu. sname = This. tbxname. text;
Mystu. Score = convert. toint32 (this. tbxscore. Text );
Int I = myclass. insertscore (mystu );
If (I = 1)
{
Response. Write ("inserted successfully ");
}
Else
{
Response. Write ("insertion failed ");
}
}
}
4. Create two classes in app_code: myclass. CS and student. CS,
The main code of myclass. CS is as follows:
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. oledb;
/// <Summary>
/// Summary of myclass
/// </Summary>
Public class myclass
{
Public myclass ()
{
//
// Todo: add the constructor logic here
//
}
Public static oledbconnection creatcon ()
{// Data source = enter the absolute path of your own database
Return new oledbconnection ("provider = Microsoft. jet. oledb.4.0; Data Source = C:/Documents and Settings/nan/My Documents ents/Visual Studio 2005/websites/website3/app_data/mydata. mdb ");
}
Public static int insertscore (student mystu)
{
String plain text = "insert into student (sname, score) values ('" + mystu. sname + "', '" + mystu. Score + "')";
Oledbconnection con = myclass. creatcon ();
Con. open ();
Oledbcommand cmd = new oledbcommand (plain text, con );
Int result = convert. toint32 (CMD. executenonquery ());
Con. Close ();
Return result;
}
}
The main code of Student. CS is as follows:
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
/// <Summary>
/// Student Summary
/// </Summary>
Public class student
{
Public String sname;
Public int score;
}