我在SQL Server 2008中有一個表,其中對於每個值id,我都插入了一個用逗號分隔的值列表。如何檢索SQL中的逗號分隔值到ListBox分開
所以在前端,當我從DropDownList
中選擇一個特定的ID時,我想分別將逗號分隔的值重新分配到ListBox
。
希望我的問題很清楚。好心幫...
我在SQL Server 2008中有一個表,其中對於每個值id,我都插入了一個用逗號分隔的值列表。如何檢索SQL中的逗號分隔值到ListBox分開
所以在前端,當我從DropDownList
中選擇一個特定的ID時,我想分別將逗號分隔的值重新分配到ListBox
。
希望我的問題很清楚。好心幫...
試試這個
//doing this in front end - C#
string values = getValuesFromDB(dropdownList.SelectedValue);
myListBox.DataSource = values.Split(',');
myListBox.DataBind();
OR
foreach(string s in values.Split(','))
{
myListBox.Items.Add(s);
}
還有一些回答上,以便展示瞭如何convert CSV to table和不要求你正在使用
其SQL數據庫很抱歉UPDATE
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();是...?? –
你是如何從數據庫中獲取價值的?我希望你能實現這種提供數據訪問的方法,例如'從myTable中選擇值,其中id = 123' – codingbiz
使用字符串分割功能會給你一個數組的值,你可以循環通過在列表框中指定
僞代碼
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
}
可能有語法錯誤,但我覺得你有這個想法。
下面是示例一旦數據進入表中,您可以輕鬆地將數據綁定到列表框中。
STEP 1
執行在您的SQL服務器窗口下面的過程。
的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 @[email protected]+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
STEP 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')
將這種方式存儲在數據庫中並不是一個好主意,因爲:
1:如果你想填補一個下拉列表可能會發現一點,當你需要不同的ID,如:
1 - First Otion
3 - Second Option
11 - Third Option
那麼就很難從數值只是進行解碼。
2:它總是讓它解析它們的時間更長。存儲並不是那麼「昂貴」,然後是處理器時間,所以它絕對不是一個好策略。
但是,如果您想要改變它或在SQL級別上解決它,可以編寫一個SQL函數來分析分隔字符串。
SQL函數是什麼做這樣的事情,可以幫助:
其中的SQL ServerDECLARE @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
? (2005或2008或2012?) –
SQL Server 2008 –