C#通訊錄設計

wzi10發表於2020-12-07

製作要求可以增刪改查功能

啟動

   public void start()
    {
        Console.WriteLine("程式啟動了,這是start方法");


        //判斷是否登入成功
        if (login())
        {
            Console.WriteLine("登入成功!");
            Menu();//顯示選單
        }
        else
        {
            Console.WriteLine("賬號或密碼錯誤");
        }



        Console.ReadKey();
    }

登入功能方法

   public bool login()
    {
        bool reaf=false;
        Console.WriteLine("登陸功能,被呼叫的login方法");
        // 接收鍵盤輸入,使用者名稱,密碼
        Console.WriteLine("請輸入使用者名稱:");
        usr_name = Console.ReadLine();
        Console.WriteLine("請輸入密碼:");
        string pwd = Console.ReadLine();

        // 去資料表中查詢(需要使用者表,使用者名稱列,密碼列)
        string sql = "select COUNT(*) from QQUser where QQID=" + usr_name + " and PassWord='" + pwd + "';";
        SqlCommand cmd = new SqlCommand(sql, connection);
        connection.Open();
        string a = cmd.ExecuteScalar().ToString();
        Console.WriteLine(a);
        connection.Close();
        if (a.Equals("1"))
        {
            reaf = true;
        }
        else
        {
            reaf = false;
        }
        // 如果查詢成功,代表登陸成功,給一個返回值
        // 如果查詢不成功,登陸失敗,給一個返回值
        return reaf;
    }

顯示選單方法

public void Menu()
{
    Console.WriteLine("1.查詢好友");
    Console.WriteLine("2.刪除好友");
    Console.WriteLine("3.新增好友");
    Console.WriteLine("4.修改好友");
    Console.WriteLine("q.退出");

    Console.WriteLine("請選擇你要操作的選擇");
    string a= Console.ReadLine();
    switch (a) { 
        case "1":
            select_from();//查詢方法
            break;
        case "2":

            break;
        case "3":

            break;
        case "4":

            break;
        default:
            break;
    }
}

 

查詢方法

public void select_from() {
    string sql = "select * from BaseInfo where qqid=" + usr_name + ";";
    string sql2 = "select count(*) from BaseInfo where qqid=" + usr_name + ";";
    connection.Open();
    
    SqlCommand cmd2 = new SqlCommand(sql2, connection);
    int a = Convert.ToInt32(cmd2.ExecuteScalar().ToString());
    if (a > 0)
    {
        SqlCommand cmd = new SqlCommand(sql, connection);
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            Console.WriteLine("QQ號:" + sdr["qqid"]);
            Console.WriteLine("暱稱:" + sdr["nickname"]);
            Console.WriteLine("年齡:" + sdr["age"]);
        }

    }
    else
    {
        Console.WriteLine("你沒有好友");
    }
    
    connection.Close();
}

相關文章