A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 郭张浩 中级黑马   /  2012-8-10 03:13  /  1591 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

运行不成功,
没有提示有语法错误。
实在找不出来原因。
求助
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.SqlClient;

  6. namespace _01ado
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine("请输入用户名");
  13.             string username =Console.ReadLine();
  14.             Console.WriteLine("请输入密码");
  15.             string password = Console.ReadLine();
  16.             string dataDir = AppDomain.CurrentDomain.BaseDirectory;
  17.             if (dataDir.EndsWith(@"\bin\Debug\")
  18.             || dataDir.EndsWith(@"\bin\Release\"))
  19.             {
  20.                 dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
  21.                 AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
  22.             }
  23.             using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;
  24.                     AttachDBFilename=|DataDirectory|\Database1.mdf;
  25.                     Integrated Security=True;User Instance=True"))
  26.             {
  27.                 conn.Open();
  28.                 using (SqlCommand cmd = conn.CreateCommand())
  29.                 {
  30.                     cmd.CommandText = "select * from User where UserName ='" + username + "'";
  31.                     using (SqlDataReader reader = cmd.ExecuteReader())
  32.                     {
  33.                         //用户名存在
  34.                         if (reader.Read())
  35.                         {
  36.                             string dbpsaaword = reader.GetString(reader.GetOrdinal("Password"));
  37.                             if (password == dbpsaaword)
  38.                             {
  39.                                 Console.WriteLine("登陆成功");
  40.                             }
  41.                             else
  42.                             {
  43.                                 Console.WriteLine("密码错误,登陆失败");
  44.                             }
  45.                         }
  46.                         else
  47.                         {
  48.                             Console.WriteLine("用户名错误");
  49.                         }
  50.                     }
  51.                     
  52.                 }
  53.             }
  54.             Console.WriteLine("打开数据库成功!");
  55.             Console.ReadKey();
  56.         }
  57.     }
  58. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
郑文 + 1

查看全部评分

2 个回复

倒序浏览
这是一种常见的错误,使用User当做表名的时候查询User表示 需要将表中括号括起来
cmd.commandtextt= "selelct * from [user] "
因为 user 在数据库中是关键字,所以会被当做关键字类编译,引发了错误,要主要命名的规则,最好加上T_user  或者其他的 避免与数据库中的关键字发生冲突

评分

参与人数 1技术分 +1 收起 理由
郑文 + 1

查看全部评分

回复 使用道具 举报
cmd.commandtextt= "selelct * from [user] " ,表的名字 被解释为关键字了,一般养成好习惯表名T_XX命名,或者和关键字冲突加[USER]

评分

参与人数 1技术分 +1 收起 理由
郑文 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马