[英]Connecting Two SQL Server Tables
I'm trying to connect the following 2 tables together: 我正在尝试将以下2个表连接在一起:
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)
);
First question: have I created the tables correctly? 第一个问题:我是否正确创建了表?
I've been able to add to the Events
with the following code but I haven't been able to successfully connect the PK of Groups
as an FK in Events
. 我已经能够使用以下代码添加到
Events
,但是我无法成功地将Events
Groups
的PK作为FK连接起来。
Does anyone have any suggestions? 有没有人有什么建议?
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();
Change the value
in your designer screen shot https://i.stack.imgur.com/c0Ryz.png to 1 and then 2 etc that corresponds to the ids you used in your database table. 将设计器屏幕快照https://i.stack.imgur.com/c0Ryz.png中的
value
更改为1,然后将2更改为与您在数据库表中使用的ID对应的值。 That is why your code is failing as you cannot convert a string name to an integer. 这就是为什么您的代码失败的原因,因为您无法将字符串名称转换为整数。 The value is the unique id (int), the name is the display text.
值是唯一ID(int),名称是显示文本。
A much better and more maintainable option would be to dynamically generate the list items from the database values when the form/control loads. 更好/更可维护的选择是在加载表单/控件时从数据库值动态生成列表项。 There are many already asked questions with good answers on this like Databind ASP.NET List of ListItem to DropDownList issue
已经有很多很好的答案的问题,例如DataItem的ASP.NET ListItem到ListDownList的问题
并在构建下拉列表时从数据库向GroupID
描述文本,您可以直接在下面的SQL中使用ID查询组ID而不是组名:
string findGroupOfEvent = "SELECT Group_Id FROM Groups WHERE Group_ID ='" + DropDownListEventGroup.SelectedItem.ToString() + "'"
Can you provide below details, SQL seems to fine and database also looking good: 您能否提供以下详细信息,SQL看起来还不错,数据库看起来也不错:
Are you getting value of DropDownListEventGroup from UI and querying global detail table, giving you data. 您是否从UI获取DropDownListEventGroup的价值并查询全局明细表,从而为您提供数据。
Do you have data in groups table? 分组表中是否有数据?
Cheers Sudeep 干杯苏迪普
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.