我有一个数据绑定列表框,可以为每个选择自动代码。 我希望能够将所有选定的值存储在参数(@ Product_Type2)中并在SQL更新查询中使用。我可以将列表框值存储为文本框(TextBox1)中的字符串,但无法使查询使用所有值。这是在SharePoint asp中,不能使用代码。我已经包含了代码片段。
<asp:ListBox runat="server" id="ListBox2" DataTextField="ProductDesc" DataSourceID="SqlDataSourceDropdownProd_2" DataValueField="Product_Type2" AutoPostBack="True" SelectionMode="Multiple">
</asp:ListBox>
<br />
<br />
<script type="text/javascript">
$(function () {
var values = "('";
var selected = $("[id*=ListBox2] option:selected");
selected.each(function () {
values += $(this).val() + "','";
});
document.getElementById('TextBox1').value =(values)+'zzz' + "')";
return false;
});
</script>
<asp:TextBox runat="server" id="TextBox1" Width="255px" Height="334px" AutoPostBack="True">
</asp:TextBox>
<asp:SqlDataSource id="SqlDataSourceDropdownProd_2" runat="server" ...
SelectCommand="SELECT DISTINCT sdt.[Product Type2] AS Product_Type2, pd2.ProductDesc, pd2.line
FROM dbo.sportsCD_Data_TEST AS sdt INNER JOIN
dbo.Dim_Product_Type2 AS pd2 ON sdt.[Product Type2] = pd2.Product
ORDER BY pd2.line" OldValuesParameterFormatString="original_{0}">
</asp:SqlDataSource>
<asp:SqlDataSource id="SqlDataSourceDropdownProd_3" runat="server" ...
SelectCommand="SELECT [Sales Pro Number] AS Sales_Pro_Number, [New Price] AS New_Price, [Product Type2] AS Product_Type2, [Percent], Dollar
FROM dbo.sportsCD_Data_TEST
WHERE ([Product Type2] in (@Product_Type2))"
UPDATE [sportsCD_Data_TEST] SET
[New Price] = CONVERT(decimal(10, 2), CAST([Sell Price] AS float) * (CAST(@Percent AS float) / 100)
+ CAST([Sell Price] AS float)+ CAST(@Dollar as float)) , [Percent] = 0, [Dollar] = 0
WHERE ([Product Type2] in (@Product_Type2))" <SelectParameters>
<asp:controlparameter ControlID="TextBox1" PropertyName="Text" Name="Product_Type2" />
</SelectParameters>
<UpdateParameters>
<asp:parameter Name="Percent" Type="Decimal" />
<asp:parameter Name="Dollar" Type="Decimal" />
<asp:parameter Name="Product_Type2" />
</UpdateParameters>
</asp:SqlDataSource>
</form>
答案 0 :(得分:0)
这是你可以做到的一种方式。
如果您的数据库已经有字符串拆分功能,请使用它。如果它没有,并且您可以添加一个,请选择一个适合您的。以下是关于如何创建一个的一些想法:http://sqlperformance.com/2012/07/t-sql-queries/split-strings(如果您可以相信您的输入只是带逗号和空格的字母数字,那么XML应该可以正常工作)。 e.g。
-- USE [DatabaseName]
CREATE FUNCTION dbo.SplitStrings_XML
(
@List NVARCHAR(MAX),
@Delimiter NVARCHAR(255)
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
(
SELECT Item = y.i.value('(./text())[1]', 'nvarchar(4000)')
FROM
(
SELECT x = CONVERT(XML, '<i>'
+ REPLACE(@List, @Delimiter, '</i><i>')
+ '</i>').query('.')
) AS a CROSS APPLY x.nodes('i') AS y(i)
);
GO
接下来,将您的jquery函数更改为如下所示:
$(function () {
var values;
var selected = $("[id*=ListBox2] option:selected");
selected.each(function () {
values += "," + $(this).val();
});
document.getElementById('TextBox1').value = values;
return false;
});
您只想提供类似&#34;,A,B,C&#34;的更新语句,不需要引号。然后使您的更新语句如下所示:
UPDATE sCDT
SET [New Price] = CAST(1.0 * [Sell Price] * @Percent / 100 + [Sell Price] + @Dollar AS DECIMAL(10,2))
, [Percent] = 0
, [Dollar] = 0
FROM dbo.SplitStrings_XML(STUFF(@Product_Type2, 1, 1, ''), ',') T -- insert appropriate string-splitting table-function, the STUFF is to get rid of leading comma
JOIN [sportsCD_Data_TEST] sCDT ON sCDT.[Product Type2] = T.[Item];