Definition---------------------------------------------
public static string TraceErrorSql(string strSqlCmd, SqlParameterCollection SqlParamsCollection)
{
if(SqlParamsCollection != null)
for(int i=0;i<SqlParamsCollection.Count;i++)
{
if(SqlParamsCollection[i] != null)
strSqlCmd = strSqlCmd.Replace(SqlParamsCollection[i].ParameterName, "'" + SqlParamsCollection[i].Value + "'");
}
return strSqlCmd;
}
}
Usage------------------------------------------------------------
public DataTable getProductsByName(string productType,string productName) {
SqlConnection l_SqlConnenction = new SqlConnection();
l_SqlConnenction.ConnectionString = "workstation id=OVERMIND;packet size=4096;user id=sa;password=sa;data source=OVERMIND;persist security info=False;initial catalog=wztj";
l_SqlConnenction.Open();
string SelectCommandText = "select * from Product where productType=@productType and name like @produtName";
SqlCommand l_SqlCommand = new SqlCommand(SelectCommandText,l_SqlConnenction);
l_SqlCommand.Parameters.Add("@productType",productType);
SqlParameter par1 = new SqlParameter("@produtName",SqlDbType.VarChar,500);
par1.Value ="%"+ productName+"%";
l_SqlCommand.Parameters.Add(par1);
string sql = TestSQL.TraceErrorSql(SelectCommandText,l_SqlCommand.Parameters);
DataSet dataset = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = l_SqlCommand;
adapter.Fill(dataset,"product");
DataTable dt = dataset.Tables["product"];
l_SqlConnenction.Close();
return dt;
}