HasRows属性 获取一个值,该值指示 SqlDataReader 是否包含一行或多行。
如果 SqlDataReader 包含一行或多行,则为 true;否则为 false。
测试代码:
string myConnString = "data source=.;uid=sa;pwd=;database=northwind ";
string mySelectQuery = "SELECT OrderID FROM Orders ";
SqlConnection myConnection = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
myConnection.Open();
SqlDataReader myReader;
myReader = myCommand.ExecuteReader();
if(myReader.HasRows)
{
MessageBox.Show( "yes ");
}
else
{
MessageBox.Show( "no ");
}
myReader.Close();
myConnection.Close();
//现在存在数据,返回yes;
//当把查询语句换为:SELECT OrderID FROM Orders where orderid = '1000 '
//无数据返回,返回no;
所以 可用hasrows 这种方法来使用在判断用户是否合法上面,具体可用在登陆界面。
还有另一种方法,是利用数据库数据进行比对
private static string SqlConnection connection = new SqlConnection(connectionString);//connectionString自己写
public bool GetPassWordByUserName(string UserName,string Password)
{
try
{
string truePass = null;
connection.Open();
SqlCommand command = new SqlCommand();
command.CommandText=String.Format("select password from UserInfo where userName={0}",UserName);
SqlDataReader reader = command.ExecuteReader();
while(reader.Read())
{
truePass =reader.Read["Password"].ToString();
}
}
catch
{}
finally
{
reader.Close();connection.Close();
}
if( (truePass==null) || (Password != truePass))
//用户名不存在或密码不正确
else
//密码正确
}
}
如果 SqlDataReader 包含一行或多行,则为 true;否则为 false。
测试代码:
string myConnString = "data source=.;uid=sa;pwd=;database=northwind ";
string mySelectQuery = "SELECT OrderID FROM Orders ";
SqlConnection myConnection = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
myConnection.Open();
SqlDataReader myReader;
myReader = myCommand.ExecuteReader();
if(myReader.HasRows)
{
MessageBox.Show( "yes ");
}
else
{
MessageBox.Show( "no ");
}
myReader.Close();
myConnection.Close();
//现在存在数据,返回yes;
//当把查询语句换为:SELECT OrderID FROM Orders where orderid = '1000 '
//无数据返回,返回no;
所以 可用hasrows 这种方法来使用在判断用户是否合法上面,具体可用在登陆界面。
还有另一种方法,是利用数据库数据进行比对
private static string SqlConnection connection = new SqlConnection(connectionString);//connectionString自己写
public bool GetPassWordByUserName(string UserName,string Password)
{
try
{
string truePass = null;
connection.Open();
SqlCommand command = new SqlCommand();
command.CommandText=String.Format("select password from UserInfo where userName={0}",UserName);
SqlDataReader reader = command.ExecuteReader();
while(reader.Read())
{
truePass =reader.Read["Password"].ToString();
}
}
catch
{}
finally
{
reader.Close();connection.Close();
}
if( (truePass==null) || (Password != truePass))
//用户名不存在或密码不正确
else
//密码正确
}
}