2016-11-13 189 views
0

我想下面的2個表連接在一起:連接兩臺SQL Server表

CREATE TABLE [dbo].[Groups] 
(
    [Group_Id] INT IDENTITY (1, 1) NOT NULL, 
    [Group_Name] NVARCHAR (50) NULL, 
    [Group_Desc] NVARCHAR (50) NULL, 

    PRIMARY KEY CLUSTERED ([Group_Id] ASC) 
); 

CREATE TABLE [dbo].[Events] 
(
    [Event_Id] INT IDENTITY (1, 1) NOT NULL, 
    [Event_Group_Id] INT null, 
    [Event_Type] NVARCHAR (50) NULL, 
    [Event_Title] NVARCHAR (50) NULL, 

    PRIMARY KEY CLUSTERED ([Event_Id] ASC), 

    CONSTRAINT fk_event_group 
     FOREIGN KEY (Event_Group_Id) REFERENCES Groups (Group_Id) 
); 

第一個問題:有我創建的表是否正確?

我已經能夠使用下面的代碼添加到Events,但是我還沒有能夠成功地將Groups的PK作爲Events中的FK連接。

有沒有人有任何建議?

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BallinoraDBConnectionString1"].ConnectionString); 
     conn.Open(); 
     string findGroupOfEvent = "SELECT Group_Id FROM Groups WHERE Group_Name ='" + DropDownListEventGroup.SelectedItem.ToString() + "'"; 
     int groupId = Convert.ToInt32(findGroupOfEvent); 
     string insertQuery = "INSERT INTO Events (Event_Group_Id, Event_Type, Event_Title) VALUES (@GroupEventID, @Type, @Title)"; 
     SqlCommand com = new SqlCommand(insertQuery, conn); 
     com.Parameters.AddWithValue("@GroupEventID", groupId); 
     com.Parameters.AddWithValue("@Type", DropDownListEventType.SelectedItem.ToString()); 
     com.Parameters.AddWithValue("@Title", TextBoxET.Text); 
     com.ExecuteNonQuery(); 
     Response.Redirect("ViewEvents.aspx"); 
     Response.Write("Registration is successful"); 
     conn.Close(); 

UI

Adding Dropdown Items

+3

'但我一直沒能組的PK成功連接爲FK在Events.' <=你這是什麼意思?你也應該把你的SqlConnections封裝在''using''塊中,並且每次你想與數據庫進行交互時都要創建一個新的實例(沒有一個可重用的中心SqlConnection!)。另請參見[最佳實踐 - 執行SQL語句](http://stackoverflow.com/documentation/.net/3589/ado-net/14261/best-practices-executing-sql-statements)。 – Igor

+0

向Event表添加新行時,我有一個下拉菜單,其中包含與[Groups]的[Group_Name]中相同的文本。我希望能夠獲得與DropDown菜單中選擇的[Group_Name]相同行的Group_ID,並將該值分配給[Events]表中的[Event_Group_ID]。 – user2911539

+0

你沒有提到任何UI,到目前爲止,這個問題只顯示了Sql DDL和ADO.NET。這部分代碼對我來說看起來很好,只需要注意一下,你在'@ GroupEventID'參數中硬編碼了'1',這可能是也可能不是你的意圖。如果你需要下拉值的幫助,你應該顯示相關的代碼。 – Igor

回答

0

改變你的設計師屏幕value拍攝https://i.stack.imgur.com/c0Ryz.png爲1,然後2等對應於您在數據庫表中使用的ID。這就是爲什麼你的代碼失敗,因爲你不能將字符串名稱轉換爲整數。該值是唯一的id(int),名稱是顯示文本。

一個更好的和更易於維護的選擇是動態生成時表單/控件加載數據庫值的列表項。有許多已經要求具有良好的答案的問題在這個喜歡Databind ASP.NET List of ListItem to DropDownList issue

+0

設法解決問題。非常感謝您的幫助。 – user2911539

+0

@ user2911539 - 很高興爲您效勞。請考慮接受答案(請參閱[如何接受答案](http://meta.stackexchange.com/a/5235))。 – Igor

-1

你能在下面提供詳情,SQL似乎罰款和數據庫還好看:

  1. 你是從UI獲得DropDownListEventGroup的價值並查詢全球細節表,爲您提供數據。

  2. 你在組表中有數據嗎?

乾杯 薩迪普

+0

嗨,我已經在組表中添加了數據的屏幕截圖 – user2911539

+0

可以顯示從UI到服務器的發佈數據,什麼是 –

+0

當我嘗試提交新事件時,出現以下錯誤:Error:System.FormatException:輸入字符串的格式不正確。 System.Convert.ToInt32(String value)在System.Number.ParseInt32(String s,NumberStyles樣式,NumberFormatInfo信息)System.Number.StringToNumber(String str,NumberStyles選項,NumberBuffer&number,NumberFormatInfo信息,布爾parseDecimal) .btnAddEvent_Click(Object sender,EventArgs e)in c:\ Users \ 11342 \ OneDrive \ Documents \ 4th Year \ FYP \ BallinoraWaterfallCommunity \ Admin.aspx.cs:line 88 – user2911539

0

和文字GroupID說明從數據庫,同時建立下拉列表中,您可以直接與ID下面SQL的組ID而不是組名稱查詢:

string findGroupOfEvent = "SELECT Group_Id FROM Groups WHERE Group_ID ='" + DropDownListEventGroup.SelectedItem.ToString() + "'"