教您不帶參數的SQL語句執行的方法
SQL語句如果不帶參數,該如何執行呢?下面就將為您介紹不帶參數的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
【編輯推薦】