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:
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)
{
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;
}
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