zoukankan      html  css  js  c++  java
  • 使用ADO.NET访问数据库

    string sqlcon="Data Source=.;Initial Calalog=MySchool;User ID=sa;Pwd=.";


    Connection:打开数据库连接
       程序与数据库沟通的桥梁

       SqlConnection con=new SqlConnection(sqlcon);
       try
       {
        //可能发生异常的代码
        con.Open();
       }
       catch(Exception ex)
       {
        //捕获异常
        Console.WriteLine(ex);
       }
       finally
       {
        con.Close();
        //永远都会被执行
       }


    Command:向数据库发送命令,提交SQL命令并从数据源中返回结果
      string sql="select count(*) from Student where StudentNo='"+username+"' and LoginPwd='"+password+"'";
      //向数据库发送一条SQL语句
      SqlCommand command=new SqlCommand(sql,con);
      //结果
      int count=(int)command.ExecuteScalar();
      if(count>0)
      {
       Console.WriteLine("登录成功");

      }else
      {
       Console.WriteLine("查无此人");
      }

  • 相关阅读:
    request相关
    C#请求接口
    qml_base
    web
    entry
    listbox
    Canvas
    pickle
    c#枚举
    数据结构——树
  • 原文地址:https://www.cnblogs.com/fl72/p/7759787.html
Copyright © 2011-2022 走看看