SQL

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

--------------------------------------------------------------------------------------------
        第十四章:使用ADO.NET查询和操作数据

StringBuilder类: 用来定义可变字符串
            StringBuilder sb = new StringBuilder("");
            //追加字符串
            sb.Append("World");
            sb.Append("!");
            //W2orld
            sb.Insert(2, "2");
            //原字符串:Wo2rld!      截取之后:W2rld! 
            sb.Remove(1, 2);
            //ToString()
            Console.WriteLine(sb.ToString());
查询学生记录数
  //打开数据库连接
                    con.Open();
                    //使用StringBuilder追加SQL语句
                    StringBuilder sb = new StringBuilder();
                    sb.Append("select ");
                    sb.Append(" Count(*) ");
                    sb.Append(" from ");
                    sb.Append("[Student]");
                    Console.WriteLine(sb.ToString());
                    //创建一个SqlCommand对象
                    SqlCommand com = new SqlCommand(sb.ToString(),con);
                    Console.WriteLine((int)com.ExecuteScalar());

DataReader:从数据源中检索只读、只进的数据流,每次读取一行数据

     StringBuilder sb = new StringBuilder();
                    sb.AppendLine("select");
                    sb.AppendLine("[StudentNo]");
                    sb.AppendLine(",[StudentName]");
                    sb.AppendLine("from");
                    sb.AppendLine("[Student]");
                    SqlCommand com = new SqlCommand(sb.ToString(), con);
                    //从数据源中检索只读、只进的数据流
                    return com.ExecuteReader();


                    SqlDataReader reader=GetStudentInfo();
                 while (reader.Read())
                 {
                     Console.WriteLine("{0}\t{1}",reader["StudentNo"],reader["StudentName"]);
                 }
                 reader.Close();
                 
ExecuteNonQuery():
      StringBuilder sb = new StringBuilder();
                    sb.AppendLine("Insert into");
                    sb.AppendLine("[Grade]([GradeName])");
                    sb.AppendLine("Values('" + gradeName + "')");
                    //3.创建一个SqlCommand
                    SqlCommand com = new SqlCommand(sb.ToString(),con);
                    //4.返回执行结果
                    return com.ExecuteNonQuery();

                    Student stu = new Student();
              Console.WriteLine("请输入年级名称:");
              string gradename = Console.ReadLine();
              int count = stu.AddGrade(gradename);
              if (count > 0)
               {
                   Console.WriteLine("success!");
               }
              else
              {
                  Console.WriteLine("success mother!");
              }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值