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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

都会背了,就是不知道咋个理解它,看看我注释的那些有错,麻烦改下
代码如下:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SQL验证登录
{
    class Program
    {
        static void Main(string[] args)
        {
            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 username= Console.ReadLine();
            Console.WriteLine("输入密码");
            string password= Console.ReadLine();
            using (SqlConnection coon = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\C#程序\第二阶段\第二阶段\ADO.NET入门\AOD.net.mdf;Integrated Security=True")//连接数据库文件
            {
                coon.Open();//打开数据库连接
                using(SqlCommand cmd=coon.CreateCommand())//创建一个执行SQLserver d的命令
                {
                    cmd.CommandText = "select *from fuser where username='"+username+"'";// 执行命令
                    using( SqlDataReader reader=cmd.ExecuteReader())//这儿是啥:求详解
                    {
                        if (reader.Read())//想问一下reader的值是什么,是调用了read()方法后我知道返回的是一个bool值 ,老师说一条一条往下看,如同有匹配的就返回ture,没有就fase,我这不明白  求解
                        {
                            string dbpass = reader.GetString(reader.GetOrdinal("passwors"));我到这不知道reader是干啥的,给GetOrdinal传个列,GetOrdinal获得序号然后给GetString传个序号,GetString把序号转换成STRING使这样吗,可我还不知道reader的值是啥
                            if (password == dbpass)
                            {
                                Console.WriteLine("登录成功");
                            }
                            else { Console.WriteLine("密码错误"); }

                        }
                        else
                        {
                            Console.WriteLine("用户名错误");
                        }
                    }

                }
            }

            Console.WriteLine("测试正常");
            Console.ReadKey();

        }
    }
}


评分

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

查看全部评分

4 个回复

倒序浏览
cmd.CommandText = "select *from fuser where username='"+username+"'";
                    using( SqlDataReader reader=cmd.ExecuteReader())
                    {
                        if (reader.Read())
                        {
z这3行代码有什么关联,cmd 这个命令啥意思z这3行代码有什么关联,c
回复 使用道具 举报
SqlDataReader reader=cmd.ExecuteReader()   啥意思v
回复 使用道具 举报
彭清贵 发表于 2013-2-21 03:40
cmd.CommandText = "select *from fuser where username='"+username+"'";
                    using( Sq ...

sqlDatareader.read();返回一个bool类型的值,读取到的值非空,则返回True,否则 False

1.jpg (10.12 KB, 下载次数: 12)

1.jpg
回复 使用道具 举报
zj_alex 发表于 2013-2-21 08:41
sqlDatareader.read();返回一个bool类型的值,读取到的值非空,则返回True,否则 False
...

我不明白  SqlDataReader reader=cmd.ExecuteReade里的']
reader她里面有什么东西   cmd.ExecuteReade给她赋值的是啥,我输出一下  都是看不明白的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马