本帖最后由 ↖落葉下♀媃媚 于 2013-4-14 21:03 编辑
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
if(dataDir.EndsWith(@"\bin\Debug\") ||dataDir.EndsWith(@"\bin\Release\"))
{ dataDir =System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectory",dataDir);
}
Console.WriteLine("请输入用户名");
string user = Console.ReadLine();
Console.WriteLine("请输入密码");
string pass = Console.ReadLine();
using (SqlConnection conn = new SqlConnection(@"Data Source=.;
Initial Catalog=Datebase1;
Integrated Security=True"))
{
conn.Open();
using (SqlCommand cod=conn.CreateCommand())
{
//先到表中查输入的用户名对应的信息
cod.CommandText = "select * from table2 where Username='"+user+"'";
using(SqlDataReader reader=cod.ExecuteReader())
{
if (reader.Read())
{
//用户名存在
string s =reader.GetString(reader.GetOrdinal("Password"));
if (pass == s)//比较数据库中密码是否和输入的密码一致
{
Console.WriteLine("登录成功");
}
else
{
Console.WriteLine("密码错误,登录失败");
}
}
else//read直接返回false,就是用户名 不存在
{
Console.WriteLine("用户名错误");
}
}
}
Console.WriteLine("连接成功");
}
Console.ReadKey();
是不是密码有错误?怎么输入之后没反应呢?!
|