前台就一个登入控件
WEB.CONFIG里增加了句
<add name="SQLconnstr" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|music.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" />
protected void Login1_Authenticate1(object sender, AuthenticateEventArgs e)
{
bool Authenticated = false;
Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password);
e.Authenticated = Authenticated;
if (Authenticated == true)
{
Response.Redirect("Home.aspx");
} }
private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
{
bool boolReturnValue = false;
SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLconnStr"].ConnectionString);
String strSQL = "Select * From user";
SqlCommand command = new SqlCommand(strSQL, Connection);
SqlDataReader Dr;
Connection.Open();
Dr = command.ExecuteReader();
while (Dr.Read())
{
if ((UserName == Dr["name"].ToString()) & (Password == Dr["Paswd"].ToString()))
{
boolReturnValue = true;
}
Dr.Close();
return boolReturnValue;
}
}运行时提示SiteLevelCustomAuthenticationMethod并非所有的代码路径都返回值
这是怎么回事?
WEB.CONFIG里增加了句
<add name="SQLconnstr" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|music.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" />
protected void Login1_Authenticate1(object sender, AuthenticateEventArgs e)
{
bool Authenticated = false;
Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password);
e.Authenticated = Authenticated;
if (Authenticated == true)
{
Response.Redirect("Home.aspx");
} }
private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
{
bool boolReturnValue = false;
SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLconnStr"].ConnectionString);
String strSQL = "Select * From user";
SqlCommand command = new SqlCommand(strSQL, Connection);
SqlDataReader Dr;
Connection.Open();
Dr = command.ExecuteReader();
while (Dr.Read())
{
if ((UserName == Dr["name"].ToString()) & (Password == Dr["Paswd"].ToString()))
{
boolReturnValue = true;
}
Dr.Close();
return boolReturnValue;
}
}运行时提示SiteLevelCustomAuthenticationMethod并非所有的代码路径都返回值
这是怎么回事?
{
if ((UserName == Dr["name"].ToString()) & (Password == Dr["Paswd"].ToString()))
{
boolReturnValue = true;
}
}
Dr.Close();
return boolReturnValue;
private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
{
bool boolReturnValue = false;
SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLconnStr"].ConnectionString);
String strSQL = "Select * From user";
SqlCommand command = new SqlCommand(strSQL, Connection);
SqlDataReader Dr;
Connection.Open();
Dr = command.ExecuteReader();
while (Dr.Read())
{
if ((UserName == Dr["name"].ToString()) & (Password == Dr["Paswd"].ToString()))
{
boolReturnValue = true;
}
Dr.Close();
}
return boolReturnValue;
}
断点后看了下..while 语句是运行了..就是没找到数据库中对应的..
我是哪里错了
{
if ((UserName == Dr["name"].ToString()) & (Password == Dr["Paswd"].ToString()))
{
boolReturnValue = true;
}
}
Dr.Close();//循环完了再关闭,应该是这里的问题。
return boolReturnValue;
{
if ((UserName == Dr["name"].ToString()) & (Password == Dr["Paswd"].ToString()))
{
boolReturnValue = true;
break;
}
}
Dr.Close();
return boolReturnValue;
{
bool boolReturnValue = false;
SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLconnStr"].ConnectionString);
String strSQL = "Select * From user";
SqlCommand command = new SqlCommand(strSQL, Connection);
SqlDataReader Dr;
Connection.Open();
Dr = command.ExecuteReader();
while (Dr.Read())
{
if ((UserName == Dr["name"].ToString()) & (Password == Dr["Paswd"].ToString()))
{
boolReturnValue = true;
}
Dr.Close();
}
return boolReturnValue;
}
while (Dr.Read())
{
if ((UserName == Dr["name"].ToString()) & (Password == Dr["Paswd"].ToString()))
{
boolReturnValue = true;
}
Dr.Close();
return boolReturnValue;
}很明显。如果dr为空的话。就没返回值了!
你可以写一个方法,将用户名和密码当做条件判断数据库中是否存在数据,来实现登录的。不要读出所有数据一一匹配,个人建议。