//关键字 'User' 附近有语法错误。 求教 错哪了?
using System.Data.SqlClient;namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
////神奇代码:
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"\bin\Debug\")
|| dataDir.EndsWith(@"\bin\Release\"))
{
dataDir = (dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
}
//编写登录代码:
Console.WriteLine("请输入用户名:");
string username = Console .ReadLine();
Console.WriteLine("请输入密码:");
string password = Console. ReadLine();
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\app.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
= "select * from User where Username =='"+username+"'";
using (SqlDataReader reader = cmd.ExecuteReader()) //关键字 'User' 附近有语法错误。
{
if (reader.Read())
{
string dbpassword = reader.GetString(reader.GetOrdinal("Password"));
if (password == dbpassword)
{
Console.WriteLine("登陆成功!");
}
else
{
Console.WriteLine("密码错误!");
}
}
else
{
Console.WriteLine ("用户名错误!");
}
}
}
}
Console.ReadKey();
}
}
}