日期:2014-05-17 浏览次数:20439 次
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace ConsoleApplication2 { 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 conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True;")) { conn.Open(); Console.WriteLine("Open SQLServer Success!"); 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")); Console.WriteLine(dbpassword); Console.WriteLine(password); if(dbpassword == password) { Console.WriteLine("登陆成功!"); }else { Console.WriteLine("密码错误!"); } } else { Console.WriteLine("用户不存在"); } } Console.WriteLine("Success!"); } } Console.ReadKey(); } } }