【IT168技术文档】
以下是不带参数的SQL语句执行方法的代码,它调用通用数据访问类(SqlHelper)执行 SqlHelper.ExecuteNonQuery()方法,使用示例为;
其中传递的4个参数:int val = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlstr, null);
“conn”—为链接字符;
“CommandType.Text”—为SQL命令类型。这里表示执行SQL命令文本形式;
“sqlstr”—为SQL命令字符;
“null”—是以数组形式提供SqlCommand命令中用到的参数列表,这里不是以数据形式,所以为空。
当ExecuteNonQuery()执行 select,结果总是返回-1,ExecuteNonQuery()对于 Update、Insert 和 Delete 语句,返回值为该命令所影响的行数。protected void btnExecuteSQL_Click(object sender, EventArgs e) { string sqlstr = "select * from “你的数据库名” where datediff(year,dateantime,getdate())=0 order by hints desc"; SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction)) { conn.Open(); int val = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlstr, null); Response.Write(" "); } }