• ADO中SqlCommand的三种执行


    1、ExecuteNonQuery()//返回受影响行数,如果多条就会累加 

    string connStr = @"server=WIN-20161229QGK\SQLEXPRESS;uid=PJ;pwd=123;database=MySchoolMoreData";
                using (SqlConnection conn=new SqlConnection (connStr))
                {
                    conn.Open();
                    string sql = string.Format("insert into grade values('{0}');select count(*)from grade",textBox1.Text.Trim ());
                    SqlCommand command = new SqlCommand(sql,conn);
                    int num = (int)command.ExecuteNonQuery();
                    MessageBox.Show(num.ToString());
                }

    2、ExecuteScalar()//返回查询结果的第一行第一列的值

    string connStr = @"server=WIN-20161229QGK\SQLEXPRESS;uid=PJ;pwd=123;database=MySchoolMoreData";
                using (SqlConnection conn=new SqlConnection (connStr))
                {
                    conn.Open();
                    string sql = string.Format("insert into grade values('{0}');select count(*)from grade",textBox1.Text.Trim ());
                    SqlCommand command = new SqlCommand(sql,conn);
                    int num = (int)command.ExecuteScalar();
                    MessageBox.Show(num.ToString());
                }

    3、ExecuteReader()//读取数据表,并存入缓存,就是数据读取器。把数据一条条的读出

    string connStr = @"server=WIN-20161229QGK\SQLEXPRESS;uid=PJ;pwd=123;database=MySchoolMoreData";
                using (SqlConnection conn=new SqlConnection (connStr))
                {
                    conn.Open();
                    string sql = "select classid,classname from grade";
                    SqlCommand command = new SqlCommand(sql,conn);
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())//判断有没有下一个数据,类似于迭代器
                    {
                         ListViewItem lv = new ListViewItem(reader["classid"].ToString());
                        lv.SubItems.Add(reader["classname"].ToString());
                        listView1.Items.Add(lv);
                    }
                }

  • 相关阅读:
    CentOS+nginx+uwsgi+Python 多站点环境搭建
    nginx实现负载均衡
    高性能Mysql主从架构的复制原理及配置详解
    centos 安装php
    Java知识总结-6
    Java知识总结-5
    Java知识总结-4
    Java知识总结-3
    Java知识总结-2
    java知识总结-1
  • 原文地址:https://www.cnblogs.com/pengJk/p/6336718.html
Copyright © 2020-2023  润新知