ExecuteNonQuery()方法
执行Insert Update Delete 语句的时候 返回受影响的行数
比如插入语句,则受影响的行数就是1,否则就是操作失败。
ExecuteScalar()方法
主要用在语句的聚合函数中,比如Max,Count等,就是获得聚合函数的值。
以下为程序代码举例:
执行Insert Update Delete 语句的时候 返回受影响的行数
比如插入语句,则受影响的行数就是1,否则就是操作失败。
ExecuteScalar()方法
主要用在语句的聚合函数中,比如Max,Count等,就是获得聚合函数的值。
以下为程序代码举例:
string Str_Value=string.Empty; SqlCommand MyCmd=new SqlCommand("select max(id) from tab ",MyConn); object obj=MyCmd.ExecuteScalar(); if(obj==System.DBNull.Value) Str_Value="0"; else Str_Value=MyCmd.ExecuteScalar().ToString(); |
ExecuteReader()方法
他主要就是返回一个SqlDataReader可以作为一个数据源使用。
举例
this.MyDataGrid.DataSource=MyCmd.ExecuteReader();
this.MyDataGrid.DataBind();