Eseguire una funzione sql da c#

martedì 08 marzo 2016 - 13.11
Tag Elenco Tags  C#  |  VB.NET  |  .NET 2.0  |  .NET 3.0  |  .NET 3.5  |  .NET 4.0  |  Windows 7  |  Windows Vista  |  Windows XP  |  Visual Studio 2010  |  Visual Studio 2008  |  Visual Studio 2005  |  Visual Studio 2003  |  SQL Server 2008 R2  |  SQL Server 2008  |  SQL Server 2005  |  SQL Server 2000  |  Office 2010  |  Internet explorer 8.0  |  Internet Explorer 7.0  |  Internet Explorer 6.0  |  Chrome  |  Opera  |  Firefox  |  Javascript  |  Crystal Reports 10.0  |  SQL Reporting Services

memmo77 Profilo | Expert

Salve a tutti, ho una funzione sql chiamata CAP_INT che accetta 6 parametri:

CREATE FUNCTION CAP_INT (@valore1 decimal(24,12), @valore2 decimal(24,12), @valore3 SMALLINT, @valore4 SMALLINT, @valore5 SMALLINT, @valore6 INT)....

Vorrei richiamarla da una classe c#, questo il codice che utilizzo ma mi da un errore sul ritorno:

Il codice sorgente non è stato renderizzato qui
perchè non c'è sufficiente spazio.
Clicca qui per visualizzarlo in una nuova finestra

Grazie a tutti!

sanghino Profilo | Junior Member

Puoi dirci che tipo di errore ti restituisce ?

In ogni modo, a grandi linee questi sono i metodi per eseguire le SP.

Se non restituisce valori:
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String"); SqlCommand cmd = new SqlCommand(); Int32 rowsAffected; cmd.CommandText = "StoredProcedureName"; cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = sqlConnection1; sqlConnection1.Open(); rowsAffected = cmd.ExecuteNonQuery(); sqlConnection1.Close();

Se deve restituire valori:

SqlConnection sqlConnection1 = new SqlConnection("Your Connection String"); SqlCommand cmd = new SqlCommand(); SqlDataReader reader; cmd.CommandText = "StoredProcedureName"; cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = sqlConnection1; sqlConnection1.Open(); reader = cmd.ExecuteReader(); // Data is accessible through the DataReader object here. sqlConnection1.Close();

Ciao !!
Sanghino
--------------------------
www.extrageek.com

memmo77 Profilo | Expert

Ciao grazie della tua risposta, si trattava solo di un errore di sintassi nella chiama della funzione, comunque ho risolto nel seguente modo:

public double capInt() { BL.Dati.ConfConnessione connessione = new BL.Dati.ConfConnessione(); double res = 0; using (SqlConnection conn = new SqlConnection(connessione.getConnectionString())) { using (SqlCommand comm = new SqlCommand("dbo.CAP_INT", conn)) { comm.CommandType = CommandType.StoredProcedure; SqlParameter p1 = new SqlParameter("@valore1", SqlDbType.Decimal); SqlParameter p2 = new SqlParameter("@valore2", SqlDbType.Decimal); SqlParameter p3 = new SqlParameter("@valore3", SqlDbType.SmallInt); SqlParameter p4 = new SqlParameter("@valore4", SqlDbType.SmallInt); SqlParameter p5 = new SqlParameter("@valore5", SqlDbType.SmallInt); SqlParameter p6 = new SqlParameter("@valore6", SqlDbType.Int); //SqlParameter p2 = new SqlParameter("@Result", SqlDbType.Bit); //p1.Direction = ParameterDirection.Input; //p2.Direction = ParameterDirection.ReturnValue; p1.Value = _valore1; p2.Value = _valore2; p3.Value = _valore3Int; p4.Value = _valore4Int; p5.Value = _valore5Int; p6.Value = _valore6; comm.Parameters.Add(p1); comm.Parameters.Add(p2); comm.Parameters.Add(p3); comm.Parameters.Add(p4); comm.Parameters.Add(p5); comm.Parameters.Add(p6); conn.Open(); comm.ExecuteNonQuery(); if (p2.Value != DBNull.Value) res = (double)p2.Value; } } return res; }

Grazie ciao!
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5