使用 C# 从 SQL 数据库表中选择

发布于 2024-09-13 14:32:51 字数 116 浏览 6 评论 0原文

我有一个用户 ID 列表和一个与 SQL Server 的开放连接。如何循环遍历此列表并选择与 First_Name 和 Last_Name 列匹配的 UserID?我假设输出可以在数据表中?

非常感谢

I have a List of UserID's and a open connection to SQL Server. How can I loop through this List and Select matching UserID with First_Name and Last_Name columns? I assume the output can be in a datatable?

many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栖竹 2024-09-20 14:32:51

根据您运行的 SQL 类型,它会略有不同,但是这个 应该可以帮助您入门。

It varies slightly depending on which type of SQL you're running, but this and this should get you started.

时光清浅 2024-09-20 14:32:51

最方便的方法是:

  1. 将列表转换为包含逗号分隔的用户 ID 值列表的字符串
  2. 将该 CSV 字符串提供到 IN 子句中,例如:

    SELECT u.first_name,
           u.姓氏
      来自 USER_TABLE u
     WHERE u.userid IN ([逗号分隔的用户 ID 列表])
    

否则,您可以将值插入到临时表中并连接到用户表:

SELECT u.first_name,
       u.last_name
  FROM USER_TABLE u
  JOIN #userlist ul ON ul.userid = u.userid

The most expedient way of doing this would be to:

  1. Turn the List into a string containing a comma separated list of the userid values
  2. Supply that CSV string into an IN clause, like:

    SELECT u.first_name,
           u.last_name
      FROM USER_TABLE u
     WHERE u.userid IN ([comma separated list of userids])
    

Otherwise, you could insert the values into a temp table and join to the users table:

SELECT u.first_name,
       u.last_name
  FROM USER_TABLE u
  JOIN #userlist ul ON ul.userid = u.userid
北方。的韩爷 2024-09-20 14:32:51

在 SQL 数据库中编写一个名为 ParseIntegerArray 的函数。这应该将逗号分隔的字符串转换为 ID 表,然后您可以在查询中加入该表。这还有助于避免连接字符串构建 SQL 时可能带来的任何 SQL 注入风险。使用 LINQ to SQL 或 LINQ to Entities 时也可以使用此函数。

DECLARE @itemIds nvarchar(max)
SET itemIds = '1,2,3'

SELECT
    i.*
FROM
               dbo.Item AS i
    INNER JOIN dbo.ParseIntegerArray(@itemIds) AS id ON i.ItemId = id.Id

Write a function in your SQL database named ParseIntegerArray. This should convert a comma delimited string into a table of IDs, you can then join to this in your query. This also helps to avoid any SQL injection risk you could get from concatenating strings to build SQL. You can also use this function when working with LINQ to SQL or LINQ to Entities.

DECLARE @itemIds nvarchar(max)
SET itemIds = '1,2,3'

SELECT
    i.*
FROM
               dbo.Item AS i
    INNER JOIN dbo.ParseIntegerArray(@itemIds) AS id ON i.ItemId = id.Id
铁憨憨 2024-09-20 14:32:51

本文应该对您有帮助:http://msdn. microsoft.com/en-us/library/aa496058%28SQL.80%29.aspx

我过去曾使用它来创建一个接受单个逗号分隔的 varchar 参数的存储过程。
我的 C# 程序源是一个选中的列表框,我使用 foreach 循环和 StringBuilder 构建了逗号分隔的字符串来进行串联。可能有更好的方法,具体取决于您列表中的项目数量。

回到 SQL 部分,本文中讨论的 fn_Split 函数使您能够将逗号分隔的字符串转换回 SQL Server 可以理解的表变量...并且您可以在存储过程中查询该变量。

下面是一个示例:

CREATE PROCEDURE GetSelectedItems
(
@SelectedItemsID  Varchar(MAX) -- comma-separated string containing the items to select
)
AS
SELECT * FROM Items
WHERE ItemID IN (SELECT Value FROM dbo.fn_Split(@SelectedItemsIDs,','))

RETURN
GO

请注意,如果您愿意,还可以使用内部联接来代替 IN()。

如果您的 SQL Server 上没有 fn_Split UDF,您可以在此处找到它: http:// odetocode.com/Articles/365.aspx

我希望这会有所帮助。

This article should help you: http://msdn.microsoft.com/en-us/library/aa496058%28SQL.80%29.aspx

I've used this in the past to create a stored procedure accepting a single comma delimited varchar parameter.
My source from the C# program was a checked list box, and I built the comma delimited string using a foreach loop and a StringBuilder to do the concatenation. There might be better methods, depending on the number of items you have in your list though.

To come back to the SQL part, the fn_Split function discussed in the article, enables you to transform the comma delimited string back to a table variable that SQL Server can understand... and which you can query in your stored procedure.

Here is an example:

CREATE PROCEDURE GetSelectedItems
(
@SelectedItemsID  Varchar(MAX) -- comma-separated string containing the items to select
)
AS
SELECT * FROM Items
WHERE ItemID IN (SELECT Value FROM dbo.fn_Split(@SelectedItemsIDs,','))

RETURN
GO

Note that you could also use an inner join, instead of the IN() if you prefer.

If you don't have the fn_Split UDF on your SQL Server, you can find it here: http://odetocode.com/Articles/365.aspx

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文