C# 如何将SQL中以逗号分隔的值分别检索到列表框中
我在SQLServer2008中有一个表,其中为每个值id插入了一个以逗号分隔的值列表 因此,在前端,当我从C# 如何将SQL中以逗号分隔的值分别检索到列表框中,c#,asp.net,sql,sql-server-2008,C#,Asp.net,Sql,Sql Server 2008,我在SQLServer2008中有一个表,其中为每个值id插入了一个以逗号分隔的值列表 因此,在前端,当我从下拉列表中选择一个特定的id时,我想将逗号分隔的值分别重设为列表框 希望我的问题清楚。请帮忙……试试这个 //doing this in front end - C# string values = getValuesFromDB(dropdownList.SelectedValue); myListBox.DataSource = values.Split(','); myLis
下拉列表
中选择一个特定的id时,我想将逗号分隔的值分别重设为列表框
希望我的问题清楚。请帮忙……试试这个
//doing this in front end - C#
string values = getValuesFromDB(dropdownList.SelectedValue);
myListBox.DataSource = values.Split(',');
myListBox.DataBind();
或
上面还有一些答案,说明了如何使用,我很抱歉没有询问您使用的是哪个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;
}
使用字符串拆分函数将在数组中为您提供值,您可以在列表框中循环分配这些值 伪码
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
}
string myid=//从数据库检索的数据
string[]id=myids.split(',')//我假设,作为分隔符
//循环thorugh id数组并将其分配给listbox项。
对于(int i=0;i,下面是一个示例,一旦数据进入表中,您就可以轻松地将数据绑定到列表框中
步骤1
在Sql Server窗口中执行以下过程
fn_CommaSeparatedStringToTable函数接受逗号分隔的字符串和一个值('Y'或'N'),以在结果集中包含NULL和空字符串
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
以这种方式在数据库中存储值不是一个好主意,因为:
1:如果您想填写dropdownlist,您可能会在需要不同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
DECLARE@string varchar(500)
--这是被选中的一个
从ID=2的列表中选择@string=Value
声明@pos int
声明@id int
声明@piece varchar(500)
DECLARE@returnTable表(
ID int,
varchar值(64)
)
设置@id=0
如果正确(rtrim(@string),1)'
设置@string=@string+','
设置@pos=patindex(“%,%”,@string)
而@pos 0
开始
设置@piece=left(@string,@pos-1)
插入@returnTable值(@id,@piece)
设置@id=@id+1
设置@string=stuff(@string,1,@pos,,)
设置@pos=patindex(“%,%”,@string)
结束
从@returnTable中选择*
在哪个SQL Server中?(2005、2008或2012?)getValuesFromDB()是什么…?您是如何从数据库中获取值的?我希望您实现提供数据访问的方法,例如从myTable中选择值,其中id=123
1 - First Otion
3 - Second Option
11 - Third Option
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