[英]How to get data in a SQL Server foreign key using asp.net?
This question is somewhat similar and related to my previous question How to get data in an sql foreign key using asp.net entity framework? 这个问题有点类似,并且与我之前的问题有关。 如何使用asp.net实体框架在sql外键中获取数据? so I will be using the same SQL Server tables. 所以我将使用相同的SQL Server表。
This time, instead of accessing the foreign key value through Entity Framework, I want to know how to do it in a regular SQL script like this (take note, the script that I'm talking about is inside the string.Format
): 这次,我不想知道如何通过Entity Framework访问外键值,而是想知道如何在像这样的常规SQL脚本中进行操作(请注意,我正在谈论的脚本位于string.Format
):
public static void getRowNumber(string brand)
{
string query = string.Format("SELECT COUNT(*) FROM stringInstrumentItem WHERE brandId LIKE @brand");
conn1.Open();
command1.CommandText = query;
command1.Parameters.Add(new SqlParameter("brand", brand));
Int32 count = (Int32)command1.ExecuteScalar();
conn1.Close();
command1.Parameters.Clear();
AddASPXAndCSFile.X = count;
}
Just to recap, I have two SQL Server tables called stringInstrumentItem
and brand
. 回顾一下,我有两个名为stringInstrumentItem
和brand
SQL Server表。 I created the foreign key in the table stringInstrumentItem
referring to the primary key of the table brand
. 我在表stringInstrumentItem
创建了外键,该外键引用了表brand
的主键。 So I'm attempting to access the column name
in table brand
through stringInstrumentItem
. 所以我试图通过stringInstrumentItem
访问表brand
的列name
。
As you can see, I'm attempting to access the value in table brand
through brandId
, which does not work. 如您所见,我试图通过brandId
访问表brand
的值,但该方法无效。
Hope you guys can help me on this one. 希望你们能在这一方面帮助我。 Cheers! 干杯!
If I understand your question correctly, it seems like a simple query - 如果我正确理解了您的问题,这似乎是一个简单的查询-
SELECT COUNT(*) FROM stringInstrumentItem
WHERE brandId IN (SELECT brandId FROM brand WHERE name LIKE @brand)
Please make use you have @
and %
(based on your requirement) . 请使用@
和%
(根据您的要求) 。
command1.Parameters.Add(new SqlParameter("@brand", "%" + brand + "%"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.