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();
}
}
}
前台界面如下:
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();
}
}
}
前台界面如下:
相关文章推荐
- asp.net(vb) 2008 连接sql2005数据库连接代码,全力奉献
- asp.net利用WMI执行cmd 关键代码
- ASP.net数据库连接相关代码?
- asp.net c# 连接各种数据库代码
- ASP.NET连接SQL数据库的简单实例代码
- 用asp.net连接数据库的登陆代码
- asp.net连接sql2005,cs文件里直接书写的代码,纯手工操作,不在web.config里面进行配置。
- 简单的asp.net模拟邮箱系统基础实现(二 (1)具体版块功能的实现及关键代码之注册页面)
- asp.net连接数据库代码解释
- asp.net连接数据库 增加,修改,删除,查询代码
- ASP.NET连接sql2008数据库的实现代码
- asp.net 通用的连接数据库实例代码
- ASP.NET连接Access数据库代码
- 在ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
- Asp.Net Core-几行代码解决Razor中的嵌套if语句
- Asp.net 连接MySQL的实现代码[]
- (asp.net)PayPal案例的关键源码代码__PayPal集成_API接口
- asp.net(vb)与sql的连接(代码写在web.config中的)