zoukankan      html  css  js  c++  java
  • 使用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} {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!");
                  }

  • 相关阅读:
    Windows上git输错一次密码不在提示输入密码
    JSON Web Token 简介
    Springboot+Shiro+Jwt实现权限管理
    Springboot配置外部容器使用JNDI读取数据源
    Springboot解决Main方法启动无法注入JNDI
    Springboot2.1.6版本部署resin4.0.62
    Java解决多线程无法@Autowired注入,手动获取Bean对象
    Linux设置Vim显示行号
    Linux使用wget后台下载
    排查生产环境CPU过高的问题
  • 原文地址:https://www.cnblogs.com/fl72/p/7759810.html
Copyright © 2011-2022 走看看