文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>SqlCommand对象ExecuteNonQuery()、ExecuteScalar()、ExecuteReader()区别

SqlCommand对象ExecuteNonQuery()、ExecuteScalar()、ExecuteReader()区别

时间:2011-02-26  来源:让燕子飞

ExecuteScalar()方法用于查询,并返回查询所返回的结果集中第一行的第一列,因为不能确定返回值的类型,所以返回值是object类型;
     应用:一般用于查询表中有多少条数据,或获取最大值等;
               对于有自动增长主键列(Id)的表来说,可以使用ExecuteScalar()方法来获取新加入表主键Id值;
             

using (SqlConnection conn = new SqlConnection(sqlconString))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "Insert Into T_Users (UserName,Password) output Inserted.Id values ('admin','889988')";
                    //cmd.ExecuteScalar();
                    int resultRow = Convert.ToInt32(cmd.ExecuteScalar());
                    Console.WriteLine("表T_Users中最后的Id值:{0}", resultRow);
                }
            }

ExecuteReader()方法执行查询,返回有多行结果集的查询;reader的GetString、GetInt32等方法只接受整数参数,也就是序号,用GetOrdinal方法根据列名动态得到序号;

相关阅读 更多 +
排行榜 更多 +
方块枪战战场安卓版

方块枪战战场安卓版

飞行射击 下载
战斗火力射击安卓版

战斗火力射击安卓版

飞行射击 下载
空中防御战安卓版

空中防御战安卓版

飞行射击 下载