14章 上机

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;                //SQL Server .NET 数据提供程序命名空间

namespace ConsoleApplication3
{
    class Program
    {
        //测试打开数据库的操作
        string strcoon = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
        public bool CheckUserInfo(string userName, string pwd, ref string strMsg)
        {
            //创建数据库连接
            SqlConnection conn = new SqlConnection(strcoon);
            try
            {
                //创建Sql语句
                string strSql = "select count(*) from Admin where LoginId='" + userName + "'and LoginPwd='" + pwd + "'";
                conn.Open();

                //创建Command 命令
                SqlCommand comm = new SqlCommand(strSql, conn);
                int iRet = (int)comm.ExecuteScalar();            //强制转换为int类型
                if (iRet != 1)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                //关闭数据库连接
                conn.Close();
            }
        }


        static void Main(string[] args)
        {
            string strMsg = "";
            Program p = new Program();
            Console.WriteLine("请输入用户名:");
            string a = Console.ReadLine();
            Console.WriteLine("请输入密码:");
            string b = Console.ReadLine();
            bool c = p.CheckUserInfo(a, b, ref strMsg);
            if (c)
            {
                Console.WriteLine("登陆成功!");
                p.show();
            }
            else
            {
                Console.WriteLine("登陆失败!");
            }
            Console.ReadLine();
        }

        public void show()
        {
            do
            {
                Console.WriteLine("=======请选择操作键======");
                Console.WriteLine("1. 统计学生人数");
                Console.WriteLine("2. 查看学生名单");
                Console.WriteLine("3. 按学号查询学生姓名");
                Console.WriteLine("4. 按姓名查询学生信息");
                Console.WriteLine("5. 修改学生出生日期");
                Console.WriteLine("6. 删除学生记录");
                Console.WriteLine("0. 退出");
                Console.WriteLine("=========================");
                Console.WriteLine("请选择:");
                int num = int.Parse(Console.ReadLine());
                switch (num)
                {
                    case 1:
                        Console.WriteLine("在校学生人数:" + show1());
                        continue;
                    case 2:
                        Console.WriteLine("2. 查看学生名单");
                        continue;
                    case 3:
                        Console.WriteLine("3. 按学号查询学生姓名");
                        continue; ;
                    case 4:
                        Console.WriteLine("4. 按姓名查询学生信息");
                        continue;
                    case 5:
                        Console.WriteLine("5. 修改学生出生日期");
                        continue;
                    case 6:
                        Console.WriteLine("6. 删除学生记录");
                        continue;
                    case 0:
                        Console.WriteLine("感谢使用!");
                        break;
                    default:
                        Console.WriteLine("输入错误");
                        continue;
                }
                break;
            } while (true);

        }
        public int show1()
        {
            SqlConnection conn = new SqlConnection(strcoon);
            try
            {
                string strSql = "select count(*) from Student";
                conn.Open();
                SqlCommand comm = new SqlCommand(strSql, conn);
                int iRet = (int)comm.ExecuteScalar();
                return iRet;
            }
            catch (Exception)
            {
                return -1;
            }
            finally
            {
                conn.Close();
            }
        }

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值