使用asp.net制作网站 如何连接数据库

来源:百度知道 编辑:UC知道 时间:2024/05/13 09:19:32
如图所示
我使用的是VS2008 + SQL2005
我希望能实现的功能是
1.当我点击注册按钮时 该页面的数据能存入数据库中
2.当我点击注册按钮时 该页面能跳转到另外一个页面(查询页面)
请问这两个功能的代码要如何写

大哥,这是最基本的东西
注册页面:引用命名空间
using System.Data.SqlClient;
构造连接字符串
string sConnection="server=localhost;database=uesers;uid=sa;pwd=123456"
连接数据库
SqlConnection conn=new SqlConnection(sConnection);

构造插入语句
string strInsert="insert into users(姓名,性别,电话,宿舍,QQ) values ('"+tb_Name.Text+"','"+tb_sex.Text+"','"+tb_phone.Text+"','"+tb_room.Text+"','"+tb_QQ.Text+"')";

创建command对象
SqlCommand cmd=new SqlCommand(strInsert,conn);
conn.Open();
try
{
cmd.ExecuteNonQuery();
Response.Redirect('查询页面的url');
}
catch(SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}