| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 489 人关注过本帖
标题://关键字 'User' 附近有语法错误。 求教 错哪了?
只看楼主 加入收藏
hellation
Rank: 2
等 级:论坛游民
帖 子:38
专家分:56
注 册:2015-4-11
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
//关键字 '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();
        }

    }
}
搜索更多相关主题的帖子: 关键字 
2015-04-11 19:02
over1230
Rank: 8Rank: 8
等 级:贵宾
威 望:25
帖 子:113
专家分:897
注 册:2009-10-9
收藏
得分:20 
= "select * from User where Username =='"+username+"'";
 sql 语句里面,判断=,只需要一个等号,不需要两个
= "select * from User where Username ='"+username+"'";
2015-04-13 16:28
hellation
Rank: 2
等 级:论坛游民
帖 子:38
专家分:56
注 册:2015-4-11
收藏
得分:0 
回复 2楼 over1230
谢谢    我之后找到解决办法了    是user 是数控据库的关键字  所以必须用[]括起来就可以了!  还是的谢谢你!
2015-04-14 17:47
快速回复://关键字 'User' 附近有语法错误。 求教 错哪了?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028105 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved