---------------------- 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/