CLR parameter-length?

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 28
Reputation: sniper1983 is an unknown quantity at this point 
Solved Threads: 0
sniper1983 sniper1983 is offline Offline
Light Poster

CLR parameter-length?

 
0
  #1
Nov 15th, 2008
Hi there,

I'm wondering whether one of you could clarify how to define the maxlength in bits when inserting a parameter in a stored procedure f.ex. What I might do is something like this:

[Microsoft.SqlServer.Server.SqlProcedure]
public static void CreateUser(SqlChars pInitials ... )
{
SqlCommand mSqlCommand = new SqlCommand("", new SqlConnection("context connection=true"));
mSqlCommand.CommandText = @"INSERT INTO .... ";

....

The databaseField to insert data might only accept VarChar(32) So I have to check for this (max 32 chars) when inserting to the DB. How can I do that in the parameterlist?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: CLR parameter-length?

 
0
  #2
Nov 15th, 2008
you don't have to, it will automatically trim it

or lets see you get a string field that you are going to insert, with max length of 5
  1. string fName = "jonathan";
  2.  
  3. if(fName.length > 5)
  4. {
  5. //you can handle what to do with the length here
  6. //we can trim it, throw an error, or whatever else
  7. }
  8.  
  9. //create command text with our parameter
  10. mSqlCommand.CommandText = @"INSERT INTO table (FIRST_NAME) values " + fName;
Last edited by dickersonka; Nov 15th, 2008 at 6:40 pm.
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: sniper1983 is an unknown quantity at this point 
Solved Threads: 0
sniper1983 sniper1983 is offline Offline
Light Poster

Re: CLR parameter-length?

 
0
  #3
Nov 15th, 2008
Thank you.. I know that I can test it inside but if I want to use the parameters directly into the sqlstring then its not that practical. But you say that I don't have to. But after inserting the stored procedure my sqlchars is analysed to pInitials (nvarchar(max)), but this "max" can it automatically test this when used in the sql. Like this:

public static void CreateUser(SqlChars pInitials, SqlChars pFirstName, SqlChars pSirName, SqlInt16 pSecurityLevel, SqlChars pPassword,
SqlChars pEmailAddress, SqlChars pPhoneNumber, SqlChars pMobileNumber)
{
SqlCommand mSqlCommand = new SqlCommand("", new SqlConnection("context connection=true"));
SqlCommand.CommandText = @"INSERT INTO MAIN.dbo.USER (Initials, FirstName, SirName, SecurityLevel, Password,
EmailAddress, PhoneNumber, MobileNumber) VALUES
(@pInitials, @pFirstName, @pSirName, @pSecurityLevel, @pPassword,
@pEmailAddress,@pPhoneNumber,@pMobileNumber)";
mSqlCommand.Connection.Open();

SqlPipe pipe = SqlContext.Pipe;
pipe.ExecuteAndSend(mSqlCommand);
mSqlCommand.Connection.Close();
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: CLR parameter-length?

 
0
  #4
Nov 15th, 2008
you want to test the parameter? like pFirstName?

also when inserting, rather than messing with sqlchars class, i would just use a string
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: sniper1983 is an unknown quantity at this point 
Solved Threads: 0
sniper1983 sniper1983 is offline Offline
Light Poster

Re: CLR parameter-length?

 
0
  #5
Nov 15th, 2008
Originally Posted by dickersonka View Post
you want to test the parameter? like pFirstName?

also when inserting, rather than messing with sqlchars class, i would just use a string
Yes I want to secure that a value is no longer than the maximum chars in the db column. Ex varchar(30) for instance.
I could do it like this
if (pFirstName.length > 30) {
Continue.. }
else{
Throw an exception..}

But I was hoping that I could use the CLR-methods parameters directly into the sql-string as shown. Is this possible or should I actually check every parameter in the input about the right length?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: CLR parameter-length?

 
0
  #6
Nov 16th, 2008
i don't understand what you can't do, you can check the length, but there isn't something to throw an exception from the runtime automatically
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: sniper1983 is an unknown quantity at this point 
Solved Threads: 0
sniper1983 sniper1983 is offline Offline
Light Poster

Re: CLR parameter-length?

 
0
  #7
Nov 16th, 2008
Forget the exception, forget the check on the length of the individual parameters. What I'm asking is if it is nessesary to check the length of the incomming parameter to the function before using them in the sql-string.
I'm having some parameters in the CLR-function/stored procedure defined as sqlvarchar for example; And I have a sql-string. What I want to do is to secure that the length of the parameter does not exceed the length of the DB-field to insert the value into. Can't I do this without checking each parameters length inside the CLR-stored procedure?
Like this (The text in red is what I want to do but the syntax is not right. In this case securing the db-field Initials with the dbtype varchar(30) ):

public static void CreateUser(SqlChars(30) pInitials)
{
SqlCommand mSqlCommand = new SqlCommand("", new SqlConnection("context connection=true"));
SqlCommand.CommandText = @"INSERT INTO MAIN.dbo.USER (Initials) Values (@pInitials)";
mSqlCommand.Connection.Open();

SqlPipe pipe = SqlContext.Pipe;
pipe.ExecuteAndSend(mSqlCommand);
mSqlCommand.Connection.Close();
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: CLR parameter-length?

 
0
  #8
Nov 17th, 2008
no you must create your own validation code

think of it this way, what happens if you change the length of your field in the db, how can the code know

you can either check it code side, or create a stored procedure that will check the length along with the insert
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC