製作要求可以增刪改查功能
啟動
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();
}