0


我有一个基本的搜索页面来搜索员工的技能。

在 dbo.Emp 和 dbo.Skill 表之间有一个桥接表,称为“Dbo.Emp_skill_Bridge”。

这是我到目前为止所做的,只允许搜索桥接表。

示例:我需要输入“2”来获取员工的详细信息,并且只能从此表中获取。我有技能可以像“java”一样输入并从 Emp 表中获取员工列表。

dbo.Emp_Skill_Bridge
SkillID (FK) | EmpID (FK)

dbo.Skills
SkillName | SkillID(PK)

dbo.Emp
EmpID (PK) | Fname| LName | .....

所以我需要从 Skills 中搜索 Skills 并获得准确的员工详细信息。
更新:

protected void Button1_Click(object sender, EventArgs e)
        {

            String var2 = System.Configuration.ConfigurationManager.ConnectionStrings["KKSTechConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(var2);

            //SqlCommand cmd = new SqlCommand("select * from Emp_Skill_Bridge where SkillID like '%" + TextBox1.Text + "%' ", con);
            SqlCommand cmd = new SqlCommand("SELECT * FROM Emp_Skill_Bridge ESB INNER JOIN Emp E ON E.EmpId = ESB.EmpId INNER JOIN Skills S ON S.SkillID = ESB.SkillID WHERE ESB.SkillID LIKE '%" + TextBox1.Text + "%' OR ESB.SkillID LIKE '%" + TextBox1.Text + "%'", con);


            //string val = TextBox1.Text.ToString();

            con.Open();

            cmd.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds, "Emp");
            GridView1.DataSourceID = null;
            GridView1.DataSource = ds;
            GridView1.DataBind();
            con.Close(); 
        }

代码正在工作,但不是 GridView 没有显示数据..

4

3 回答 3

0

我们有 Int.Parse 方法,当提供的字符串可以解析为 int 时返回true ,否则返回false

因此,当您需要检查文本框中的输入值是字符串还是数字时,如果它的数字搜索基于SkillID列,或者它的字符串搜索基于列SkillName

string strcommand = string.Empty;
int num;
bool result = int.TryParse(txtboxsearch.Text, out num);
if (result == true)
{
strcommand = "SELECT * FROM Emp_Skill_Bridge ESB INNER JOIN  Emp E ON "+
          "E.EmpId = ESB.EmpId INNER JOIN  Skills S ON " +
          "S.SkillID = ESB.SkillID WHERE ESB.SkillID = " + TextBox1.Text ;
}
else
{
    strcommand = "SELECT * FROM Emp_Skill_Bridge ESB INNER JOIN  Emp E " + 
                 "ON E.EmpId = ESB.EmpId INNER JOIN  Skills S ON " +
                 "S.SkillID = ESB.SkillID WHERE ESB.SkillName  like '%" + TextBox1.Text + "%'" ;

}

SqlCommand cmd = new SqlCommand(strcommand ,con);
于 2012-08-13T08:27:51.110 回答
0

如果我正确理解了这个问题

 //From this query you will get the skill ID
select * from Emp_Skill_Bridge where SkillID like '% + TextBox1.Text +"%' "
//save the result of this query in a variable and pass it to the below query. Example I save the skill iD in a variable myskillid.

//To get the employee you have to pass teh skillID in the employee table, you have to write   another query
select * from Emp where skillID = myskillId

 //It is better to write a store procedure and use them in your code
 Create procedure getskill 
 @skill bigint -- change it to your column's type
 AS
    Declare @skillid bigint -- change it to your column's type
    set @skillid = ( select * from Emp_Skill_Bridge where SkillID like '%' + skillid +'%'  );
    select * from Emp where skillID = @skillid

  // and in your code you have to make the following changes
   SqlCommand cmd = new SqlCommand();
   cmd.CommandText = getskill; // procedure name
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Connection = con; // give the connection object
        con.Open(); // open the connection

        cmd.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
于 2012-08-13T06:27:17.410 回答
0

如果我对您的理解正确,您可以加入这两个表并编写一个查询,其中包含一个 OR 关键字,将用户输入与 WHERE 条件中的技能和 id 列进行比较

SELECT * FROM Emp_skill_Bridge ESB
INNER JOIN Emp E
ON E.Id = ESB.Id
INNER JOIN Skill S
ON S.Id = ESB.SkillId
WHERE SkillID LIKE '% + TextBox1.Text + %' 
OR SkillID LIKE '% + TextBox1.Text + %'

我试图在没有数据库结构的情况下即兴创作,但类似上述工作的东西应该可以工作。你可能需要稍微调整一下,因为我没有测试它。

于 2012-08-13T06:18:36.267 回答