第十四章 使用ADD.NET访问数据库

来源:互联网 发布:淘宝如何营销推广 编辑:程序博客网 时间:2024/05/22 12:25
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.SqlClient;namespace ConsoleApplication1{    class Program    {        private const string ent = @"Data Source=.;Initial Catalog=MySchool;Integrated Security=True; User ID=ru;Password=12";        static void Main(string[] args)        {            Program Q = new Program();            Q.su();                       string names = "Admin";            string pass = "0";            string strMsg = "信息";            bool b = Q.CheckUserInfo( names, pass,ref strMsg);            if (b)            {                Console.WriteLine(strMsg);            }            else            {                Console.WriteLine(strMsg);            }            Console.ReadLine();        }        public void su()        {            Console.WriteLine("请输入用户名:");            string name = Console.ReadLine();            Console.WriteLine("请输入密码:");          int pass =int.Parse(Console.ReadLine());          if (pass != 000)          {              Console.WriteLine("登录失败");          }          else          {              Console.WriteLine("登录成功");              cg();          }        }        public void cg()        {            Console.WriteLine("==========请选择操作键==========");            Console.WriteLine("1.统计学生人数");            Console.WriteLine("2.查看学生名单");            Console.WriteLine("3.按学号查询学生姓名");            Console.WriteLine("4.按姓名查询学生信息");            Console.WriteLine("5.修改学生出生日期");            Console.WriteLine("6.删除学生记录");            Console.WriteLine("7.新增年纪记录");            Console.WriteLine("8.退出");            Console.WriteLine("================================");          int m=int.Parse(Console.ReadLine());            switch(m){                case 1:                 Console.WriteLine("在校学生人数:"+qun());                    break;                case 2:                    break;                case 3:                    break;            case 4:                    break;                case 5:                                        break;                case 6:                    break;                case 7:                    break;                case 8:                    Console.WriteLine("退出!");                    break;                default:                    Console.WriteLine("输入错误,请重新输入:");                    break;                            }        }        public int qun()        {            SqlConnection tuy = new SqlConnection(ent);            try            {                tuy.Open();                string re = "select count(StudentNo) from dbo.Student  ";                    SqlCommand  du=new SqlCommand(re,tuy);                int we=(int)du.ExecuteScalar();                return we;            }            catch(Exception ex)            {                Console.WriteLine(ex.Message);                return -1;            }            finally            {           tuy.Close();           Console.WriteLine("关闭语句");            }         }            public bool CheckUserInfo(string names,string pass,ref string strMsg )            {            SqlConnection connection = new SqlConnection(ent);            try            {                connection.Open();                string rr = "select COUNT(*)as 总数 from [MySchool].[dbo].[Admin] where LoginId='" + names + "'and LoginPwd='" + pass + "'";                               Console.WriteLine("打开数据库连接");                SqlCommand comm = new SqlCommand(rr, connection);                int ww = (int)comm.ExecuteScalar();                              Console.WriteLine("打开"+ww+"连接");                if (ww != 1)                {                        strMsg = "输入无效";                    return false;                }                else                {                    return true;                }            }            catch (Exception ex)            {                                Console.WriteLine("出现异常:" + ex.Message);                Console.WriteLine("出现了问题");                strMsg = "发生异常";                return false;            }            finally            {            connection.Close();            Console.WriteLine("关闭数据库连接成功");            }                              }            }}

0 0
原创粉丝点击