学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">
<html xmlns="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> <a href=/"Javascript:confirmDel();/">删除</a> <a href=/"#/">修改</a>";
SysClientHTML += "<a href=/"AddInfo.aspx/">添加</a> <a href=/"DelInfo.aspx?id=" + objRs.Fields("编号") + "/">删除</a> <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;
}
}
}
新闻热点
疑难解答