static void Main(string[] args) { Console.WriteLine("輸入用戶編號:"); string cusernum = Console.ReadLine(); Console.WriteLine("輸入用戶名:"); string cusername = Console.ReadLine(); string connString = @"Data Source=.; Initial Catalog=lh0216; User ID=sa;pwd=Founder123"; //出了using括號objConnection類會被自動釋放 using (SqlConnection objConnection = new SqlConnection(connString)) { objConnection.Open(); using (SqlCommand objCmd = objConnection.CreateCommand()) { objCmd.CommandText = "select * from TBZG where czgbh='" + cusernum + "'" ; using (SqlDataReader objReader = objCmd.ExecuteReader()) { //結果集不為空 if (objReader.Read()) { string cValue = objReader.GetString(objReader.GetOrdinal("czgxm")); //檢查手工輸入的姓名與數據庫中查詢的名稱是否相同 if (cValue == cusername) { Console.WriteLine("你好{0}",cusername); } } else { Console.WriteLine("用戶名不存在。"); } } } } Console.ReadKey(); }
新聞熱點
疑難解答