且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在SQL Server的单个列中插入多个值(如数组)

更新时间:2022-10-20 17:46:35

你不能在一个单元格中存储一个数组,这是一个坏主意 - 你可以通过将数组的每个元素转换为字符串并将它们连接起来形成CSV数据来实现它,但那是一个噩梦,以后再使用。



如果你试图将一个数组存储到多行中,那就太糟糕了:将你的数组转换为DataTable并使用 SqlBulkCopy.WriteToServer方法(DataRow [])(System.Data.SqlClient) [ ^ ]

i want to store multiple value in sql server, i have done this in Oracel but it in SQL server i am able to do that please suggest some aleternate

What I have tried:

i have tired using Declare but it doesnt store value longer

You can't store an array in a single cell and it would be a bad idea to try - you can do it by converting each element of the array to a string and concatenate them to form CSV data, but that's a nightmare to work with later.

If you are trying to store an array into multiple rows, that's not too bad at all: convert your array to a DataTable and use the SqlBulkCopy.WriteToServer Method (DataRow[]) (System.Data.SqlClient)[^]