110,538
社区成员
发帖
与我相关
我的任务
分享
using System.Data;
using System.Data.SqlClient;
...
void Page_Load(object sender, System.EventArgs e)
{
string username = this.TextBox1.Text.Trim();
string pwd = this.TextBox2.Text.Trim();
ConnectSql(username, pwd);
}
private void ConnectSql(string username, string pwd)
{
IDbConnection conn = null;
try
{
conn = new SqlConnection("server=192.168.0.220;uid=sa;pwd=;database=text");
conn.Open();
string mySel = "select * from [user] where name=" + "'" + username + "'";
SqlCommand com = new SqlCommand(mySel, (SqlConnection)conn);
SqlDataReader reader = com.ExecuteReader();
if (!reader.HasRows)
{
Response.Redirect("index.aspx?error=用户名错误");
}
else
{
while (reader.Read())
{
if (pwd != reader.GetString(2))
Response.Redirect("index.aspx?error=密码错误");
else
Response.Redirect("Main.html");
}
}
}
catch (SqlException)
{
Response.Write("在打开连接时出现连接级别的错误!");
}
finally
{
if (conn != null)
conn.Close();
}
}