ASP.net(c#)用類的思想實(shí)現(xiàn)插入數(shù)據(jù)到ACCESS例子
要實(shí)現(xiàn)的功能如下:
盡量用類的思想來完成數(shù)據(jù)的插入,因?yàn)檫@個(gè)例子簡(jiǎn)單,所以我也就不多說什么.大家自己看代碼,不懂的可以到論壇交流.
1、首先是ACCESS數(shù)據(jù)庫的設(shè)計(jì),數(shù)據(jù)庫名:myData,表名:student
字段名稱 數(shù)據(jù)類型
sid 自動(dòng)編號(hào)
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=后面請(qǐng)寫你自己的數(shù)據(jù)庫的絕對(duì)路徑
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;
}
- C#操作Access通用類實(shí)例
- C#數(shù)據(jù)庫操作類AccessHelper實(shí)例
- c#連接access數(shù)據(jù)庫操作類分享
- asp.net(C#) Access 數(shù)據(jù)操作類
- C# Access數(shù)據(jù)庫增刪查改的簡(jiǎn)單方法
- ACCESS的參數(shù)化查詢,附VBSCRIPT(ASP)和C#(ASP.NET)函數(shù)
- C#動(dòng)態(tài)創(chuàng)建Access數(shù)據(jù)庫及表的方法
- C#編程實(shí)現(xiàn)連接ACCESS數(shù)據(jù)庫實(shí)例詳解
- C#對(duì)Access進(jìn)行增刪改查的完整示例
- C#使用ADO.Net部件來訪問Access數(shù)據(jù)庫的方法
- C#通過oledb訪問access數(shù)據(jù)庫的方法
- C#實(shí)現(xiàn)Access通用訪問類OleDbHelper完整實(shí)例
相關(guān)文章
C# FTP,GetResponse(),遠(yuǎn)程服務(wù)器返回錯(cuò)誤
C# FTP,GetResponse(),遠(yuǎn)程服務(wù)器返回錯(cuò)誤:(550) 文件不可用(例如,未找到文件,無法訪問文件)2009-06-06
Json數(shù)據(jù)轉(zhuǎn)換list對(duì)象實(shí)現(xiàn)思路及代碼
本文為大家詳細(xì)介紹下Json數(shù)據(jù)轉(zhuǎn)換list對(duì)象的具體實(shí)現(xiàn),感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-04-04
詳解.NET?Core如何構(gòu)建一個(gè)彈性的HTTP請(qǐng)求機(jī)制
在分布式系統(tǒng)中,服務(wù)間的依賴關(guān)系復(fù)雜,任何一個(gè)服務(wù)的故障都可能導(dǎo)致整個(gè)系統(tǒng)的不可用,這時(shí)彈性?HTTP?請(qǐng)求機(jī)制就可以幫助我們,下面我們就來看看.NET?Core如何構(gòu)建一個(gè)彈性的HTTP請(qǐng)求機(jī)制吧2025-01-01
asp.net實(shí)現(xiàn)非常實(shí)用的自定義頁面基類(附源碼)
這篇文章主要介紹了asp.net實(shí)現(xiàn)非常實(shí)用的自定義頁面基類,包含日志處理、控件賦值、異常處理等功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-11-11
asp.net SqlParameter關(guān)于Like的傳參數(shù)無效問題
用傳參方式模糊查詢searchName2009-06-06
設(shè)置ASP.NET頁面的運(yùn)行超時(shí)時(shí)間詳細(xì)到單個(gè)頁面及站點(diǎn)
這篇文章主要介紹了如何設(shè)置ASP.NET頁面的運(yùn)行超時(shí)時(shí)間,包括全局超時(shí)時(shí)間、單個(gè)站點(diǎn)超時(shí)時(shí)間、單個(gè)頁面請(qǐng)求超時(shí)時(shí)間,需要的朋友可以參考下2014-06-06
asp.net下實(shí)現(xiàn)支持文件分塊多點(diǎn)異步上傳的 Web Services
asp.net下實(shí)現(xiàn)支持文件分塊多點(diǎn)異步上傳的 Web Services...2007-04-04
GridView中動(dòng)態(tài)設(shè)置CommandField是否可用或可見的小例子
GridView中動(dòng)態(tài)設(shè)置CommandField是否可用或可見的小例子,需要的朋友可以參考一下2013-05-05

