zoukankan      html  css  js  c++  java
  • 第十三章博客

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.SqlClient;

    namespace 第十三章
    {
    class ConnectionDB
    {
    //准备连接字符串
    static string str = "Data Source=.;Initial Catalog=MySchool;User ID=sa; password=1";
    //准备连接对象
    public SqlConnection con = new SqlConnection(str);
    //连接数据库
    public void OpenDB()
    {
    try
    {
    con.Open();

    }
    catch (Exception ex)
    {
    Console.WriteLine("发生异常:"+ex);
    }
    }
    //关闭数据库
    public void CloseDB()
    {
    con.Close();
    }
    }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace 第十三章
    {
    class User
    {
    ConnectionDB db = new ConnectionDB();
    //登录方法
    public void Login(string loginID, string loingPwd)
    {
    //如何登录
    //步骤一:创建一个command对象
    string sql = "SELECT CONT(1) FROM Login WHERE LoginID='" + loginID + "'AND LoginPwd'" + loingPwd + "'";
    Console.WriteLine(sql);
    //打开数据库连接
    db.OpenDB();
    SqlConnection cmd = new SqlConnection(sql, db.con);
    //步骤二:接受数据库返回值
    int count = (int)cmd.ExecuteScalar();
    //步骤三:判断返回值
    if (count > 0)
    {
    Console.WriteLine("登录成功!");

    Console.ReadLine();
    }
    }

    }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.SqlClient;

    namespace 第十三章
    {
    class Program
    {
    static void Main(string[] args)
    {

    #region 数据库连接
    ////步骤1:配置参数(连接到的服务器,连接的数据库名称,用户,密码)
    //String constr = "Data Source=.;Initial Catalog=MySchool;User ID=sa;pwd=1";
    ////步骤2:创建Connection对象连接数据库(SqlConnection)
    //SqlConnection con = new SqlConnection(constr);
    ////步骤3:打开数据库连接
    //con.Open();
    //Console.WriteLine("打开MySchool数据库连接成功!");
    //con.Close();
    //Console.WriteLine("关闭MySchool数据库连接成功!");
    //#endregion
    //Console.ReadLine();
    #endregion
    #region 捕获异常
    // //步骤1:配置参数(连接到的服务器,连接的数据库名称,用户,密码)
    // String constr = "Data Source=.;Initial Catalog=MySchool;User ID=sa;pwd=1";
    // //步骤2:创建Connection对象连接数据库(SqlConnection)
    // SqlConnection con = new SqlConnection(constr);
    // try
    // {
    // //将可能发生运行时异常的代码放入到try快中
    // con.Open();
    // Console.WriteLine("打开MySchool数据库连接成功!");
    // //如果会发生异常,那么将发生异常的类型去和catch块中的异常类型匹配,如果匹配上,那么将会执行catch块中的代码

    // }
    // //catch块可以有N个
    // catch (SqlException ex)
    // {
    // Console.WriteLine("EqlException异常:"+ex);
    //}
    // //顶级异常:Exception必须写在最后
    // catch (Exception ex);
    // {
    // Console.WriteLine("EqlException异常:"+ex);
    //}


    // try
    // {

    // }
    // catch (Exception)
    // {

    // throw;
    // }
    // }

    //}

    #endregion
    #region 登录
    Console.WriteLine("请输入用户名:");
    string loginID = Console.ReadLine();
    Console.WriteLine("请输入密码:");
    string loginpwd = Console.ReadLine();
    User user = new User();
    user.Login(loginID, loginpwd);
    #endregion

    }
    }
    }

  • 相关阅读:
    Linux-Bond-Configure
    Kvm学习文档记录
    Can't locate Log/Dispatch.pm in @INC
    sas2ircu工具信息收集及磁盘定位
    python 之tornado 入门
    zabbix 监控 tomcat jmx
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
  • 原文地址:https://www.cnblogs.com/ringqq/p/10169491.html
Copyright © 2011-2022 走看看