搜索项目使用sqlQuery C#ASP.NET和在gridview中显示

我看不到在gridview中显示的任何数据。 看不到任何搜索结果。 我正在尝试使用sqlparameter。正如你所看到的,我的sqlQuery是一个很长的。 如果我输入'卡',它应该找到'大卡'或'小卡'值的item_description。 这里是我的代码:搜索项目使用sqlQuery C#ASP.NET和在gridview中显示

searchWord = Request.Cookies["Search"].Value; 

searchType = Request.Cookies["Display"].Value;

string sqlQuery;

string custSearch = searchWord;

DataTable dt = new DataTable();

SqlConnection sc = new SqlConnection(GetConnectionString());

sqlQuery = "SELECT Player.player_id AS 'ID', Player.fname AS 'First name', "

+"Player.lname AS 'Last name', Player.sport AS 'Sports',"

+"Player.position AS 'Position', Player.debut_year AS 'Debut year',"

+"Player.prof_year AS 'Major year', Player.birth_date AS 'Date of birth',"

+"Player.birth_place AS 'Place of birth', Player.team_f AS 'Current team',"

+"Player.team_s AS 'Past team1', Player.team_t AS 'Past team2',"

+"Player.living AS 'Death status' "

+"FROM Player WHERE (Player.fname LIKE '@SearchPam') "

+"OR (Player.lname LIKE '@SearchPam') OR (Player.sport LIKE '@SearchPam') "

+"OR (Player.position LIKE '@SearchPam') OR (Player.team_f LIKE '@SearchPam') "

+"OR (Player.team_s LIKE '@SearchPam') OR (Player.team_t LIKE '@SearchPam');";

try

{

sc.Open();

string result = sqlQuery;

SqlCommand cmd = new SqlCommand(result, sc);

cmd.Parameters.AddWithValue("@SearchPam", custSearch);

SqlDataAdapter adapter = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

adapter.Fill(ds);

GridView1.DataSource = ds;

GridView1.DataBind();

}

catch{...}

finally{sc.Close();}

public string GetConnectionString()

{

return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

}

回答:

您可以使用下面的查询,我想你会开始使用此预期输出:

string sql = "SELECT * FROM TableA WHERE Col1 LIKE @SearchPam"; 

.

.

cmd.Parameters.AddWithValue("@SearchPam", "%" + txtSearch.Text + "%");

OR

string sql = "SELECT * FROM TableA WHERE Col1 LIKE '%' + @SearchPam+ '%'"; 

.

.

cmd.Parameters.AddWithValue("@SearchPam", txtSearch.Text);

以上是 搜索项目使用sqlQuery C#ASP.NET和在gridview中显示 的全部内容, 来源链接: utcz.com/qa/263613.html

回到顶部