使用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("查无此人");
  }

原文地址:https://www.cnblogs.com/fl72/p/7759787.html