从C#中的SQL Server DB检索值

编程入门 行业动态 更新时间:2024-10-28 21:21:09
本文介绍了从C#中的SQL Server DB检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的SQL Server数据库中,一行包含逗号拆分值。 喜欢, id | column_name ------------------------- 1 | 3,4,5,6,7 我想检索这些值并存储到数组中。 请有人建议一种方法来完成这个/参考一些示例代码来执行此操作。 这对我非常有用!

解决方案

SQL Server没有开箱即用的拆分功能,因此一种简单的方法是构建自己的并在SQL语句中使用它。例如,看看简单但有效使用Transact-SQL拆分字符串的方法 [ ^ ] 如果您不想使用某个功能,可以选择其他功能诸如拆分逗号分隔列表等方法不使用功能 [ ^ ] 当然有一个选项不是在数据库方面做的,而只是获取整个字符串值,然后使用 String.Split [ ^ ]拆分字符串并得到一个数组。

In my SQL Server DB one row contains comma split values. like, id | column_name ------------------------- 1 | 3,4,5,6,7 I want retrieve those values and store in to array. please anyone suggest a way to accomplish this/ refer some sample code to do this. It would be very helpful to me!

解决方案

SQL Server doesn't have a split function out-of-the-box so one easy way is to build your own and use it inside SQL statements. For example have a look at An Easy But Effective Way to Split a String using Transact-SQL[^] If you don't want to use a function there are alternative approaches such as Split Comma Separated List Without Using a Function[^] Of course one option is not to do it on the database side but just fetch the whole string value and then use String.Split[^] on the calling side to split the string and get an array.

更多推荐

从C#中的SQL Server DB检索值

本文发布于:2023-11-07 22:14:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567578.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SQL   Server   DB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!