2012-08-13 108 views
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 | ..... 

所以我需要從技能中搜索技能並獲得精確的員工細節。
更新時間:

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不顯示數據..

+0

據我所知,你有文本框,當你輸入數字值(如2,3 ...)時,搜索將根據Emp表中的一些ID帶走,當輸入「java」時應該從Emp_Skill_Bridge表搜索,是這樣嗎? – Mourya 2012-08-13 07:51:03

+0

Ur足夠接近..用我的第一個代碼,我所能做的就是使用像(2,3 ..)這樣的數值進行搜索。但是我希望用戶輸入Java,asp.net等..我有隻有在文本框和一個btn_Search控制.. – Girish 2012-08-13 07:55:12

回答

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 + %' 

我試圖沒有數據庫結構即興創作,但像上述工作應該工作。你可能需要調整一下,因爲我沒有測試它。

+0

好吧,但我該怎麼做?任何可能的方式,你可以告訴我幾個代碼?將不勝感激,謝謝。 – Girish 2012-08-13 06:21:24

+0

這看起來確實不錯,但在ADO.NET連接中,我很困惑如何實現這個.. – Girish 2012-08-13 07:45:44

+0

'%+ TextBox1.Text +%'這部分將是一個參數@userInput,然後您將發送命令與「 addParameterWithValue「的值是TextBox1.Text – 2012-08-13 07:54:34

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; 
+0

只有一個疑問.. SkillID是一個INT類型在Emp_Skill_Bridge ..但技能列在dbo.Skills和員工的詳細信息在dbo.Emp .. 請查看我對我的問題的編輯以查看數據庫設計.. – Girish 2012-08-13 07:39:36

+0

@Girish,因此您可以將您的技能ID傳遞給Emp_Skill_Bridge表,並且可以獲取empID,然後您必須將其傳遞到emp表。爲什麼你讓你的設計如此複雜?只需在dbo.Emp表中添加技能ID即可解決問題 – 2012-08-13 08:03:15

+0

@WaqarJanjua您無法向Emp表添加skillID,因爲一名員工可以擁有多項技能。你認爲如何處理擁有5項技能的員工? – 2012-08-13 08:08:53

0

我們Int.Parse方法,這將返回時提供的字符串可以解析爲int和當它不能。

所以當你需要檢查,如果其編號搜索基於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); 
+0

那麼,我試過的上述方法正在工作,但沒有在我的GridView1控件中填充.. – Girish 2012-08-13 08:29:40

+0

然後更好地檢查DataSet是否不爲空。 ** da.Fill(ds); ** – Mourya 2012-08-13 09:01:40

+0

@Girish,您最好刪除** GridView1.DataSourceID = null; **嘗試運行代碼。 – Mourya 2012-08-13 09:13:24