您的位置:首页 > 数据库

ASP.NET连接SQL server2008的几行关键代码

2013-05-15 15:41 281 查看
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Data;

namespace Web6

{

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

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Button1_Click(object sender, EventArgs e)

{

string connString = "server=服务器名字;database=MyDatabase;user id=数据库名字;password=数据库密码";

//建立数据库连接对象

SqlConnection sqlconn = new SqlConnection(connString);

sqlconn.Open();

string sql = "select * from register where
name=@name and password=@password" ;

SqlCommand Comm = new SqlCommand(sql, sqlconn);

Comm.Parameters.Add("name",TextBox1.Text);

Comm.Parameters.Add("password",TextBox2.Text);

SqlDataReader sdr = Comm.ExecuteReader();

if (sdr.Read())

{

Session["UserName"] = TextBox1.Text;

Session["PassWord"] = TextBox2.Text;

Response.Redirect("point_login.aspx");

}

else

{

Response.Redirect("point_login2.aspx");

}

sqlconn.Close();

}

}

}

前台界面如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: