博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
黑马程序员——ADO.NET 初级登录练习
阅读量:4561 次
发布时间:2019-06-08

本文共 1582 字,大约阅读时间需要 5 分钟。

---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------

 

 

using System;

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

namespace OneDay4

{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入用户名:");
string username = Console.ReadLine();
Console.WriteLine("请输入密码:");
string password = Console.ReadLine();

using (SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;attachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();

using(SqlCommand cmd=conn.CreateCommand())
{
cmd.CommandText="select * from T_Users where UserName='"+username+"'";//
using(SqlDataReader reader=cmd.ExecuteReader())
{
if (reader.Read())
{
string dbpassword = reader.GetString(reader.GetOrdinal("PassWord"));//在给定列名称的情况下获取列序号
if (password == dbpassword)
{ Console.WriteLine("登陆成功!"); }
else
{ Console.WriteLine("密码错误!登录失败"); }
}
else
{ Console.WriteLine("用户名错误"); }
}
}
}
Console.WriteLine("ok");
Console.ReadKey();

}

}
}

当用户名和密码均输入正确时显示

当用户名输入正确,密码输入错误时显示

当用户名输入错误时,密码无论是否输入正确都会提示

 

文中特别强调using的使用方法,

     using关键字有两个重要的应用:1.作为指令,用于为命名空间创建别名或导入其它命名空间中定义的类型;

                                               2.作为语句,用于定义一个范围,在此范围的末尾将释放对象。

     using指令的两个用途: 1.允许在命名空间中使用类型,这样就不必在该命名空间中限定某个类型的使用,例如:using System.Date ;

                                    2. 为命名空间或类型创建别名,例如:using myClass1=NameSpace1.myClass;

     using语句的用途:提供能够确保正确使用IDisposeable(定义一种释放分配的资源的方法)的方便语法,

 

 

---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ---------------------- 详细请查看:http://net.itheima.com/

转载于:https://www.cnblogs.com/BlackHouse007/archive/2011/12/10/2283266.html

你可能感兴趣的文章
Data Guard 管理原理
查看>>
java处理excel-xlsx格式大文件的解决方案
查看>>
collections工具类
查看>>
jQuery EasyUI 表单插件 - Combobox 组合框
查看>>
高质量程序设计指南c++/c语言(18)--使用断言
查看>>
Spring的自动扫描与管理
查看>>
C++数据成员的内存布局
查看>>
英国体系环境下项目有什么特征(一)
查看>>
获取点击元素的绝对位置
查看>>
hdu1495 bfs搜索、模拟
查看>>
adb启动和关闭
查看>>
Linux磁盘分区的实用管理命令
查看>>
MFC控件编程之鼠标跟键盘消息
查看>>
【ASP.NET Core】给路由规则命名有何用处
查看>>
linux(4) vi编辑/删除、复制、粘贴 /bash shell 环境变量设置/数据流重定向 | 的用法...
查看>>
Ubuntu 下面安装 JDK1.7
查看>>
ECharts的x轴和y轴均使用数值类型
查看>>
Windows Phone 7Silverlight控件之--Panorama
查看>>
Liunx下的有关于tomcat的相关操作 && Liunx 常用指令
查看>>
PMP考试通过
查看>>