如何將存儲過程與參數一起作爲字符串傳遞給函數?將存儲過程作爲字符串傳遞
我想這個代碼,但沒有運氣..
這是業務訪問層的代碼
try
{
string Query_string = "SP_InsertOffer_Tab @offer_name ='" + this.offer_name +"', @offer_price = " + this.offer_price + ",@start_date = '" + this.start_date +
"',@end_date = '" + this.end_date + "'";
int result = DbAcess.Insert_Query(Query_string);
return result;
}
catch (Exception ex)
{
throw ex;
}
finally
{
DbAcess = null;
}
數據庫層的代碼是相反如下
public int Insert_Query(string strSQL)
{
SqlConnection con = new SqlConnection();
con = OpenConnection();
try
{
sqlcmd = new SqlCommand();
sqlcmd.Connection = con;
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = strSQL;
int Result = sqlcmd.ExecuteNonQuery();
return Result;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
那麼,什麼是例外? – 2013-03-13 19:53:16
不要這樣做:catch(Exception ex){throw ex; }'。 – Oded 2013-03-13 19:54:09
請閱讀[SQL注入](http://en.wikipedia.org/wiki/SQL_injection) - SQL的字符串連接不好。 – Oded 2013-03-13 19:54:47