简体   繁体   English

如何使用asp.net获取SQL Server外键中的数据?

[英]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 . 回顾一下,我有两个名为stringInstrumentItembrand 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.

相关问题 如何使用asp.net实体框架在SQL外键中获取数据? - How to get data in an sql foreign key using asp.net entity framework? 带有ASP.Net后端的Azure移动应用程序使用外键将数据发布到SQL Server表 - Azure Mobile App with ASP.Net backend posting data to a SQL Server table with a foreign key 如何在ASP.NET C#中的SQL Server中添加外键 - How to add foreign key in SQL Server in ASP.NET C# 我如何在Asp.Net MVC中使用Entity Framework和Web Api获取外键表数据 - How do i get foreign key table data using Entity Framework and Web Api in Asp.Net MVC 如何在ASP.NET MVC4中添加外键数据 - How to add foreign key data in asp.net mvc4 如何使用外键关系将数据从视图返回到 asp.net 中的 controller? - How do you return data from a view to controller in asp.net using foreign key relationship? 如何使用asp.net通过eval读取外键数据? - how to read data of foreign key though eval using asp.net? 如何使用 c# asp.net 将外键数据插入数据库 - How to insert foreign key data into database using c# asp.net 如何使用列表获取数据和 SQL 服务于 ASP.NET - How to get data using a list and SQL Serve in ASP.NET 从主键asp.net C#获取SQL外键列值 - Get SQL Foreign key columns values from Primary Key asp.net c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM