Dave Burke - Freelance .NET Developer specializing in Online Communities

A freelance .NET Developer

Data Access Application Block, Part 3 (Samples)

I've completed my playing with the MS Data Access Application Block for now.  Its time to start saving time and writing cleaner code with it now.  Here is a ZIP containing a webform and codebehind containing calls to Sqlhelper for binding to a grid.  The guts of the thing are below, intended to be uncommented by sample to demo various Sqlhelper methods.  The .ASPX contains a label and datagrid.  That's it.  Also included is a SQL script to create a couple of test procs in PUBS.

// shared connection and query strings
string cn = "server=localhost;uid=sa;pwd=;database=pubs";
string sql = "select au_id, au_lname from authors where au_lname like 'm%' ";
string sql2 = "select pub_id, pub_name from publishers where pub_name like '%pub%' ";
string sql3 = "select title_id, title from titles where title like '%computer%' ";

// SAMPLE ONE: using FillDataSet with multiple query strings in a single pass 

// DataSet ds = new DataSet();
// SqlHelper.FillDataset(cn,CommandType.Text, sql + sql2 + sql3, ds, new string[]{"authors","pubs","titles"});
// lblTableName.Text = ds.Tables[2].TableName.ToString();
// dgtest.DataSource = ds.Tables[2].DefaultView; 
// See my DAAB, Part 2 post below on updating Sqlhelper.cs to use tablenames, i.e., ds.Tables["titles"].DefaultView;

// SAMPLE TWO: using ExecuteDataSet with multiple sql statements. Returns Table0, Table1, etc. 

// lblTableName.Text = SqlHelper.ExecuteDataset(cn, CommandType.Text, sql + sql2).Tables[1].TableName;
// dgtest.DataSource = SqlHelper.ExecuteDataset(cn, CommandType.Text, sql + sql2).Tables[1].DefaultView;

// SAMPLE THREE: using ExecuteDataSet with stored procedure and two string param input values

// string lname_initial = "m";
// string state = "CA";
// dgtest.DataSource = SqlHelper.ExecuteReader(cn, "p_authors", new String[]{lname_initial, state});

// SAMPLE FOUR: using ExecuteDataSet with stored procedure and one string param, one integer input values

// string stor_id = "8042";
// int qty = 15;
// dgtest.DataSource = SqlHelper.ExecuteReader(cn, "p_sales", new String[]{stor_id, qty.ToString()});


How's it go?  This posting and all associated code is provided "AS IS" with no warranties, and confers no rights.  Use at own risk and have fun. 

 

Comments

Onder said:

Hi Dave,

how does it work when i call a stored procedure with more than one SELECT statements?
Does ExecuteDataSet method return more than one table?
I may be doing something wrong but i get an error when i reference the returned dataset's second table!
# September 18, 2003 4:24 AM

Dave Burke said:

Onder, Could you reference the second table prior to using the DAAB? I always thought that a stored procedure could return one table only?... It can execute multiple query statements or execute multiple stored procs, but not return more than one table. Interesting. Maybe I'm wrong about that.
# September 18, 2003 8:10 AM

Onder said:

Dave i solved the problem :)
it was my mistake! It is okay with the daab ExecuteDataSet method returning two tables for a stored proc. with two select statements. No problem.

I my case, it is a good idea for my application to read the contents of two different database tables so i do not read to make two queries to the database.
# September 18, 2003 10:30 AM

dshorter said:

Onder,

What does your SP look like?
# December 8, 2003 12:25 PM

chris said:

dshorter, you can include statments in your SP like:

CREATE PROCEDURE testproc
AS
SELECT * FROM table1;
SELECT * FROM table2;
GO

This will work with a FillDataSet method similar to:

SqlHelper.FillDataset(cn,CommandType.StoredProcedure,"testproc",ds,new string[] {"table1","table2"});
# December 26, 2003 2:58 AM

Han said:

hi,what if i want to return a multiset of select statement from my SP and the name of the table is based on my SP rather then name it inside my code

Example:
in my SP

SELECT * FROM tbl1
SELECT * FROM tbl2


in side my code
DataSet ds;
ds=SqlHelper.FillDataSet(objConn,"spName",CommandType.StoredProcedure);
//automatically i will have my datatable in my dataset name as tbl1 and tbl2.

Can this bedone in DataAccess Application Block???
please advise.
# April 17, 2004 10:40 PM

Dave Burke said:

Han,

SqlHelper.FillDataset(cn,CommandType.StoredProcedure,"testproc",ds,new string[] {"tbl1","tbl2"});

oughta do it for you. Then, after the statement you can immediately do a

DataGrid1.DataSource = tbl1.DefaultView;
DataGrid1.DataBind();

or whatever. Maybe I'm missing something?
# April 18, 2004 7:51 AM

frances said:

I do not have

SqlHelper.FillDataset in Data Access Application Block,. Kind of weird.

# July 6, 2007 2:59 PM

shashank said:

hi Dave,

Is it possible to set SqlCommand Timeout using the MS datablocks? i am getting query timeout on a large query and want to set the command timeout, do u have any idea on this.

# March 18, 2008 8:59 AM

shashank said:

Dave, well i made some reading on the net and found out that the Data block version 2.0, is distributed as source code. I made changes (command.timeout) to the method and its working now.

# March 18, 2008 11:16 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)