|
要實(shí)現(xiàn)的功能如下:
盡量用類的思想來完成數(shù)據(jù)的插入,因?yàn)檫@個(gè)例子簡單,所以我也就不多說什么.大家自己看代碼,不懂的可以到論壇交流.
1、首先是ACCESS數(shù)據(jù)庫的設(shè)計(jì),數(shù)據(jù)庫名:myData,表名:student
字段名稱 數(shù)據(jù)類型
sid 自動編號
sname 文本
score 數(shù)字
2、建立插入的頁面default.ASPx,具體代碼如下:
<%@ 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>無標(biāo)題頁</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ASP:Label ID="Label1" runat="server" Text="姓名"></ASP:Label>
<ASP:TextBox ID="tbxName" runat="server"></ASP:TextBox><br />
<br />
<ASP:Label ID="Label2" runat="server" Text="成績"></ASP:Label>
<ASP:TextBox ID="tbxScore" runat="server"></ASP:TextBox><br />
<br />
<ASP:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="插入數(shù)據(jù)" />
</div>
</form>
</body>
</html>
3、雙擊default.ASPx進(jìn)入default.ASPx.cs,代碼如下:
default.ASPx.cs的主要代碼如下:
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("插入成功");
}
else
{
Response.Write("插入失敗");
}
}
}
4、在App_Code建立兩個(gè)類,一個(gè)是MyClass.cs,另一個(gè)是student.cs,
MyClass.cs的主要代碼如下:
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>
/// MyClass 的摘要說明
/// </summary>
public class MyClass
{
public MyClass()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
public static OleDbConnection creatCon()
{//Data Source=后面請寫你自己的數(shù)據(jù)庫的絕對路徑
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;
}
}
student.cs的主要代碼如下:
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>
public class student
{
public string sname;
public int score;
}
AspNet技術(shù):ASP.net(c#)用類的思想實(shí)現(xiàn)插入數(shù)據(jù)到ACCESS例子,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。