【问题标题】:String Array Parsing (C# & SQL Server)字符串数组解析(C# & SQL Server)
【发布时间】:2016-07-26 07:21:50
【问题描述】:

我正在尝试将字符串数组传递给DB层,它首先是List<string>类型,然后转换为字符串数组。

但是,由于字符串数组元素有逗号 (,),SQL Server 得到它的方式不同。

这里是列表元素:

className":"Ellipse","data":{"x":10,"y":31,"width":195,"height":96,"strokeWidth":5,"strokeColor":"#000","fillColor":"rgba(224,224,224,0.28)"},"id":"fc9746f9-e07b-9847-1824-5cf4468532f7"}
"className\":\"Ellipse\",\"data\":{\"x\":741,\"y\":153,\"width\":-126,\"height\":40,\"strokeWidth\":5,\"strokeColor\":\"#000\",\"fillColor\":\"rgba(224,224,224,0.28)\"},\"id\":\"73bcdc9f-9f57-f162-8264-f5e894fcf6ab\"}],\"backgroundShapes\":"

这是我用来将列表转换为字符串的代码

string StringLocationDataList = String.Join(" ", LocationDataList.ToArray());

我在 DB 中解析这个字符串

结果如下:

我要做的是:将列表元素解析为字符串数组,然后将它们发送到数据库。

提前致谢

【问题讨论】:

  • 你正在做的很好。但是在您的 Listdata 中有子元素。您已经根据数据读取了孩子,然后插入。取决于您插入数据库的数据要求。
  • 如何根据数据读取孩子?当我传递数据时,它总是由逗号组成。
  • 你能发布完整的StringLocationDataList 转换为 ToArray(); 后得到的输出吗?如果您通常从 StringLocationDataList 的输出中获取特定数据,则可以使用索引。
  • 字符串数组?它看起来像 JSON。

标签: c# arrays sql-server string list


【解决方案1】:

这就是答案;

DataTable asd = new DataTable(); asd.Columns.Add("Id", typeof(int)); asd.Columns.Add("LocationName", typeof(string));

            for (int i = 0; i < LocationDataList.Count; i++)
            {
                //asd.Columns.Add(LocationDataList[i], typeof(string));
                try { 
                asd.Rows.Add(i, LocationDataList[i].ToString());
                } catch(Exception ex)
                {

                }
            }
            command.Parameters.Add("@PLocationDataList", SqlDbType.Structured, -1).Value = asd;

另外,用户定义的表类型需要满足DataTable。

详情; C# DataTable into SQL Server stored procedure

【讨论】:

    猜你喜欢
    • 2021-02-18
    • 2021-03-28
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 2023-03-22
    • 2021-03-02
    相关资源
    最近更新 更多