50分,要C#登陆代码,运行于ASP.NET环境下,连接SQL2005数据库,有详细的步骤说明的
3个回答
展开全部
1.在web.config文件中配置 连接数据库语句 Password是你数据库的密码
database 数据库名字
<configuration>
<appSettings>
<add key="dsn" value="Data Source=数据库服务器(local);Initial Catalog=database;User ID=sa;Password=;"/>
</appSettings>
2.传递两个或者多个参数 建立一个aspx 在上面放两个文本框eUsername ePassword 放个按钮 btnok 在 按钮的onclik事件中调用下面的函数(只验证用户名密码):
public bool OpenSqlRlt(string username,string password)
string strconn = ConfigurationSettings.AppSettin["dsn"];
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
SqlDataAdapter mycommand = new SqlDataAdapter("select * from name ='"+eUsername+"' and pass='"+ePassword+"'" , conn);//数据操作
DataSet ds = new DataSet();
mycommand.Fill(ds);
if(ds.Tables[0].Rows.Count>0)
return true;
else
return false;
}
3.调用 在btnok 的onclik的事件中 调用
bool a=OpenSqlRlt(eUsername.Text,ePassword.Text);
接着判断a 是 true or false 就ok 了!
database 数据库名字
<configuration>
<appSettings>
<add key="dsn" value="Data Source=数据库服务器(local);Initial Catalog=database;User ID=sa;Password=;"/>
</appSettings>
2.传递两个或者多个参数 建立一个aspx 在上面放两个文本框eUsername ePassword 放个按钮 btnok 在 按钮的onclik事件中调用下面的函数(只验证用户名密码):
public bool OpenSqlRlt(string username,string password)
string strconn = ConfigurationSettings.AppSettin["dsn"];
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
SqlDataAdapter mycommand = new SqlDataAdapter("select * from name ='"+eUsername+"' and pass='"+ePassword+"'" , conn);//数据操作
DataSet ds = new DataSet();
mycommand.Fill(ds);
if(ds.Tables[0].Rows.Count>0)
return true;
else
return false;
}
3.调用 在btnok 的onclik的事件中 调用
bool a=OpenSqlRlt(eUsername.Text,ePassword.Text);
接着判断a 是 true or false 就ok 了!
展开全部
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
//ConfigurationManager.ConnectionStrings["source"].ConnectionString
string connectionString = ConfigurationSettings.AppSettings["source"];
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
public static SqlConnection Connection
{
get
{
//ConfigurationManager.ConnectionStrings["source"].ConnectionString
string connectionString = ConfigurationSettings.AppSettings["source"];
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
WEB.config
<!--
设置数据库链接
-->
<connectionStrings>
<add name="ConnSqlStr" connectionString="Data Source = (local); User ID = sa; password=; Initial Catalog = 数据库名; Pooling=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
protected void Button1_Click(object sender, EventArgs e)
{
if ((this.Username.Text!="") && (this.Pwd.Text!= "")) //判断用户名和密码框是否为空
{
string conn = ConfigurationManager.ConnectionStrings["ConnSqlStr"].ToString();
SqlConnection con = new SqlConnection(conn);
//连接数据库
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select count(*) from admin where adminName='" + this.Username.Text.Trim() +"' and adminPwd='" + this.Pwd.Text.Trim() +"'";
cmd.Connection = conn;
if (Convert.ToInt32(cmd.ExecuteScalar()) > 0)
{
Session["admin"]="admin";
conn.Close();
Response.Redirect("main.aspx");
}
else
{
Response.Write("<script>alert('用户名和密码错误!');location.href='index.aspx';</script>");
}
}
}
可以HI我
<!--
设置数据库链接
-->
<connectionStrings>
<add name="ConnSqlStr" connectionString="Data Source = (local); User ID = sa; password=; Initial Catalog = 数据库名; Pooling=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
protected void Button1_Click(object sender, EventArgs e)
{
if ((this.Username.Text!="") && (this.Pwd.Text!= "")) //判断用户名和密码框是否为空
{
string conn = ConfigurationManager.ConnectionStrings["ConnSqlStr"].ToString();
SqlConnection con = new SqlConnection(conn);
//连接数据库
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select count(*) from admin where adminName='" + this.Username.Text.Trim() +"' and adminPwd='" + this.Pwd.Text.Trim() +"'";
cmd.Connection = conn;
if (Convert.ToInt32(cmd.ExecuteScalar()) > 0)
{
Session["admin"]="admin";
conn.Close();
Response.Redirect("main.aspx");
}
else
{
Response.Write("<script>alert('用户名和密码错误!');location.href='index.aspx';</script>");
}
}
}
可以HI我
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询