分享
 
 
 

基于ADODBX对数据库的CURD

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: |||超大  

学asp.net也有一个多星期了,之前对这个一无所知,也不知道怎么去找一些相关的资料去学习,不懂了就问问别人这个怎么做,那个怎么写,要不是有jsp和php的基础,估计还得弄上好长的时间来学习。记录一下自己写的东西,简单的一个CURD.

index.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="CURD.index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<% Response.Write(this.ClientHTML); %>

</div>

</form>

</body>

</html>

在连接数据库之前先引用ADODBX;

index.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using ADODBX;

namespace CURD

{

public partial class index : System.Web.UI.Page

{

PRivate string SysClientHTML = "";

public string ClientHTML { get { return SysClientHTML; } }

protected void Page_Load(object sender, EventArgs e)

{

ADODBX.Connection objConn = new Connection();

ADODBX.Recordset objRs = new Recordset();

//SysClientHTML += "<script type=\"text/javascript\">";

//SysClientHTML += " function confirmDel(){";

//SysClientHTML += " if(confirm(\"确定要删除吗?\")){";

//SysClientHTML += " Response.Redirect(\"DelInfo.aspx?id=<%=objRs.Fields('编号')%>\");";

//SysClientHTML += " }";

//SysClientHTML += " }";

//SysClientHTML += "</script>";

SysClientHTML += " <table border=1 style=\"margin:0 auto;width:600px;text-align:center;\">";

SysClientHTML += " <tr>";

SysClientHTML += " <td height=\"25px;\">信息编号</td>";

SysClientHTML += " <td height=\"25px;\">用户名称</td>";

SysClientHTML += " <td height=\"25px;\">用户年龄</td>";

SysClientHTML += " <td height=\"25px;\">用户邮箱</td>";

SysClientHTML += " <td height=\"25px;\">数据操作</td>";

SysClientHTML += " </tr>";

objConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_data/") + "CRUDTest.mdb");

objRs.Open("select * from [person]", objConn, 3);

while (!objRs.EOF)

{

SysClientHTML += " <tr>";

SysClientHTML += " <td height=\"25px;\">" + objRs.Fields("编号") + "</td>";

SysClientHTML += " <td height=\"25px;\">" + objRs.Fields("username") + "</td>";

SysClientHTML += " <td height=\"25px;\">" + objRs.Fields("age") + "</td>";

SysClientHTML += " <td height=\"25px;\">" + objRs.Fields("Email") + "</td>";

SysClientHTML += " <td height=\"25px;\">";

//SysClientHTML += "<a href=\"AddInfo.aspx\">添加</a>&nbsp;<a href=\"Javascript:confirmDel();\">删除</a>&nbsp;<a href=\"#\">修改</a>";

SysClientHTML += "<a href=\"AddInfo.aspx\">添加</a>&nbsp;<a href=\"DelInfo.aspx?id=" + objRs.Fields("编号") + "\">删除</a>&nbsp;<a href=\"ChangeInfo.aspx?id=" + objRs.Fields("编号")+ "\">修改</a>";

SysClientHTML += " </td>";

SysClientHTML += " </tr>";

objRs.MoveNext();

}

objRs.Close();

objConn.Close();

objRs = null;

objConn = null;

SysClientHTML += " </table>";

}

}

}

查询这部分就做完了,接下来就是向数据库增加数据

AddInfo.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddInfo.aspx.cs" Inherits="CURD.AddInfo" %>

<!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 id="Head1" runat="server">

<title></title>

</head>

<body>

<% if (Request["action"] == null || Request["action"] == "") { %>

<form id="form1" runat="server">

<script type="text/javascript">

function selfSubmit() {

var form1 = document.getElementById("form1");

form1.action = "AddInfo.aspx?action=savedata";

form1.submit();

}

</script>

<table border="1" id="table1" style="margin:0 auto;width:400px;">

<tr>

<td width="200">用户昵称</td>

<td width="200"><asp:TextBox runat="server" ID="username"></asp:TextBox></td>

</tr>

<tr>

<td width="200">用户年龄</td>

<td width="200"><asp:TextBox runat="server" ID="age"></asp:TextBox></td>

</tr>

<tr>

<td width="200">用户邮箱</td>

<td width="200"><asp:TextBox runat="server" ID="Email"></asp:TextBox></td>

</tr>

<tr>

<td width="200" colspan="2" style="text-align:center;">

<input type="button" value="提交" onclick="javascript:selfSubmit();" />

<input type="reset" value="重置" />

</td>

</tr>

</table>

</form>

<% } %>

<% if (Request["action"] == "savedata") {

//数据回传后做的数据输出处理

} %>

</body>

</html>

AddInfo.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using ADODBX;

namespace CURD

{

public partial class AddInfo : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (Request["action"] == "savedata") savedata();

}

