index > .NET Framework Data Access and Storage > How to Pass in a SQL Decimal with Precision and Scale using DAAB?

How to Pass in a SQL Decimal with Precision and Scale using DAAB?

I'm trying to port my websites to the DAAB and I have a question: how do you get the output of a SQL Server DECIMAL(8,2) datatype in the AddOutParameter() method? Here's my original ADO.NET code:

SqlParameter pPrice = new
Scott Whigham

Here's a "workaround" (if that's the correct way to think of it);

protected void Page_Load(object sender, EventArgs e){

SqlDatabase sqlDatabase = new SqlDatabase("data source=(local);initial catalog=tempdb;Integrated Security = SSPI;");

DbCommand myCommand = sqlDatabase.GetStoredProcCommand("dbo.MyProc"); // CREATE PROC MyProc (@p1 DECIMAL(8,2) OUTPUT) AS SET @p1 = 8.35

// sqlDatabase.AddOutParameter(myCommand, "p1", DbType.Decimal, 10); // this returns only the number 8 and uses NUMERIC(29,0)

SqlParameter pOut = new SqlParameter("@p1", SqlDbType.Decimal);

pOut.Precision = 8;

pOut.Scale = 2;

pOut.Direction = ParameterDirection.Output;

myCommand.Parameters.Add(pOut);

sqlDatabase.ExecuteNonQuery(myCommand);

Response.Write(sqlDatabase.GetParameterValue(myCommand, "@p1").ToString());

}




Check out http://www.LearnSqlServer.com - 20+ hours of SQL Server 2005/2000 tutorials
Scott Whigham

Of course, that begs the question: "Is there a better way to do this?"

Anyone else have experience with this?




Check out http://www.LearnSqlServer.com - 20+ hours of SQL Server 2005/2000 tutorials
Scott Whigham
This is for the DAAB 2.0:


Dim db As Database = DatabaseFactory.CreateDatabase()

dbComm = db.GetStoredProcCommand("dbo.MyProc")

db.AddParameter(dbComm, "@p1", DbType.Decimal, 8, ParameterDirection.Output, True, 8, 2, "p1", DataRowVersion.Current, value)
kbradl1

You feel for it too, eh? That was my first try as well :)

Your code, unfortunately, submits the following to SQL Server:

declare @p1 numeric(29,0)
set @p1=8
exec dbo.MyProc @p1=@p1 output
select @p1

That ain't what we want... Notice how it ignores your precision and scale parameters.

And, as I mentioned in the first entry, it's illogical for me to pass in DataRowVersion, nullability, and "value" for an output parameter. What "value" should I pass in to an output parameter?

Also, how did you calculate the Length? I see that you choose "8" as the length. Why 8? Why not 10 or 19? I honestly don't have an answer; I'm asking because I don't know why you chose 8.

Just to make sure I have it right, here's what I used:

// CREATE PROC MyProc(@p1 DECIMAL(8,2) OUTPUT) AS SET @p1 = 8.35;

SqlDatabase sqlDatabase = new SqlDatabase("data source=(local);initial catalog=tempdb;Integrated Security = SSPI;");

DbCommand myCommand = sqlDatabase.GetStoredProcCommand("dbo.MyProc");

sqlDatabase.AddParameter(myCommand, "@p1", DbType.Decimal, 8, ParameterDirection.Output, true, 8, 2, "p1", DataRowVersion.Current, 666);

sqlDatabase.ExecuteNonQuery(myCommand);

decimal p1 = (Decimal)sqlDatabase.GetParameterValue(myCommand, "p1");




Check out http://www.LearnSqlServer.com - 20+ hours of SQL Server 2005/2000 tutorials
Scott Whigham
reply 5

You can use google to search for other answers

 

More Articles

• [OTP]mouseover and open new page
• Application Blocks
• Executing Oracle function using ADO.Net
• Some how the update command is not working.
• Question regarding TableAdapters.
• insert, delete, update in a datagrid and the permanent table? thr...
• SqlDbType enumeration value invalid
• Determine Primary Keys using SqlConnection.GetSchema()
• Dinamically adding columns
• Data Adapter Error VB.NET 2003 SQL 2005
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Images from the database
• Help with MS Access SQL Query
• Enterprise Library problem with oracle 1
• Concurrency violation while row exists
• Error when failing over to mirrored data
• international character support in ms ac
• Connecting SqlExpress from .net 1.1
• Update query in DeleteCommand
• Database application
• .Net 2.0 Web App linking to SQL 2000
• DOA2016.DLL
• Can't figure out this error message
• OleDbPermission
• oracleCommand error
• UpdateBatchSize without dataSets

Hot Articles

• getting info from database
• Application Startup
• Sql database network connection
• Very strange SqlCommand TimeOut question
• access sql server database with vb
• suggestions for naming a Name field in a
• Read data from an Opened excel file with
• UnExpected reflaction after Updating a t
• Help trying to Insert to Access DB with
• An error has occurred while establishing
• Application Blocks
• How do I change the location of "|D
• OraclePermission
• DataView, CType's Expression & Sqlex
• Access Excel File On Different Server **

Recommend Articles

• ADO Recordset Getting Closed in C#
• Data Sources Window
• Program hangs on remote connection when
• DateTime Conversions
• Import Excel sheet using ASP.net web app
• Errors creating new data source
• suggestion on DB design
• Repeating name in namespace + classname,
• How to Manage Transactions
• questions about Automatically Generating
• Test if a MDB is Corrupted
• TransactionScope, Transactions and MSDTC
• Trusted_Connection Problem with VS2005
• Crystal Report Linked to access database
• Parameters.AddWithValue Output paramete