Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Monday, February 20, 2012

Passing SqlDataSource object an array as a parameter

Hi,

I am trying to get the selected options from a listbox and either pass a SqlDataSource object the array or loop through it and pass each element of the array. I then need to modify the returned databtable to graphing function, but first drop the last column. I was wondering if anyone can help me with the following:

1. Pass an array into SqlDataSource Select OR
2. Pass a single argument into the Select statement and populate a datatable without it writing over the current row each time it iterates through the foreach statement. I am looking for the dataview to append to dt each time it loops. Is there a property for dataview that behaves like the "ClearBeforeFill" for table adapters?
3. Update a parameter programmatically

Below code works, but I think it can be more efficient. Any suggestions would be greatly appreciated.

Thanks in advance!!

DataTable dt = new DataTable();
DataTable dt2 = new DataTable();
DataView dv = new DataView();

foreach(ListItem liOptions in ListBox1.Items)
{
if(liOptions.Selected)
{
SqlDataSource1.SelectParameters.Add("Parameter1", liOptions);
dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
dt2 = dv.Table;
dt.Merge(dt2);
dt2.Dispose();
SqlDataSource1.SelectParameters.Clear();
}
}


if (dt.Rows.Count > 0)
{
Graph(dt); //Pass original datatable (dt) to Graph();
dt.Columns.RemoveAt(2); //Reformat datatable (dt) and remove last column before binding to Gridview1

GridView1.DataSource = dt;
GridView1.DataBind();
} else {

errorMessage.Text = "No data was returned!";
}

Dear sillyrabbit,

XML. It's a new feature in SQL Server 2005. If you don't have SS2005, then I don't think this will work for you.

The best way to pass an array to is to use XML.

XmlDocument xmlDocument =new XmlDocument();XmlElement rootNode = xmlDocument.CreateElement("RootName");xmlDocument.AppendChild(rootNode);XmlElement xNode;XmlText xText;for (int intList = 0; intArray.Length > intList; ++intList){if (intArray[intList] != 0){xNode = xmlDoc.CreateElement("IdName");xText = xmlDoc.CreateTextNode(intArray[intList].ToString());rootNode.AppendChild(xNode);xNode.AppendChild(xText);}}return xmlDocument.InnerXml;

http://grahamsibley.typepad.com/thoughtfactory/2006/09/stored_procedur.html

hope this helps,

fathrDave

|||

Also,

Your SqlDbType needs to be XML.

In the Stored Procedure I used "CROSS APPLY @.xmlName.nodes('//IdName') as R(nref)" to join to my DB tables (after the FROM). I did a "dbField = nref.value('.', 'int')" to connect it to DB table (after the WHERE).

I did have some trouble with ArithAbort and had to Set it to ON before I ran the proc, and then OFF afterwards. Also, the time (timeout) is a factor and I am trying to solve that currently.

hope this helps too.

fathrDave

|||Thanks very much for the suggestion. I'll give it a try.

Passing some sort of Data Structure to a Stored Procedure

Hi all,
Is it possible to pass some sort of array to a stored procedure using
ADO.net.
In particular, I have a list of usernames that I need to pass to the
procedure and then have the procedure loop through that array and perform an
update action on the database.
The only alternative I can think of is to call a stored procedure over and
over again. I'd rather pass th usernames in bulk. Can anyone suggest how to
do this?
Thanks all
Kindest Regards
SimonSimon
This is one approach
CREATE PROCEDURE sparray_method
@.array nvarchar(4000)
AS
BEGIN
SET NOCOUNT ON
DECLARE @.nsql nvarchar(4000)
SET @.nsql = '
SELECT *
FROM sysobjects
WHERE name IN ( ' + @.array + ')'
PRINT @.nsql
EXEC sp_executesql @.nsql
END
GO
EXEC sparray_method
@.array = '''sysobjects'',''sysindexes'',''syscolu
mns'''
"Simon Harvey" <simon.harvey@.the-web-works.co.uk> wrote in message
news:#YWnhn4$DHA.692@.TK2MSFTNGP11.phx.gbl...
> Hi all,
> Is it possible to pass some sort of array to a stored procedure using
> ADO.net.
> In particular, I have a list of usernames that I need to pass to the
> procedure and then have the procedure loop through that array and perform
an
> update action on the database.
> The only alternative I can think of is to call a stored procedure over and
> over again. I'd rather pass th usernames in bulk. Can anyone suggest how
to
> do this?
> Thanks all
> Kindest Regards
> Simon
>|||If the list is short, perhaps Uri's method would be faster...
You could also parse the list using SQL string commands in a loop and do the
updates
You could also store the names in a #temp table and have the SP join to the
#temp table to choose which rows would be updated...
Wayne Snyder, MCDBA, SQL Server MVP
Computer Education Services Corporation (CESC), Charlotte, NC
www.computeredservices.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"Simon Harvey" <simon.harvey@.the-web-works.co.uk> wrote in message
news:%23YWnhn4$DHA.692@.TK2MSFTNGP11.phx.gbl...
> Hi all,
> Is it possible to pass some sort of array to a stored procedure using
> ADO.net.
> In particular, I have a list of usernames that I need to pass to the
> procedure and then have the procedure loop through that array and perform
an
> update action on the database.
> The only alternative I can think of is to call a stored procedure over and
> over again. I'd rather pass th usernames in bulk. Can anyone suggest how
to
> do this?
> Thanks all
> Kindest Regards
> Simon
>|||Thank you all
Simon|||YOU MIGHT JUST TRY USING THE "adArray" type declaration for the type of data
being passed. I would guess that you would create a dimensioned array
Dim A As Variant
A = Array(30)
A(1) = "JOHN"
A(1) = "JOHN2"
With cmd_Users_Update
.ActiveConnection = Users_DB_Connection
.CommandType = adCmdStoredProc
.CommandText = "dp_process_users_array"
.Parameters.Append .CreateParameter("@.users_array", _
adArray, adParamInput)
.parameters("@.users_array").Value = A
End With
and pass that array to the parameter of the command object.
Dan Kirk