public void savedata() {

string Suser = Request["username"];

string Sage =Request["age"];

string SEmail = Request["Email"];

//创建对象

ADODBX.Connection objConn = new Connection();

ADODBX.Recordset objRs = new Recordset();

//链接数据库

objConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_data/")+ "CRUDTest.mdb");

objRs.Open("Select * from [person]",objConn,2);

objRs.AddNew();

objRs.Fields("username", Suser);

objRs.Fields("age", Sage);

objRs.Fields("Email", SEmail);

objRs.Update();

//Server.Execute("index.aspx");

Response.Redirect("~/index.aspx");

objRs.Close();

objConn.Close();

objRs = null;

objConn = null;

}

}

}

SaveInfo.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SaveInfo.aspx.cs" Inherits="CURD.SaveInfo" %>

<!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></title>

</head>

<body>

<form id="form1" runat="server">

<div>

</div>

</form>

</body>

</html>

SaveInfo.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using ADODBX;

namespace CURD

{

public partial class SaveInfo : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string Suser = Request["username"];

//string sUserName

//ruquest获取过来的都是字符串类型,进行类型转换

int Sage = Convert.ToInt32(Request["age"]);

string SEmail = Request["Email"];

//创建对象

ADODBX.Connection objConn = new Connection();

ADODBX.Recordset objRs = new Recordset();

//链接数据库

objConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_data/") + "CRUDTest.mdb");

objRs.Open("select * form [use]",objConn,2);

objRs.AddNew();

objRs.Fields("username", Suser);

objRs.Fields("age", Convert.ToString(Sage));

objRs.Fields("Email", SEmail);

objRs.Update();

objRs.Close();

objConn.Close();

objRs = null;

objConn = null;

}

}

}

添加数据这部分结束,下面这部分为修改部分,在修改这部分里面注意id的传递问题。

ChangeInfo.aspx

<%@ Page Language="C#" EnableViewStateMac= "false" AutoEventWireup="true" CodeBehind="ChangeInfo.aspx.cs" Inherits="CURD.changeInfo" %>

<!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></title>

</head>

<body>

<form id="form1" runat="server" action="UpdateInfo.aspx" method="post">

<%-- <asp:TextBox runat="server" ID="hidden" name="id" Visible="False"></asp:TextBox>--%>

<input type="hidden" id="ID" name="hidden" runat="server" />

<table border=1 style="margin:0 auto;width:400px;">

<tr>

<td width="200px;">用户名称</td>

<td width="200px;"><asp:TextBox ID="username" runat="server" ></asp:TextBox></td>

</tr>

<tr>

<td width="200px;">用户年龄</td>

<td width="200px;"><asp:TextBox runat="server" ID="age"></asp:TextBox></td>

</tr>

<tr>

<td width="200px;">用户邮箱</td>

<td width="200px;"><asp:TextBox runat="server" ID="Email"></asp:TextBox></td>

</tr>

<tr>

<td style="text-align:center" colspan="2">

<input type="submit" value="提交" />

<input type="reset" value="重置" />

</td>

</tr>

</table>

</form>

</body>

</html>

ChangeInfo.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using ADODBX;

namespace CURD

{

public partial class changeInfo : System.Web.UI.Page

{

private string SysClientHTML = "";

public string ClientHTML { get { return SysClientHTML; } }

protected void Page_Load(object sender, EventArgs e)

{

int id = Convert.ToInt32(Request["id"]);

ADODBX.Connection objConn = new Connection();

ADODBX.Recordset objRs = new Recordset();

objConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_data/") + "CRUDTest.mdb");

objRs.Open("select * from person where 编号=" + id, objConn, 2);

username.Text = objRs.Fields("username");

age.Text = objRs.Fields("age");

Email.Text = objRs.Fields("Email");

ID.Value = objRs.Fields("编号");

objRs.Close();

objConn.Close();

objConn = null;

objRs = null;

}

}

}

UpdateInfo.aspx

<%@ Page Language="C#" EnableViewStateMac= "false" AutoEventWireup="true" CodeBehind="UpdateInfo.aspx.cs" Inherits="CURD.UpdateInfo" %>

<!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></title>

</head>

<body>

<form id="form1" runat="server">

<div>

</div>

</form>

</body>

</html>

UpdateInfo.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using ADODBX;

namespace CURD

{

public partial class UpdateInfo : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string Sid = Request["ID"];

string Suser = Request["username"];

Response.Write(Suser);

string Sage = Request["age"];

string SEmail = Request["Email"];

//创建对象

ADODBX.Connection objConn = new Connection();

ADODBX.Recordset objRs = new Recordset();

//链接数据库

objConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_data/") + "CRUDTest.mdb");

objRs.Open("Select * from [person] where 编号=" + Sid, objConn, 2);

objRs.Fields("username", Suser);

objRs.Fields("age", Sage);

objRs.Fields("Email", SEmail);

objRs.Update();

//Server.Execute("index.aspx");

Response.Redirect("index.aspx");

objRs.Close();

objConn.Close();

objRs = null;

objConn = null;

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有