代码展示:
利用方法来完成,而不是利用返回值
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QQ通讯
{
class Program
{
static void Main(string[] args)
{
//实例化game类
Game game = new Game();
game.index();
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace QQ通讯
{
class Game
{
SqlConnection connstr = new SqlConnection("Data Source=.;Initial Catalog=bg;Integrated Security=True");
//计数器
int x = 3;
public void index() {
while (true) {
login();
if (x == 0) {
Console.WriteLine("你的机会已用尽");
break;
}
}
}
public void login() {
//登录方法
Console.WriteLine("欢迎使用QQ通讯录");
Console.WriteLine("请输入账号");
String a = Console.ReadLine();
Console.WriteLine("请输入密码");
String b = Console.ReadLine();
String sql = "select count(*) from UserManagement where qqaccount = ('" + a + "') and qqpassword = ('" + b + "')";
connstr.Open();
SqlCommand cmd = new SqlCommand(sql, connstr);
int sca = (int)cmd.ExecuteScalar();
connstr.Close();
if (sca == 1) {
Console.WriteLine("登录成功!");
menu();
}
else if (sca == 0) {
x--;
Console.WriteLine("登录失败!");
Console.WriteLine("你还有" + x + "次机会");
}
}
public void menu()
{
Console.WriteLine("登录成功");
Console.WriteLine("1.添加用户\n2.删除用户\n3.修改用户\n4.查询用户\nq.退出程序");
String menu = Console.ReadLine();
//分支选择
while (true)
{
switch (menu)
{
case "1":
Console.WriteLine("//添加用户");
addUser();
break;
case "2":
Console.WriteLine("//删除用户");
deleteUser();
break;
case "3":
Console.WriteLine("//修改用户");
makeUser();
break;
case "4":
Console.WriteLine("//查询用户");
queryUser();
break;
case "q":
Console.WriteLine("//退出程序");
//退出程序
index();
break;
default:
Console.WriteLine("你输入的序号有误,回到菜单");
index();
break;
}
}
}
//添加用户功能
public void addUser() {
Console.WriteLine("请输入你要添加的用户名");
String name = Console.ReadLine();
Console.WriteLine("请输入你要添加的QQ号");
String qq = Console.ReadLine();
//连接
connstr.Open();
//添加sql语句
String sql = "insert into UserInfo(name,qq)values('" + name + "'," + qq + ") ";
//添加连接对象
SqlCommand cmd = new SqlCommand(sql,connstr);
int tj = (int)cmd.ExecuteNonQuery();
Console.WriteLine("受影响的行数是{0}", tj);
Console.WriteLine("添加用户{0}成功!", name);
//提供回到菜单功能
connstr.Close();
Console.WriteLine("是否回到主菜单(y/n)");
String s = Console.ReadLine();
if(s.Equals("y")){
menu();
}else if (s.Equals("n")){
return;
}
}
//删除用户功能
public void deleteUser() {
Console.WriteLine("请输入你要删除的用户名");
String name = Console.ReadLine();
//连接
connstr.Open();
//删除一整行对象
String sql = "delete from UserInfo where name = ('" + name + "')";
//添加连接对象
SqlCommand cmd = new SqlCommand(sql, connstr);
int tj = (int)cmd.ExecuteNonQuery();
Console.WriteLine("受到影响的行数是{0}", tj);
Console.WriteLine("删除用户{0}成功", name);
//提供回到菜单功能
connstr.Close();
Console.WriteLine("是否回到主菜单(y/n)");
String s = Console.ReadLine();
if (s.Equals("y"))
{
menu();
}
else if (s.Equals("n"))
{
return;
}
}
//修改用户功能
public void makeUser() {
Console.WriteLine("请输入你要修改的用户名");
String name = Console.ReadLine();
Console.WriteLine("请输入新的用户名");
String usernew = Console.ReadLine();
//连接
connstr.Open();
//修改一整行对象
String sql = "update UserInfo set name = '"+usernew+"'where name = '"+name+"'";
//添加连接对象
SqlCommand cmd = new SqlCommand(sql, connstr);
int tj = (int)cmd.ExecuteNonQuery();
Console.WriteLine("受到影响的行数{0}成功!", usernew);
connstr.Close();
//提供回到菜单功能
connstr.Close();
Console.WriteLine("是否回到主菜单(y/n)");
String s = Console.ReadLine();
if (s.Equals("y"))
{
menu();
}
else if (s.Equals("n"))
{
return;
}
}
//查询用户名
public void queryUser() {
Console.WriteLine("请输入你要查询的用户名");
String name = Console.ReadLine();
//连接
connstr.Open();
//修改一整行的对象
String sql = "select name,gender,qq,birthday from UserInfo where name = '" + name + "'";
//添加连接对象
SqlCommand cmd = new SqlCommand(sql, connstr);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
String res = string.Format("姓名:{0},QQ账号:{1}",reader["name"],reader["qq"]);
Console.WriteLine(res);
//提供回到菜单功能
connstr.Close();
Console.WriteLine("是否回到主菜单(y/n)");
String s = Console.ReadLine();
if (s.Equals("y"))
{
menu();
}
else if (s.Equals("n"))
{
return;
}
}
}
}
效果展示:
账户密码登录效果:
添加用户效果
删除用户功能
修改用户功能
查询用户功能
退出程序功能