运行不成功,
没有提示有语法错误。
实在找不出来原因。
求助
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data.SqlClient;
- namespace _01ado
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("请输入用户名");
- string username =Console.ReadLine();
- Console.WriteLine("请输入密码");
- string password = Console.ReadLine();
- 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);
- }
- 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 User where UserName ='" + username + "'";
- using (SqlDataReader reader = cmd.ExecuteReader())
- {
- //用户名存在
- if (reader.Read())
- {
- string dbpsaaword = reader.GetString(reader.GetOrdinal("Password"));
- if (password == dbpsaaword)
- {
- Console.WriteLine("登陆成功");
- }
- else
- {
- Console.WriteLine("密码错误,登陆失败");
- }
- }
- else
- {
- Console.WriteLine("用户名错误");
- }
- }
-
- }
- }
- Console.WriteLine("打开数据库成功!");
- Console.ReadKey();
- }
- }
- }
复制代码 |