Wednesday, October 18, 2006

 

Code Sample: Passing object[] to SqlHelper

Thanks, Steve. Of course there should be some code. Here:
Apologies for the lost indentation: that I can't use CopyAsHtml well at blogger.com is probably the biggest reason that I should be blogging with Subtext.


// Call SqlHelper with the 'params SqlParameter[] overload'

public void GetWithSqlParams(SystemUser aUser)

{

SqlParameter idParam = new SqlParameter("@id", aUser.Id);

SqlParameter nameParam = new SqlParameter("@name", aUser.Name);

SqlParameter emailParam = new SqlParameter("@name", aUser.Email);

SqlParameter loginParam = new SqlParameter("@name", aUser.LastLogin);

SqlParameter logoutParam = new SqlParameter("@name", aUser.LastLogOut);

SqlHelper.ExecuteNonQuery(

Settings.ConnectionString, CommandType.StoredProcedure, "User_Update",

idParam,

nameParam,

emailParam,

loginParam,

logoutParam);

}

// Call SqlHelper with the 'params object[] overload'

public void GetWithObjects(SystemUser aUser)

{

SqlHelper.ExecuteNonQuery(

Settings.ConnectionString, "User_Update",

aUser.Id,

aUser.Email,

aUser.Name,

aUser.LastLogin,

aUser.LastLogOut);

}

// Which is semantically identical with:

public void GetWithObjectArray(SystemUser aUser)

{

SqlHelper.ExecuteNonQuery(

Settings.ConnectionString, "User_Update",

new object[]

{

aUser.Id,

aUser.Email,

aUser.Name,

aUser.LastLogin,

aUser.LastLogOut

});

}


Comments:
I'm guessing all of those @name parameter names in the first example was a typo?
 
Here is one more tool that helps in writing SQL code https://www.devart.com/dbforge/sql/sqlcomplete/code-completion.html
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?