Passing some sort of Data Structure to a Stored Procedure

Hi all,
Is it possible to pass some sort of array to a stored procedure using
ADO.net.
In particular, I have a list of usernames that I need to pass to the
procedure and then have the procedure loop through that array and perform an
update action on the database.
The only alternative I can think of is to call a stored procedure over and
over again. I'd rather pass th usernames in bulk. Can anyone suggest how to
do this?
Thanks all
Kindest Regards
SimonSimon
This is one approach
CREATE PROCEDURE sparray_method
@.array nvarchar(4000)
AS
BEGIN
SET NOCOUNT ON
DECLARE @.nsql nvarchar(4000)
SET @.nsql = '
SELECT *
FROM sysobjects
WHERE name IN ( ' + @.array + ')'
PRINT @.nsql
EXEC sp_executesql @.nsql
END
GO
EXEC sparray_method
@.array = '''sysobjects'',''sysindexes'',''syscolumns'''
"Simon Harvey" <simon.harvey@.the-web-works.co.uk> wrote in message
news:#YWnhn4$DHA.692@.TK2MSFTNGP11.phx.gbl...
> Hi all,
> Is it possible to pass some sort of array to a stored procedure using
> ADO.net.
> In particular, I have a list of usernames that I need to pass to the
> procedure and then have the procedure loop through that array and perform
an
> update action on the database.
> The only alternative I can think of is to call a stored procedure over and
> over again. I'd rather pass th usernames in bulk. Can anyone suggest how
to
> do this?
> Thanks all
> Kindest Regards
> Simon
>|||Depending on what processing you are actually doing, you
could create an ADO.NET dataset that has a datatable
holding the usernames, write the table to the SQL Server,
and then have your procedure access this table. One
advantage to this is that you may be able to perform a
joined update statement that would speed things up
drastically over looping through a list of users.
Just a thought which I hope will help.
Matthew Bando
matthew.bando@.csctgi(remove).com
>--Original Message--
>Hi all,
>Is it possible to pass some sort of array to a stored
procedure using
>ADO.net.
>In particular, I have a list of usernames that I need to
pass to the
>procedure and then have the procedure loop through that
array and perform an
>update action on the database.
>The only alternative I can think of is to call a stored
procedure over and
>over again. I'd rather pass th usernames in bulk. Can
anyone suggest how to
>do this?
>Thanks all
>Kindest Regards
>Simon
>
>.
>|||If the list is short, perhaps Uri's method would be faster...
You could also parse the list using SQL string commands in a loop and do the
updates
You could also store the names in a #temp table and have the SP join to the
#temp table to choose which rows would be updated...
--
Wayne Snyder, MCDBA, SQL Server MVP
Computer Education Services Corporation (CESC), Charlotte, NC
www.computeredservices.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"Simon Harvey" <simon.harvey@.the-web-works.co.uk> wrote in message
news:%23YWnhn4$DHA.692@.TK2MSFTNGP11.phx.gbl...
> Hi all,
> Is it possible to pass some sort of array to a stored procedure using
> ADO.net.
> In particular, I have a list of usernames that I need to pass to the
> procedure and then have the procedure loop through that array and perform
an
> update action on the database.
> The only alternative I can think of is to call a stored procedure over and
> over again. I'd rather pass th usernames in bulk. Can anyone suggest how
to
> do this?
> Thanks all
> Kindest Regards
> Simon
>|||Thank you all
Simon|||YOU MIGHT JUST TRY USING THE "adArray" type declaration for the type of data being passed. I would guess that you would create a dimensioned array
Dim A As Varian
A = Array(30
A(1) = "JOHN
A(1) = "JOHN2
With cmd_Users_Updat
.ActiveConnection = Users_DB_Connectio
.CommandType = adCmdStoredPro
.CommandText = "dp_process_users_array
.Parameters.Append .CreateParameter("@.users_array",
adArray, adParamInput
.parameters("@.users_array").Value = End Wit
and pass that array to the parameter of the command object
Dan Kirk