使用SQL Server查询从id为一位或两位的字符串中查找user_id

时间:2017-10-31 13:06:49

标签: c# sql asp.net .net sql-server

这是我的C#代码

private DataTable bindTrainingAttendeedGridView(string condition)
{
    DataTable dtGrid = new DataTable();
    con.ConnectionString = constr;

    string query = "select training_id,CONVERT(varchar(10),training_date,101) AS Training_date,topic,trainee from tbl_training_attendee where 1=1 " + condition;

    SqlCommand cmd = new SqlCommand(query, con);
    SqlDataAdapter sqladapter = new SqlDataAdapter(cmd);
    sqladapter.Fill(dtGrid);

    if (dtGrid.Rows.Count <= 0)
    {
        Label1.Text = "No Matching Training taken Entries Found, Thank You.";
    }
    else
    {
        Label1.Text = "";
    }

    //con.Open();
    return dtGrid;
}

protected void TrainingAttendenceGridViewBind(string condition)
{
    try
    {
        TrainingAttendeedGridView.DataSource = bindTrainingAttendeedGridView(condition);
        TrainingAttendeedGridView.DataBind();
    }
    catch (Exception er)
    {
        throw er;
    }
}

protected void training_attendence_condition()
{
    string condition = "";

    if (UserDropDownList.SelectedIndex > 0)
    {
        condition += "AND PATINDEX('%61%',trainee)!=0 or(PATINDEX('%,61,%',trainee)!=0 and PATINDEX('%,61%',trainee)!=0 and PATINDEX('%61,%',trainee)!=0 ) 
    }

    TrainingAttendenceGridViewBind(condition);
}

training_attendence_condition中,我可以编写一个查询来获取该行的user_id,但它只能显示两位数的数字记录,当取一位数时,它可以返回第二位数字。所以请帮助获得一个或两个数字

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试这个SQL查询,不需要training_attendence_condition就可以直接写出给定的查询:

select training_id,CONVERT(varchar(10),training_date,101) AS Training_date,topic,trainee from tbl_training_attendee where CONCAT( ',',trainee,',') like CONCAT( '%,','61',',%' )