注册 登录
编程论坛 C# 论坛

//关键字 'User' 附近有语法错误。 求教 错哪了?

hellation 发布于 2015-04-11 19:02, 489 次点击
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();
        }

    }
}
2 回复
#2
over12302015-04-13 16:28
= "select * from User where Username =='"+username+"'";
 sql 语句里面,判断=,只需要一个等号,不需要两个
= "select * from User where Username ='"+username+"'";
#3
hellation2015-04-14 17:47
回复 2楼 over1230
谢谢    我之后找到解决办法了    是user 是数控据库的关键字  所以必须用[]括起来就可以了!  还是的谢谢你!
1