SqlCommand对象

  早段时间就想弄清楚这几个方法怎么用:

 

  1.SqlCommand 常见的属性

  Sqlcommand属性为执行命令做准备。

  (1)CommandText属性:执行的SQL语句;

  (2)CommandType属性:解析CommandText的值;

  (3)Connection属性:解析CommandText的值;

  (4)CommandTimeout属性:设置需要执行多久停止

  (5)Parameters属性:设置参数

 

  2.SqlCommand 类构造函数

  SqlCommand cmd=new SqlCommand("select * from table",conn);

 

  3.SqlCommand 常见方法:ExecuteReader() 和ExecuteNonQuery()

  (1).ExecuteReader() 方法:它主要执行Select语句。将结果返回到SqlDataReader对象。

  如:

    SqlCommand cmd=new SqlCommand("select * from table where id='123' ", conn);

    conn.Open();

    SqlDataReader rd=cmd.ExecuteReader();

    rd.Read();

    lable.text=rd["ccc"].ToString();

    rd.Close();

    conn.Close();

  (2).ExecuteNonQuery()方法:此方法主要执行Insert、Update、Delete语句。返回值为该命令所影响的行数。

  (3).ExecuteScalar()方法:返回获得的聚合值(共有多少行数据)。

  (4).ExecuteXmlReader()方法:该方法用于执行返回数据源中某个XML数据流的FOR XML SELECT语句。ExecuteXMLReader命令只与SQL Server2000及更高版本兼容。

posted @ 2011-08-25 15:43  银色小猫  阅读(698)  评论(0编辑  收藏  举报