using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.SqlClient;namespace ConsoleApplication1{ class DBOperation { private static string conn = "Data Source=.;Initial Catalog=qq;Integrated Security=True"; //User ID=sa;Password=123456";如果Password為空,可以不寫 public static SqlConnection con = new SqlConnection(conn); string strMsg=""; #region 異常處理 public void Conn() { try { //打開數據庫 con.Open(); CheckUserInFo(); Console.WriteLine("打開成功"); Console.WriteLine(strMsg); } catch (Exception ex) { Console.WriteLine("異常:{0}", ex.Message); } finally { //關閉數據 con.Close(); Console.WriteLine("關閉成功"); Console.ReadLine(); } } #endregion #region public string CheckUserInFo() { Console.Write("請輸入QQ號:"); int ID = int.Parse(Console.ReadLine()); Console.Write("請輸入密碼:"); string pwd = Console.ReadLine(); string sql =string.Format("select QQid,password from qquser where qqid={0} and password={1}",ID,pwd); SqlCommand com = new SqlCommand(sql,con); int i = (int)com.ExecuteScalar(); if (i != 1) { strMsg = "輸入錯誤"; return strMsg; } else { strMsg = "登陸成功"; return strMsg; } } #endregion }}
7 回答

LEATH
TA貢獻1936條經驗 獲得超7個贊
你多對這個方法不熟悉 ExecuteScalar(),他的意思是返回第一行第一列的值,所以你在查詢的時候count(0) 查看該登錄名和密碼是否在數據庫中存在...
- 7 回答
- 0 關注
- 705 瀏覽
添加回答
舉報
0/150
提交
取消