从C#中的SQL Server DB检索值 [英] retrieve values from SQL Server DB in C#

查看:96
本文介绍了从C#中的SQL Server DB检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的SQL Server数据库中,一行包含逗号拆分值。

喜欢,

id | column_name

-------------------------

1 | 3,4,5,6,7



我想检索这些值并存储到数组中。



请有人建议一种方法来完成这个/参考一些示例代码来执行此操作。

这对我非常有用!

解决方案

< blockquote> SQL Server没有开箱即用的拆分功能,因此一种简单的方法是构建自己的并在SQL语句中使用它。例如,看看简单但有效使用Transact-SQL拆分字符串的方法 [ ^ ]



如果您不想使用某个功能,可以选择其他功能诸如拆分逗号分隔列表等方法不使用功能 [ ^ ]



当然有一个选项不是在数据库方面做的,而只是获取整个字符串值,然后使用 String.Split [< a href =呼叫方https://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx\"target =_ blanktitle =新窗口> ^ ]拆分字符串并得到一个数组。


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检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