在Web.config文件中添加连接字符串,指定使用SQL Server身份验证模式进行登录。
在登录页面的代码中,使用SqlConnection对象打开数据库连接,并通过SqlDataAdapter从数据库中查询用户信息进行验证。 string connectionString = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString; using(SqlConnection connection = new SqlConnection(connectionString)) { SqlDataAdapter adapter = new SqlDataAdapter("SELECT COUNT(*) FROM Users WHERE Username=@username AND Password=@password", connection); adapter.SelectCommand.Parameters.AddWithValue("@username", username); adapter.SelectCommand.Parameters.AddWithValue("@password", password); DataTable table = new DataTable(); adapter.Fill(table); if(table.Rows[0][0].ToString() == "1") { //登录成功 } else { //登录失败 } }