2011年11月28日 星期一

c# 資料庫查詢like寫法


/// <summary>
/// 查產品
/// </summary>
/// <param name="pTitle)">產品名稱</param>
public static DataTable GetPoducts(string pTitle)
{
DataTable dt = new DataTable();

using (SqlConnection conn = new SqlConnection(ConnectionString()))
{

string Sql = "select * from Products where 1 = 1 and Title=@Title";
SqlCommand cmd = new SqlCommand(Sql, conn);
cmd.Parameters.Add("@Title", SqlDbType.VarChar).Value = "%" + pTitle + "%";

SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);

cmd.Dispose();
da.Dispose();

}
return dt;
}