Results 1 to 13 of 13

Thread: Microsoft Data Access Application Block

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Microsoft Data Access Application Block

    I was just reading an article on Data Access Application Blocks here, and had a few questions:

    1) Is there a simpler way to type it? How about DAAB?

    2) How often is this used? Do you use DAAB in your applications, or is the general practice to use normal coding?

  2. #2

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    BURP.

    I mean... BUMP.

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Nobody likes me.

    Well, I just thought I'd share a bit of what I think of DAAB here. I tried it out, and I like the way that they have implemented this. Very good work by them. Who says they don't write good code!

    Data access has now become extremely simple:

    Connecting is as easy as performing brain surgery on one of my coworkers (not required):

    VB Code:
    1. Dim ds As DataSet
    2.         Dim strConnection As String
    3.         Dim categoryID As Integer
    4.  
    5.         strConnection = "Data Source=(local);" & _
    6.                             "Initial Catalog=NorthWind;" & _
    7.                             "User ID=sa;Password=;"
    8.         categoryID = 4
    9.  
    10.  
    11. 'This line is for Stored Procs
    12.         'ds = SqlHelper.ExecuteDataset(strConnection, CommandType.StoredProcedure, "getProductsByCategory", New SqlParameter("@CategoryID", categoryID))
    13.  
    14. 'This is for SQL statements
    15.         ds = SqlHelper.ExecuteDataset(strConnection, CommandType.Text, "SELECT * FROM Products")

    And there, you're done.

    The helpfile left me asking for more, but it's for the best. I can always keep working on it.

    Good stuff, I definitely reccommend this.
    Last edited by mendhak; Jun 28th, 2004 at 12:37 AM.

  4. #4

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    And look at this:

    To update, using a stored proc:

    VB Code:
    1. Dim trans As SqlTransaction = conn.BeginTransaction()
    2.  
    3.        'Define the parameters.
    4.  
    5.         Dim params(1) As SqlParameter
    6.  
    7.         'Here is one way to do it... the detailed way...
    8.         params(0) = New SqlParameter
    9.  
    10.         params(0).ParameterName = "@varQPU"
    11.         params(0).DbType = SqlDbType.VarChar
    12.         params(0).Size = 20
    13.         params(0).Value = TextBox1.Text
    14.  
    15.  
    16.         'The other way
    17.         params(1) = New SqlParameter("@PID", SqlDbType.Int)
    18.         params(1).Value = PID
    19.  
    20.  
    21.  
    22.         Try
    23.  
    24.            
    25. 'Update using a stored proc  
    26.          'SqlHelper.ExecuteNonQuery(trans, CommandType.StoredProcedure, "update_products", params)
    27.  
    28. 'Or update using a SQL statement.  
    29.          SqlHelper.ExecuteNonQuery(trans, CommandType.Text, "UPDATE Products SET QuantityPerUnit = '" & TextBox1.Text & "' WHERE ProductID = " & PID & ";")
    30.  
    31.  
    32.  
    33.  
    34.  
    35.             trans.Commit()

    This is great stuff. You should be using it too.

    Ok, I'll shuttup now.

  5. #5
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Hey mendhak,

    I read this a few weeks ago, (maybe months, you know how it is in a dungeon) and started looking into this and other blocks on MSDN. While the method structure follows microsofts best practices, the code contained within does not full use the best optimized method calls:

    eg.
    VB Code:
    1. Dim paramInstance As IDbDataParameter = CType(parameterValues(i), IDbDataParameter)

    should be coded thus:
    VB Code:
    1. Dim paramInstance As IDbDataParameter = DirectCast(parameterValues(i), IDbDataParameter)

    The entire block is rife with this kinds of errors, so while the general concept is a good one, the block should (and I am) be recoded.
    Whadayamean it doesn't work....
    It works fine on my machine!

  6. #6
    Member EricDalnas's Avatar
    Join Date
    Sep 2004
    Location
    Rhode Island
    Posts
    51

    uh huh

    I use it.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Originally posted by CyberHawke
    Hey mendhak,

    I read this a few weeks ago, (maybe months, you know how it is in a dungeon) and started looking into this and other blocks on MSDN. While the method structure follows microsofts best practices, the code contained within does not full use the best optimized method calls:

    eg.
    VB Code:
    1. Dim paramInstance As IDbDataParameter = CType(parameterValues(i), IDbDataParameter)

    should be coded thus:
    VB Code:
    1. Dim paramInstance As IDbDataParameter = DirectCast(parameterValues(i), IDbDataParameter)

    The entire block is rife with this kinds of errors, so while the general concept is a good one, the block should (and I am) be recoded.
    In your example why do you feel that Ctype is less efficent then DirectCast?

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Originally posted by mendhak
    And look at this:

    To update, using a stored proc:

    VB Code:
    1. Dim trans As SqlTransaction = conn.BeginTransaction()
    2.  
    3.        'Define the parameters.
    4.  
    5.         Dim params(1) As SqlParameter
    6.  
    7.         'Here is one way to do it... the detailed way...
    8.         params(0) = New SqlParameter
    9.  
    10.         params(0).ParameterName = "@varQPU"
    11.         params(0).DbType = SqlDbType.VarChar
    12.         params(0).Size = 20
    13.         params(0).Value = TextBox1.Text
    14.  
    15.  
    16.         'The other way
    17.         params(1) = New SqlParameter("@PID", SqlDbType.Int)
    18.         params(1).Value = PID
    19.  
    20.  
    21.  
    22.         Try
    23.  
    24.            
    25. 'Update using a stored proc  
    26.          'SqlHelper.ExecuteNonQuery(trans, CommandType.StoredProcedure, "update_products", params)
    27.  
    28. 'Or update using a SQL statement.  
    29.          SqlHelper.ExecuteNonQuery(trans, CommandType.Text, "UPDATE Products SET QuantityPerUnit = '" & TextBox1.Text & "' WHERE ProductID = " & PID & ";")
    30.  
    31.  
    32.  
    33.  
    34.  
    35.             trans.Commit()

    This is great stuff. You should be using it too.

    Ok, I'll shuttup now.
    Building your sql statement in that manner leaves you open to SQL injection attacks. You should use parameterized queries where ever possible.

    VB Code:
    1. SqlHelper.ExecuteNonQuery(trans, CommandType.Text, "UPDATE Products SET QuantityPerUnit = @Text WHERE ProductID = @ProductId",New SqlParameter("@Text", TextBox1.Text)New SqlParameter("@ProductId", PID)

    As a simple example try typing the following into TextBox1 with your code:

    ' WHERE 1 = 1 --
    Last edited by Edneeis; Sep 15th, 2004 at 10:56 PM.

  9. #9

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    This thread is three months old!!

    I was only experimenting at the time, and just thought I'd share some stuff...

    I recently found out about the little textbox trick from Woka.

    In a textbox, you could go something like

    <removed: Should I even be discussing this here? >

    and it'd wreak havoc, assuming you know a few details there.

  10. #10
    Hyperactive Member Kirun's Avatar
    Join Date
    Oct 2001
    Location
    Karachi , Pakistan
    Posts
    333
    i am also using that application in my code and its working excellent for me ...

  11. #11
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    It was not my intent to indicate that there was some problem with the code base, or that it does not work. Microsofts description of the Block states that it has been optimized to follow their best practices. If you review the code in the block you will find a number of things that are not optimized and also a number of inconsistencies.

    Does the code work? Yes
    Is it optimized? No

    And because it is not optimized, you are no better off using it than if you had written the code yourself.

    Am I suggesting that you not use it? No

    Because it does wrap the functionality of accessing data using SqlClient methods quite nicely and provides an excellent resource for novice programmers who need help with rolling out a project and are looking for quick and easy ways to write their data access code.

    Buf if you are an experienced programmer, and are familiar with Microsofts best practices, and are accustomed to writing consitent code then you will spot the problems in the Block that I have been speaking of.

    Armed with that knowledge, you will probably want to rewrite the code in a more optimzed way, and also to ensure consistency throughout the code.
    Whadayamean it doesn't work....
    It works fine on my machine!

  12. #12
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Originally posted by mendhak
    This thread is three months old!!

    I was only experimenting at the time, and just thought I'd share some stuff...

    I recently found out about the little textbox trick from Woka.

    In a textbox, you could go something like

    <removed: Should I even be discussing this here? >

    and it'd wreak havoc, assuming you know a few details there.
    I have attended several anti-hacker seminars, and it's my experience that you should not discuss even well known hacks because there is that person out there, who has never been a hacker, but sees your example, wants to try it out, and then the seed is planted.
    Whadayamean it doesn't work....
    It works fine on my machine!

  13. #13

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Agreed. I won't even mention that neat little DOS command line hack that you can use to <removed by mendhak> to gain access to <removed by aliens>


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width