主题:用ASP.NET操作数据库问题,希望大家帮忙。
我用ASP.NET制作了个注册的小程序,实现了将注册的信息填写完后从另一个页面显示出来,现在想将注册页面连接数据库,然后再让另一个页面调用数据库,从而显示出来,但是不会使用ASP.NET对数据库操作,希望大家帮忙,我现在的代码如下。
Default3.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!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>
<asp:Label ID="Label1" runat="server" Style="z-index: 100; left: 217px; position: absolute;
top: 79px" Text="用户名"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Style="z-index: 101; left: 290px; position: absolute;
top: 77px"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Style="z-index: 102; left: 217px; position: absolute;
top: 133px" Text="密码"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Style="z-index: 103; left: 291px; position: absolute;
top: 126px" TextMode="Password"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Style="z-index: 104; left: 217px; position: absolute;
top: 181px" Text="重复密码"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server" Style="z-index: 105; left: 294px; position: absolute;
top: 175px" TextMode="Password"></asp:TextBox>
<asp:Label ID="Label4" runat="server" Style="z-index: 106; left: 219px; position: absolute;
top: 237px" Text="性别"></asp:Label>
<asp:RadioButton ID="RadioButton1" runat="server" Style="z-index: 107; left: 306px;
position: absolute; top: 237px" Text="男" />
<asp:RadioButton ID="RadioButton2" runat="server" Style="z-index: 108; left: 403px;
position: absolute; top: 235px" Text="女" />
<asp:Label ID="Label5" runat="server" Style="z-index: 109; left: 216px; position: absolute;
top: 289px" Text="爱好"></asp:Label>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True"
RepeatDirection="Horizontal" Style="z-index: 110; left: 299px; position: absolute;
top: 287px">
<asp:ListItem>打球</asp:ListItem>
<asp:ListItem>看书</asp:ListItem>
<asp:ListItem>音乐</asp:ListItem>
<asp:ListItem>游戏</asp:ListItem>
<asp:ListItem>旅游</asp:ListItem>
<asp:ListItem>其他</asp:ListItem>
</asp:CheckBoxList>
<asp:Label ID="Label6" runat="server" Style="z-index: 111; left: 217px; position: absolute;
top: 343px" Text="所在城市"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
Style="z-index: 112; left: 311px; position: absolute; top: 342px">
<asp:ListItem>广东</asp:ListItem>
<asp:ListItem>湖南</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" Style="z-index: 113;
left: 402px; position: absolute; top: 342px">
<asp:ListItem>广州</asp:ListItem>
<asp:ListItem>长沙</asp:ListItem>
</asp:DropDownList>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Style="z-index: 114;
left: 311px; position: absolute; top: 396px">
<asp:ListItem>警察</asp:ListItem>
<asp:ListItem>强盗</asp:ListItem>
<asp:ListItem>海军</asp:ListItem>
<asp:ListItem>海盗</asp:ListItem>
<asp:ListItem>一毛</asp:ListItem>
</asp:ListBox>
<asp:Label ID="Label7" runat="server" Style="z-index: 115; left: 223px; position: absolute;
top: 412px" Text="职业"></asp:Label>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="z-index: 116;
left: 388px; position: absolute; top: 399px" Text="添加" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Style="z-index: 117;
left: 388px; position: absolute; top: 435px" Text="删除" />
<asp:ListBox ID="ListBox2" runat="server" AutoPostBack="True" Style="z-index: 118;
left: 456px; position: absolute; top: 396px"></asp:ListBox>
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Style="z-index: 119;
left: 262px; position: absolute; top: 504px" Text="提交" />
<asp:Button ID="Button4" runat="server" Style="z-index: 121; left: 416px; position: absolute;
top: 500px" Text="重写" />
</div>
</form>
</body>
</html>
Default3.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
switch(DropDownList1.SelectedIndex)
{
case 0:
ArrayList val=new ArrayList();
val.Add("广州");
val.Add("佛山");
DropDownList2.DataSource=val;
DropDownList2.DataBind();
break;
case 1:
DropDownList2.Items.Clear();
DropDownList2.Items.Add("长沙");
DropDownList2.Items.Add("怀化");
break;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
for(int i = 0; i<ListBox1.Items.Count; i++)
{
if(ListBox1.Items[i].Selected)
ListBox2.Items.Add(ListBox1.Items[i]);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
for(int i=ListBox2.Items.Count -1;i>=0;i--)
if (ListBox2.Items[i].Selected)
ListBox2.Items.Remove(ListBox2.Items[i]);
}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
//if (CheckBox1.Checked)
//{
// for (int i = 0; i < CheckBoxList1.Items.Count; i++)
// CheckBoxList1.Items[i].Selected = true;
//}
}
protected void Button3_Click(object sender, EventArgs e)
{
//Response.Redirect("Default4.aspx?name=" + TextBox1.Text);
//Response.Redirect("Default4.aspx?name=" + TextBox2.Text);
string n="";
if(RadioButton1.Checked )
{
n = "男";
}
if(RadioButton2.Checked)
{
n = "女";
}
string s = "";
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
s = s + CheckBoxList1.Items[i].Text;
s = s + "</br>";
}
}
if (DropDownList1.SelectedItem.Selected && DropDownList2.SelectedItem.Selected)
// string v = TextBox1 + TextBox2 + n;
Response.Redirect("Default4.aspx?name=" + TextBox1.Text + "&pass=" + TextBox2.Text + "&sex=" + n + "&love=" + s + "&city=" + DropDownList1.SelectedItem.Text +"&work="+ListBox2.Text);
}
}
Default4.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!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>
Default4.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = Request["name"];
string pas = Request["pass"];
string se = Request["sex"];
string lov = Request["love"];
string cit = Request["city"];
string wor = Request["work"];
Response.Write(str+"<br>" + pas+"<br>" + se+"<br>" + lov+"<br>" + cit+"<br>" + wor);
}
}
就是这2个页面,数据库我创建的名字为huiyuan创建的列名有Name,Password,Sex,Love,City,Job,数据类型全部是varchar,姓名和密码不能为空
Default3.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!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>
<asp:Label ID="Label1" runat="server" Style="z-index: 100; left: 217px; position: absolute;
top: 79px" Text="用户名"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Style="z-index: 101; left: 290px; position: absolute;
top: 77px"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Style="z-index: 102; left: 217px; position: absolute;
top: 133px" Text="密码"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Style="z-index: 103; left: 291px; position: absolute;
top: 126px" TextMode="Password"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Style="z-index: 104; left: 217px; position: absolute;
top: 181px" Text="重复密码"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server" Style="z-index: 105; left: 294px; position: absolute;
top: 175px" TextMode="Password"></asp:TextBox>
<asp:Label ID="Label4" runat="server" Style="z-index: 106; left: 219px; position: absolute;
top: 237px" Text="性别"></asp:Label>
<asp:RadioButton ID="RadioButton1" runat="server" Style="z-index: 107; left: 306px;
position: absolute; top: 237px" Text="男" />
<asp:RadioButton ID="RadioButton2" runat="server" Style="z-index: 108; left: 403px;
position: absolute; top: 235px" Text="女" />
<asp:Label ID="Label5" runat="server" Style="z-index: 109; left: 216px; position: absolute;
top: 289px" Text="爱好"></asp:Label>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True"
RepeatDirection="Horizontal" Style="z-index: 110; left: 299px; position: absolute;
top: 287px">
<asp:ListItem>打球</asp:ListItem>
<asp:ListItem>看书</asp:ListItem>
<asp:ListItem>音乐</asp:ListItem>
<asp:ListItem>游戏</asp:ListItem>
<asp:ListItem>旅游</asp:ListItem>
<asp:ListItem>其他</asp:ListItem>
</asp:CheckBoxList>
<asp:Label ID="Label6" runat="server" Style="z-index: 111; left: 217px; position: absolute;
top: 343px" Text="所在城市"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
Style="z-index: 112; left: 311px; position: absolute; top: 342px">
<asp:ListItem>广东</asp:ListItem>
<asp:ListItem>湖南</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" Style="z-index: 113;
left: 402px; position: absolute; top: 342px">
<asp:ListItem>广州</asp:ListItem>
<asp:ListItem>长沙</asp:ListItem>
</asp:DropDownList>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Style="z-index: 114;
left: 311px; position: absolute; top: 396px">
<asp:ListItem>警察</asp:ListItem>
<asp:ListItem>强盗</asp:ListItem>
<asp:ListItem>海军</asp:ListItem>
<asp:ListItem>海盗</asp:ListItem>
<asp:ListItem>一毛</asp:ListItem>
</asp:ListBox>
<asp:Label ID="Label7" runat="server" Style="z-index: 115; left: 223px; position: absolute;
top: 412px" Text="职业"></asp:Label>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="z-index: 116;
left: 388px; position: absolute; top: 399px" Text="添加" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Style="z-index: 117;
left: 388px; position: absolute; top: 435px" Text="删除" />
<asp:ListBox ID="ListBox2" runat="server" AutoPostBack="True" Style="z-index: 118;
left: 456px; position: absolute; top: 396px"></asp:ListBox>
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Style="z-index: 119;
left: 262px; position: absolute; top: 504px" Text="提交" />
<asp:Button ID="Button4" runat="server" Style="z-index: 121; left: 416px; position: absolute;
top: 500px" Text="重写" />
</div>
</form>
</body>
</html>
Default3.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
switch(DropDownList1.SelectedIndex)
{
case 0:
ArrayList val=new ArrayList();
val.Add("广州");
val.Add("佛山");
DropDownList2.DataSource=val;
DropDownList2.DataBind();
break;
case 1:
DropDownList2.Items.Clear();
DropDownList2.Items.Add("长沙");
DropDownList2.Items.Add("怀化");
break;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
for(int i = 0; i<ListBox1.Items.Count; i++)
{
if(ListBox1.Items[i].Selected)
ListBox2.Items.Add(ListBox1.Items[i]);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
for(int i=ListBox2.Items.Count -1;i>=0;i--)
if (ListBox2.Items[i].Selected)
ListBox2.Items.Remove(ListBox2.Items[i]);
}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
//if (CheckBox1.Checked)
//{
// for (int i = 0; i < CheckBoxList1.Items.Count; i++)
// CheckBoxList1.Items[i].Selected = true;
//}
}
protected void Button3_Click(object sender, EventArgs e)
{
//Response.Redirect("Default4.aspx?name=" + TextBox1.Text);
//Response.Redirect("Default4.aspx?name=" + TextBox2.Text);
string n="";
if(RadioButton1.Checked )
{
n = "男";
}
if(RadioButton2.Checked)
{
n = "女";
}
string s = "";
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
s = s + CheckBoxList1.Items[i].Text;
s = s + "</br>";
}
}
if (DropDownList1.SelectedItem.Selected && DropDownList2.SelectedItem.Selected)
// string v = TextBox1 + TextBox2 + n;
Response.Redirect("Default4.aspx?name=" + TextBox1.Text + "&pass=" + TextBox2.Text + "&sex=" + n + "&love=" + s + "&city=" + DropDownList1.SelectedItem.Text +"&work="+ListBox2.Text);
}
}
Default4.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!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>
Default4.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = Request["name"];
string pas = Request["pass"];
string se = Request["sex"];
string lov = Request["love"];
string cit = Request["city"];
string wor = Request["work"];
Response.Write(str+"<br>" + pas+"<br>" + se+"<br>" + lov+"<br>" + cit+"<br>" + wor);
}
}
就是这2个页面,数据库我创建的名字为huiyuan创建的列名有Name,Password,Sex,Love,City,Job,数据类型全部是varchar,姓名和密码不能为空