SqlParameter to SQL declare representation

A

antonitomt

Hello guys,
I'm working on some dynamic sql.

Is there a way, method or function to convert in VS a Sqlparameter variableinto a string with the equivalent declare t-sql (i.e: "@parámetro Numeric(15,2))",
"@parámetro Tinyint", "parámetro Date" and so on.. ?

Thanks

Sorry my englisth is bad.
 
M

Marcel Müller

Hello guys,
I'm working on some dynamic sql.

Is there a way, method or function to convert in VS a Sqlparameter variable into a string with the equivalent declare t-sql (i.e: "@parámetro Numeric(15,2))",
"@parámetro Tinyint", "parámetro Date" and so on.. ?

I do not know a built in way to do so. But it should be straight forward
to write a function the converts the type in an SqlParameter instance to
an T-SQL type.

Something like

public static string ToSQLTypeString(this SqlParameter param)
{
switch (param.DbType)
{case DbType.Int32:
return "INT";
case DbType.Decimal:
return string.Format("DECIMAL({0},{1})", param.Size, param.Scale);
...
}
}

Now you can write

string.Format("DECLARE {0} {1};", "@variable_name",
ToSQLTypeString(sqlparam));


Marcel
 
A

Arne Vajhøj

I'm working on some dynamic sql.

Is there a way, method or function to convert in VS a Sqlparameter variable into a string with the equivalent declare t-sql (i.e: "@parámetro Numeric(15,2))",
"@parámetro Tinyint", "parámetro Date" and so on.. ?

I would expect:

p.ParameterName
p.SqlDbType
p.Size
p.Precision

to provide what you need.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top