手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >ASP.net(c#)用类的思想实现插入数据到ACCESS例子
ASP.net(c#)用类的思想实现插入数据到ACCESS例子
摘要:昨天写了一半,一直没弄清楚当ACCESS数据库的连接代码写成类的时候路径该怎么写,搞了半天,还是用绝对路径解决了,似乎Server.MapP...

昨天写了一半,一直没弄清楚当ACCESS数据库的连接代码写成类的时候路径该怎么写,搞了半天,还是用绝对路径解决了,似乎Server.MapPath没法在cs文件中使用.

要实现的功能如下:

尽量用类的思想来完成数据的插入,因为这个例子简单,所以我也就不多说什么.大家自己看代码,不懂的可以到论坛交流.

1、首先是ACCESS数据库的设计,数据库名:myData,表名:student

字段名称数据类型

sid自动编号

sname文本

score数字

2、建立插入的页面default.aspx,具体代码如下:

<%@PageLanguage="C#"Debug="true"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>无标题页</title>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:LabelID="Label1"runat="server"Text="姓名"></asp:Label>

<asp:TextBoxID="tbxName"runat="server"></asp:TextBox><br/>

<br/>

<asp:LabelID="Label2"runat="server"Text="成绩"></asp:Label>

<asp:TextBoxID="tbxScore"runat="server"></asp:TextBox><br/>

<br/>

<asp:ButtonID="btnInsert"runat="server"OnClick="btnInsert_Click"Text="插入数据"/>

</div>

</form>

</body>

</html>

3、双击default.aspx进入default.aspx.cs,代码如下:

default.aspx.cs的主要代码如下:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

publicpartialclass_Default:System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

}

protectedvoidbtnInsert_Click(objectsender,EventArgse)

{

studentmyStu=newstudent();

myStu.sname=this.tbxName.Text;

myStu.score=Convert.ToInt32(this.tbxScore.Text);

inti=MyClass.insertScore(myStu);

if(i==1)

{

Response.Write("插入成功");

}

else

{

Response.Write("插入失败");

}

}

}

4、在App_Code建立两个类,一个是MyClass.cs,另一个是student.cs,

MyClass.cs的主要代码如下:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

usingSystem.Data.OleDb;

///<summary>

///MyClass的摘要说明

///</summary>

publicclassMyClass

{

publicMyClass()

{

//

//TODO:在此处添加构造函数逻辑

//

}

publicstaticOleDbConnectioncreatCon()

{//DataSource=后面请写你自己的数据库的绝对路径

returnnewOleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;DataSource=C:/DocumentsandSettings/nan/MyDocuments/VisualStudio2005/WebSites/WebSite3/App_Data/myData.mdb");

}

publicstaticintinsertScore(studentmyStu)

{

stringcmdText="insertintostudent(sname,score)values('"+myStu.sname+"','"+myStu.score+"')";

OleDbConnectioncon=MyClass.creatCon();

con.Open();

OleDbCommandcmd=newOleDbCommand(cmdText,con);

intresult=Convert.ToInt32(cmd.ExecuteNonQuery());

con.Close();

returnresult;

}

}

student.cs的主要代码如下:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

///<summary>

///student的摘要说明

///</summary>

publicclassstudent

{

publicstringsname;

publicintscore;

}

【ASP.net(c#)用类的思想实现插入数据到ACCESS例子】相关文章:

ASP.net(c#)打造24小时天气预报及实时天气

ASP.NET中使用Application对象实现简单在线人数统计功能

Asp.net实现向上向下排序的例子

ASP.NET中 Execl导出的六种方法实例

asp.net(c#) MS AJAX的安装

asp.net ext treepanel 动态加载XML的实现方法

.net的socket异步通讯示例分享

ASP.Net Post方式获取数据流的一种简单写法

ASP.NET连接MySql数据库的2个方法及示例

ASP.Net下载大文件的实现方法

精品推荐
分类导航