asp.net (C #) using the idea of class to insert data into Access examples _ practical tips

Source: Internet
Author: User
I wrote half of it yesterday, and I didn't figure it out. When the Access database connection code written in class, how to write the path, for a long time, or with the absolute path to solve, it seems server.mappath can not be used in the CS file.

The functionality to implement is as follows:

Try to use the idea of class to complete the insertion of the data, because this example is simple, so I do not say anything more. Everyone to see the code, do not understand the forum can communicate.

1, the first is the design of Access database, database name: MyData, table name: Student

Field name data type
Sid Automatic Numbering
Sname text
Score Digital
2, create the inserted page default.aspx, the specific 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 ">

<title> Untitled Page </title>

<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= "Results" ></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>



3, double-click Default.aspx Enter Default.aspx.cs, the code is as follows:

The main code for 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 ("Insert succeeded");
}
Else
{
Response.Write ("Insert failed");
}
}
}


4. Create two classes in App_Code, one is MyClass.cs, the other is Student.cs,

The main code for 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 description of MyClass
</summary>
public class MyClass
{

Public MyClass ()
{
//
TODO: Add constructor logic here
//
}
public static OleDbConnection Creatcon ()
{//data source= behind please write your own absolute path to the database
return new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/documents and Settings/nan/my Documents/ Visual Studio 2005/websites/website3/app_data/mydata.mdb ");
}
public static int Insertscore (student mystu)
{
String cmdtext = "INSERT into student (Sname,score) VALUES (' + Mystu.sname +" ', ' "+ Mystu.score +") ";
OleDbConnection con = Myclass.creatcon ();
Con. Open ();
OleDbCommand cmd = new OleDbCommand (Cmdtext, con);
int result = Convert.ToInt32 (cmd. ExecuteNonQuery ());
Con. Close ();
return result;

}
}


The main code for 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>
Summary description of Student
</summary>
public class student
{
public string sname;
public int score;
}

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.