不带参数的SQL语句执行方法

以下是不带参数的SQL语句执行方法的代码,它调用通用数据访问类(SqlHelper)执行 SqlHelper.ExecuteNonQuery()方法,使用示例为;

int val = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlstr, null);

其中传递的4个参数:

“conn”—为链接字符;

“CommandType.Text”—为SQL命令类型。这里表示执行SQL命令文本形式;

“sqlstr”—为SQL命令字符;

“null”—是以数组形式提供SqlCommand命令中用到的参数列表,这里不是以数据形式,所以为空。


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(" ");
}
}

当ExecuteNonQuery()执行 select,结果总是返回-1,ExecuteNonQuery()对于 Update、Insert 和 Delete 语句,返回值为该命令所影响的行数。对于其他所有类型的语句,返回值为 -1要了解更多情况可查看本人网站的相关内容。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-03
  • 2022-12-23
  • 2021-10-15
  • 2022-12-23
  • 2022-12-23
  • 2021-07-18
猜你喜欢
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-10
  • 2021-08-01
  • 2021-08-06
相关资源
相似解决方案