【问题标题】:How to retrieve commas delimited values in SQL to a ListBox separately如何将 SQL 中的逗号分隔值分别检索到 ListBox
【发布时间】:2012-09-01 00:38:22
【问题描述】:

我在 SQL Server 2008 中有一个表,对于每个值 id,我插入了一个以逗号分隔的值列表。

所以在前端,当我从 DropDownList 中选择一个特定的 id 时,我想将逗号分隔的值分别检索到 ListBox

希望我的问题很清楚。请帮忙...

【问题讨论】:

  • 在哪个 SQL Server 中? (2005 年或 2008 年或 2012 年?)

标签: c# asp.net sql sql-server-2008


【解决方案1】:

试试这个

 //doing this in front end - C#
 string values = getValuesFromDB(dropdownList.SelectedValue);
 myListBox.DataSource = values.Split(',');
 myListBox.DataBind();

 foreach(string s in values.Split(','))
 {
   myListBox.Items.Add(s);
 }

还有一些关于 SO 的答案显示了如何convert CSV to table,很抱歉没有询问您使用的是哪个 SQL 数据库

更新

private string getValuesFromDB(string selectedID)
{
    //this might not be the rea; query
    string query = "SELECT commaValues FROM myTable WHERE id = " + selectedID; 
    ....
    ....
    string commaSeparatedValues = "retrieved values from this recod";
    return commaSeparatedValues;
}

【讨论】:

  • getValuesFromDB();是……??
  • 您是如何从数据库中获取值的?我希望您实现提供数据访问的方法,例如select values from myTable where id=123
【解决方案2】:

使用字符串拆分函数将为您提供数组中的值,您可以循环分配到列表框中

伪代码

string myids = //data retrived from DB
string [] id = myids.split(',') //I assume , as delimiter
//loop thorugh id array and assign it to listbox item.
for(int i=0; i<id.Length; i++)
{
listbox1.items.add(id[i]); //Assuming listbox1 is your Listbox control
}

可能存在语法错误,但我想你明白了。

【讨论】:

    【解决方案3】:

    以下是示例一旦数据进入表格,您就可以轻松地将数据绑定到列表框。

    第 1 步

    在您的 Sql Server 窗口中执行以下过程。

    fn_CommaSeparatedStringToTable 函数接受逗号分隔的字符串和一个值('Y' 或 'N')以在结果集中包含 NULL 和 EMPTY 字符串

    ALTER FUNCTION [dbo].[fn_CommaSeparatedStringToTable]
    (
     @CommaSeparatedValues     VARCHAR(MAX),
     @IncludeEmptyStrings    CHAR(1)
    )
    RETURNS @Item TABLE 
    (
       RowId int IDENTITY(1, 1) NOT NULL, 
       Value VARCHAR(200)
    )
     AS
      BEGIN
    
          DECLARE @IndefOfComma int,
         @Value VARCHAR(200),@StartPos bigint,@EndPos 
    
          bigint,@LengthOfString int, @ReachedEnd Char(1)
    
      SET @StartPos=1
      SET @EndPos=0
      SET @LengthOfString=LEN(@CommaSeparatedValues)
      SET @ReachedEnd='N'
    
      WHILE @ReachedEnd<>'Y'
            BEGIN
                  SET @EndPos=CHARINDEX(',',@CommaSeparatedValues,@StartPos)
                  IF @EndPos>0
                  BEGIN
                      SET @Value = SUBSTRING(@CommaSeparatedValues, @StartPos,@EndPos-  
     @StartPos) 
                      SET @StartPos=@EndPos+1      
                  END
                  ELSE
                  BEGIN
                     SEt @ReachedEnd='Y'
                     SET @Value = SUBSTRING(@CommaSeparatedValues,   
     @StartPos,@LengthOfString-(@StartPos-1))
                  END
                  IF(@Value<>'' OR @IncludeEmptyStrings='Y')
                  INSERT INTO @Item(Value) VALUES(@Value)
      END
      RETURN
     END
    

    第 2 步

    要访问该功能,请使用以下查询

      Include Empty or Null String in the result
    
      SELECT * FROM [dbo].[fn_CommaSeparatedStringToTable]
      ('ALICE,BRAD,JOHN,JOE,MIC,SCAIF,JEMY, ,','Y')
    
    
     Do not include Empty or Null String in the result 
    SELECT * FROM [dbo].[fn_CommaSeparatedStringToTable]
    ('ALICE,BRAD,JOHN,JOE,MIC,SCAIF,JEMY, ,','N')
    

    【讨论】:

      【解决方案4】:

      以这种方式将值存储在数据库中不是一个好主意,因为:

      1:如果你想填写一个下拉列表,你可能会找到需要不同 id 的点,例如:

      1 - First Otion
      3 - Second Option
      11 - Third Option
      

      那么很难仅从值中解码。

      2:解析它们的时间总是更长。存储不是那么“昂贵”,然后是处理器时间,所以这绝对不是一个好策略。

      但是,如果您想在 SQL 级别上对其进行更改或解决,您可以编写一个 SQL 函数来解析分隔字符串。

      一个 SQL 函数正在做这样的事情,可以提供帮助:

      DECLARE @string varchar(500)
      
      --Here comes the selected one
      SELECT @string = Value FROM List WHERE ID = 2
      
      DECLARE @pos int
      DECLARE  @id int
      DECLARE @piece varchar(500)
      
      DECLARE @returnTable table(
      ID int,
      Value varchar(64)
      )
      
      SET @id = 0
      
      IF right(rtrim(@string),1) <> ','
       SET @string = @string  + ','
      
      SET @pos =  patindex('%,%' , @string)
      WHILE @pos <> 0 
      BEGIN
       SET @piece = left(@string, @pos - 1)
      
       INSERT INTO @returnTable VALUES(@id, @piece)
      
       SET @id = @id + 1
      
       SET @string = stuff(@string, 1, @pos, '')
       SET @pos =  patindex('%,%' , @string)
      END
      
      SELECT * FROM @returnTable
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-22
        • 2015-03-09
        • 1970-01-01
        相关资源
        最近更新 更多