Re: SqlDataAdapter - can it be (re)used for 2 SELECT statements
- From: "Matt Jensen" <replytonewsgroups@xxxxxxxxxxxxx>
- Date: Fri, 1 Jul 2005 09:49:47 +0100
Might be best if I give an example. I'm wondering if I can use the one
dataadapter for 2 different calls to the same sproc, and/or how I can use
less code:
// Open a connection to database
SqlConnection objConn = new
SqlConnection(ConfigurationSettings.AppSettings["connJBS"]);
//create dataset to hold all table rows
DataSet objDSJobTypes = new DataSet("JobTypes");
//Web Jobs
//Create the stored procedure command object & add parameter objects for
the stored procedure parameter
SqlCommand objCmdWebJobs = new SqlCommand("spTFL_RequestJob_JobTypes",
objConn);
objCmdWebJobs.CommandType = CommandType.StoredProcedure;
objCmdWebJobs.Parameters.Add("@JobCatID", SqlDbType.Int);
objCmdWebJobs.Parameters["@JobCatID"].Value = 1;
//create our DataAdapter object and use it to fill the dataset object
SqlDataAdapter objDAWebJobs = new SqlDataAdapter(objCmdWebJobs);
objDAWebJobs.Fill(objDSJobTypes,"WebJobs");
//bind to repeater
Repeater1.DataSource=objDSJobTypes.Tables["WebJobs"].DefaultView;
Repeater1.DataBind();
//Web Projects
//Create the stored procedure command object & add parameter objects for
the stored procedure parameter
SqlCommand objCmdWebProjects = new SqlCommand("spTFL_RequestJob_JobTypes",
objConn);
objCmdWebProjects.CommandType = CommandType.StoredProcedure;
objCmdWebProjects.Parameters.Add("@JobCatID", SqlDbType.Int);
objCmdWebProjects.Parameters["@JobCatID"].Value = 2;
//create our DataAdapter object and use it to fill the dataset object
SqlDataAdapter objDAWebProjects = new SqlDataAdapter(objCmdWebProjects);
objDAWebProjects.Fill(objDSJobTypes,"WebProjects");
//bind to repeater
Repeater2.DataSource=objDSJobTypes.Tables["WebProjects"].DefaultView;
Repeater2.DataTextField = "JobTypeDesc";
Repeater2.DataValueField = "JobTypeID";
Repeater2.DataBind();
Any advice greatly appreciated.
Thanks
Matt
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:es3A%23rbfFHA.3904@xxxxxxxxxxxxxxxxxxxxxxx
> Not sure what you are asking about about the parameters.
>
> If you are using an sproc (as you indicated) you'd pass in all the
> parameters needed for both query to a single sproc and the queries can use
> them as needed.
>
> If you want to refer to the tables by name, simply name them after you've
> done the fill based on the index (so use the index the first time, then
> the name).
>
> Karl
>
>
>
>
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
> come!)
>
>
> "Matt Jensen" <replytonewsgroups@xxxxxxxxxxxxx> wrote in message
> news:u7idOnZfFHA.3256@xxxxxxxxxxxxxxxxxxxxxxx
>>I guess I can just build up SqlCommand objects (including adding
>>parameters etc.) then just comma list them inside the SqlDataAdapter
>>statement, can I?
>> How about naming the tables?
>> Thanks
>> Cheers
>> Matt
>>
>> "Matt Jensen" <replytonewsgroups@xxxxxxxxxxxxx> wrote in message
>> news:%23qEa3kZfFHA.1204@xxxxxxxxxxxxxxxxxxxxxxx
>>> Nice, thanks :-)
>>> Same for stored procedures with parameters though?
>>> How about naming the tables in the dataset, or does one just refer to
>>> them by table index number?
>>> Cheers
>>> Matt
>>>
>>> "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
>>> wrote in message news:Oio5xZZfFHA.572@xxxxxxxxxxxxxxxxxxxxxxx
>>>> Just do it in 1 query.
>>>>
>>>>
>>>> select * from x; select * from y
>>>>
>>>> it'll populate 2 tables in the 1 dataset that you Fill()
>>>>
>>>> Karl
>>>>
>>>> --
>>>> MY ASP.Net tutorials
>>>> http://www.openmymind.net/ - New and Improved (yes, the popup is
>>>> annoying)
>>>> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
>>>> come!)
>>>>
>>>>
>>>> "Matt Jensen" <replytonewsgroups@xxxxxxxxxxxxx> wrote in message
>>>> news:O3j4KVZfFHA.3436@xxxxxxxxxxxxxxxxxxxxxxx
>>>>> Howdy
>>>>> Fairly simple question I think, I presume the answer is no it can't be
>>>>> reused for 2 *SELECT* statements, but just hoping for clarification.
>>>>> Just asking in the interests of trying to minimise code.
>>>>>
>>>>> i.e. if the SqlDataAdapter uses a connection to the one database for a
>>>>> select statement and I want to do a second select statement on exactly
>>>>> the same database, can I reuse the SqlDataAdapter?
>>>>>
>>>>> I ask because I want to put both 'select' results into the one
>>>>> dataset, just thought I'd use the SqlDataAdapter if I could.
>>>>>
>>>>> Thanks
>>>>> Cheers
>>>>> Matt
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
.
- Prev by Date: Regarding connectivity at runtime in Crystal Reports
- Next by Date: Re: code driving me crazy, any assistance?
- Previous by thread: Regarding connectivity at runtime in Crystal Reports
- Next by thread: Re: SqlDataAdapter - can it be (re)used for 2 SELECT statements
- Index(es):
Relevant Pages
|