Calling SQL Defined function in C#
up vote
12
down vote
favorite
I have written this scalar function in TSQL:
create function TCupom (@cupom int)
returns float
as
begin
declare @Tcu float;
select @Tcu = sum (total) from alteraca2 where pedido = @cupom
if (@tcu is null)
set @tcu = 0;
return @tcu;
end
I want to call this function in my C# code. Here's what I have so far:
public void TotalCupom(int cupom)
{
float SAIDA;
SqlDataAdapter da2 = new SqlDataAdapter();
if (conex1.State == ConnectionState.Closed)
{
conex1.Open();
}
SqlCommand Totalf = new SqlCommand("Tcupom", conex1);
SqlParameter code1 = new SqlParameter("@code", SqlDbType.Int);
code1.Value = cupom ;
Totalf.CommandType = CommandType.StoredProcedure ;
SAIDA = Totalf.ExecuteScalar();
return SAIDA;
}
c# sql visual-studio-2010 sql-server-2008
add a comment |
up vote
12
down vote
favorite
I have written this scalar function in TSQL:
create function TCupom (@cupom int)
returns float
as
begin
declare @Tcu float;
select @Tcu = sum (total) from alteraca2 where pedido = @cupom
if (@tcu is null)
set @tcu = 0;
return @tcu;
end
I want to call this function in my C# code. Here's what I have so far:
public void TotalCupom(int cupom)
{
float SAIDA;
SqlDataAdapter da2 = new SqlDataAdapter();
if (conex1.State == ConnectionState.Closed)
{
conex1.Open();
}
SqlCommand Totalf = new SqlCommand("Tcupom", conex1);
SqlParameter code1 = new SqlParameter("@code", SqlDbType.Int);
code1.Value = cupom ;
Totalf.CommandType = CommandType.StoredProcedure ;
SAIDA = Totalf.ExecuteScalar();
return SAIDA;
}
c# sql visual-studio-2010 sql-server-2008
1
Are you having a problem? Would you like to tell us what it is?
– SLaks
Jun 11 '13 at 14:49
Possible duplicate of ExecuteScalar always returns null when calling a scalar-valued function
– GSerg
Aug 23 at 12:45
add a comment |
up vote
12
down vote
favorite
up vote
12
down vote
favorite
I have written this scalar function in TSQL:
create function TCupom (@cupom int)
returns float
as
begin
declare @Tcu float;
select @Tcu = sum (total) from alteraca2 where pedido = @cupom
if (@tcu is null)
set @tcu = 0;
return @tcu;
end
I want to call this function in my C# code. Here's what I have so far:
public void TotalCupom(int cupom)
{
float SAIDA;
SqlDataAdapter da2 = new SqlDataAdapter();
if (conex1.State == ConnectionState.Closed)
{
conex1.Open();
}
SqlCommand Totalf = new SqlCommand("Tcupom", conex1);
SqlParameter code1 = new SqlParameter("@code", SqlDbType.Int);
code1.Value = cupom ;
Totalf.CommandType = CommandType.StoredProcedure ;
SAIDA = Totalf.ExecuteScalar();
return SAIDA;
}
c# sql visual-studio-2010 sql-server-2008
I have written this scalar function in TSQL:
create function TCupom (@cupom int)
returns float
as
begin
declare @Tcu float;
select @Tcu = sum (total) from alteraca2 where pedido = @cupom
if (@tcu is null)
set @tcu = 0;
return @tcu;
end
I want to call this function in my C# code. Here's what I have so far:
public void TotalCupom(int cupom)
{
float SAIDA;
SqlDataAdapter da2 = new SqlDataAdapter();
if (conex1.State == ConnectionState.Closed)
{
conex1.Open();
}
SqlCommand Totalf = new SqlCommand("Tcupom", conex1);
SqlParameter code1 = new SqlParameter("@code", SqlDbType.Int);
code1.Value = cupom ;
Totalf.CommandType = CommandType.StoredProcedure ;
SAIDA = Totalf.ExecuteScalar();
return SAIDA;
}
c# sql visual-studio-2010 sql-server-2008
c# sql visual-studio-2010 sql-server-2008
edited Mar 16 at 9:25
Massimiliano Kraus
2,33741532
2,33741532
asked Jun 11 '13 at 14:47
alejandro carnero
77061836
77061836
1
Are you having a problem? Would you like to tell us what it is?
– SLaks
Jun 11 '13 at 14:49
Possible duplicate of ExecuteScalar always returns null when calling a scalar-valued function
– GSerg
Aug 23 at 12:45
add a comment |
1
Are you having a problem? Would you like to tell us what it is?
– SLaks
Jun 11 '13 at 14:49
Possible duplicate of ExecuteScalar always returns null when calling a scalar-valued function
– GSerg
Aug 23 at 12:45
1
1
Are you having a problem? Would you like to tell us what it is?
– SLaks
Jun 11 '13 at 14:49
Are you having a problem? Would you like to tell us what it is?
– SLaks
Jun 11 '13 at 14:49
Possible duplicate of ExecuteScalar always returns null when calling a scalar-valued function
– GSerg
Aug 23 at 12:45
Possible duplicate of ExecuteScalar always returns null when calling a scalar-valued function
– GSerg
Aug 23 at 12:45
add a comment |
2 Answers
2
active
oldest
votes
up vote
32
down vote
accepted
You can't just call the function name, you will need to write an inline SQL statement which makes use of the UDF:
SqlCommand Totalf = new SqlCommand("SELECT dbo.Tcupom(@code)", conex1);
And remove the CommandType
, this isn't a Stored Procedure, its a User Defined Function.
In all:
public void TotalCupom(int cupom)
{
float SAIDA;
SqlDataAdapter da2 = new SqlDataAdapter();
if (conex1.State == ConnectionState.Closed)
{
conex1.Open();
}
SqlCommand Totalf = new SqlCommand("SELECT dbo.Tcupom(@code)", conex1);
SqlParameter code1 = new SqlParameter("@code", SqlDbType.Int);
code1.Value = cupom;
SAIDA = Totalf.ExecuteScalar();
return SAIDA;
}
Actually because he didTotalf.CommandType = CommandType.StoredProcedure
he can just call the function name.
– Scott Chamberlain
Jun 11 '13 at 14:50
@ScottChamberlain Are you sure? What if there was a stored procedure calledTcupom
? Surely SQL Server would get confused...
– Curt
Jun 11 '13 at 14:52
2
No, you are right, I just realized he was using a scalar function, not a table based function. I don't know if those can be called via SqlCommand set as StoredProcedure
– Scott Chamberlain
Jun 11 '13 at 14:53
2
Refresh your page, I agree with you now.
– Scott Chamberlain
Jun 11 '13 at 14:57
1
number of corrections: 1. your method has a void return type and, you are returning something else (return SAIDA;) 2. you have initialize an SqlDataAdapter and it is never used. 3. ExecuteScalar(); returns an object, needs type conversion.
– Sam Saarian
Dec 5 '17 at 22:18
|
show 4 more comments
up vote
0
down vote
...
try
{
if (connectionState != ConnectionState.Open)
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "udfName";
cmd.CommandType = CommandType.StoredProcedure;
foreach (var cmdParam in sqlParams)
{
cmd.Parameters.Add(cmdParam);
}
var retValParam = new SqlParameter("RetVal", SqlDbType.Int)
{
//Set this property as return value
Direction = ParameterDirection.ReturnValue
};
cmd.Parameters.Add(retValParam);
cmd.ExecuteScalar();
retVal = retValParam.Value;
}
}
finally
{
if (connectionState == ConnectionState.Open)
conn.Close();
}
...
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
32
down vote
accepted
You can't just call the function name, you will need to write an inline SQL statement which makes use of the UDF:
SqlCommand Totalf = new SqlCommand("SELECT dbo.Tcupom(@code)", conex1);
And remove the CommandType
, this isn't a Stored Procedure, its a User Defined Function.
In all:
public void TotalCupom(int cupom)
{
float SAIDA;
SqlDataAdapter da2 = new SqlDataAdapter();
if (conex1.State == ConnectionState.Closed)
{
conex1.Open();
}
SqlCommand Totalf = new SqlCommand("SELECT dbo.Tcupom(@code)", conex1);
SqlParameter code1 = new SqlParameter("@code", SqlDbType.Int);
code1.Value = cupom;
SAIDA = Totalf.ExecuteScalar();
return SAIDA;
}
Actually because he didTotalf.CommandType = CommandType.StoredProcedure
he can just call the function name.
– Scott Chamberlain
Jun 11 '13 at 14:50
@ScottChamberlain Are you sure? What if there was a stored procedure calledTcupom
? Surely SQL Server would get confused...
– Curt
Jun 11 '13 at 14:52
2
No, you are right, I just realized he was using a scalar function, not a table based function. I don't know if those can be called via SqlCommand set as StoredProcedure
– Scott Chamberlain
Jun 11 '13 at 14:53
2
Refresh your page, I agree with you now.
– Scott Chamberlain
Jun 11 '13 at 14:57
1
number of corrections: 1. your method has a void return type and, you are returning something else (return SAIDA;) 2. you have initialize an SqlDataAdapter and it is never used. 3. ExecuteScalar(); returns an object, needs type conversion.
– Sam Saarian
Dec 5 '17 at 22:18
|
show 4 more comments
up vote
32
down vote
accepted
You can't just call the function name, you will need to write an inline SQL statement which makes use of the UDF:
SqlCommand Totalf = new SqlCommand("SELECT dbo.Tcupom(@code)", conex1);
And remove the CommandType
, this isn't a Stored Procedure, its a User Defined Function.
In all:
public void TotalCupom(int cupom)
{
float SAIDA;
SqlDataAdapter da2 = new SqlDataAdapter();
if (conex1.State == ConnectionState.Closed)
{
conex1.Open();
}
SqlCommand Totalf = new SqlCommand("SELECT dbo.Tcupom(@code)", conex1);
SqlParameter code1 = new SqlParameter("@code", SqlDbType.Int);
code1.Value = cupom;
SAIDA = Totalf.ExecuteScalar();
return SAIDA;
}
Actually because he didTotalf.CommandType = CommandType.StoredProcedure
he can just call the function name.
– Scott Chamberlain
Jun 11 '13 at 14:50
@ScottChamberlain Are you sure? What if there was a stored procedure calledTcupom
? Surely SQL Server would get confused...
– Curt
Jun 11 '13 at 14:52
2
No, you are right, I just realized he was using a scalar function, not a table based function. I don't know if those can be called via SqlCommand set as StoredProcedure
– Scott Chamberlain
Jun 11 '13 at 14:53
2
Refresh your page, I agree with you now.
– Scott Chamberlain
Jun 11 '13 at 14:57
1
number of corrections: 1. your method has a void return type and, you are returning something else (return SAIDA;) 2. you have initialize an SqlDataAdapter and it is never used. 3. ExecuteScalar(); returns an object, needs type conversion.
– Sam Saarian
Dec 5 '17 at 22:18
|
show 4 more comments
up vote
32
down vote
accepted
up vote
32
down vote
accepted
You can't just call the function name, you will need to write an inline SQL statement which makes use of the UDF:
SqlCommand Totalf = new SqlCommand("SELECT dbo.Tcupom(@code)", conex1);
And remove the CommandType
, this isn't a Stored Procedure, its a User Defined Function.
In all:
public void TotalCupom(int cupom)
{
float SAIDA;
SqlDataAdapter da2 = new SqlDataAdapter();
if (conex1.State == ConnectionState.Closed)
{
conex1.Open();
}
SqlCommand Totalf = new SqlCommand("SELECT dbo.Tcupom(@code)", conex1);
SqlParameter code1 = new SqlParameter("@code", SqlDbType.Int);
code1.Value = cupom;
SAIDA = Totalf.ExecuteScalar();
return SAIDA;
}
You can't just call the function name, you will need to write an inline SQL statement which makes use of the UDF:
SqlCommand Totalf = new SqlCommand("SELECT dbo.Tcupom(@code)", conex1);
And remove the CommandType
, this isn't a Stored Procedure, its a User Defined Function.
In all:
public void TotalCupom(int cupom)
{
float SAIDA;
SqlDataAdapter da2 = new SqlDataAdapter();
if (conex1.State == ConnectionState.Closed)
{
conex1.Open();
}
SqlCommand Totalf = new SqlCommand("SELECT dbo.Tcupom(@code)", conex1);
SqlParameter code1 = new SqlParameter("@code", SqlDbType.Int);
code1.Value = cupom;
SAIDA = Totalf.ExecuteScalar();
return SAIDA;
}
edited Mar 16 at 9:27
Massimiliano Kraus
2,33741532
2,33741532
answered Jun 11 '13 at 14:49
Curt
75.3k50230316
75.3k50230316
Actually because he didTotalf.CommandType = CommandType.StoredProcedure
he can just call the function name.
– Scott Chamberlain
Jun 11 '13 at 14:50
@ScottChamberlain Are you sure? What if there was a stored procedure calledTcupom
? Surely SQL Server would get confused...
– Curt
Jun 11 '13 at 14:52
2
No, you are right, I just realized he was using a scalar function, not a table based function. I don't know if those can be called via SqlCommand set as StoredProcedure
– Scott Chamberlain
Jun 11 '13 at 14:53
2
Refresh your page, I agree with you now.
– Scott Chamberlain
Jun 11 '13 at 14:57
1
number of corrections: 1. your method has a void return type and, you are returning something else (return SAIDA;) 2. you have initialize an SqlDataAdapter and it is never used. 3. ExecuteScalar(); returns an object, needs type conversion.
– Sam Saarian
Dec 5 '17 at 22:18
|
show 4 more comments
Actually because he didTotalf.CommandType = CommandType.StoredProcedure
he can just call the function name.
– Scott Chamberlain
Jun 11 '13 at 14:50
@ScottChamberlain Are you sure? What if there was a stored procedure calledTcupom
? Surely SQL Server would get confused...
– Curt
Jun 11 '13 at 14:52
2
No, you are right, I just realized he was using a scalar function, not a table based function. I don't know if those can be called via SqlCommand set as StoredProcedure
– Scott Chamberlain
Jun 11 '13 at 14:53
2
Refresh your page, I agree with you now.
– Scott Chamberlain
Jun 11 '13 at 14:57
1
number of corrections: 1. your method has a void return type and, you are returning something else (return SAIDA;) 2. you have initialize an SqlDataAdapter and it is never used. 3. ExecuteScalar(); returns an object, needs type conversion.
– Sam Saarian
Dec 5 '17 at 22:18
Actually because he did
Totalf.CommandType = CommandType.StoredProcedure
he can just call the function name.– Scott Chamberlain
Jun 11 '13 at 14:50
Actually because he did
Totalf.CommandType = CommandType.StoredProcedure
he can just call the function name.– Scott Chamberlain
Jun 11 '13 at 14:50
@ScottChamberlain Are you sure? What if there was a stored procedure called
Tcupom
? Surely SQL Server would get confused...– Curt
Jun 11 '13 at 14:52
@ScottChamberlain Are you sure? What if there was a stored procedure called
Tcupom
? Surely SQL Server would get confused...– Curt
Jun 11 '13 at 14:52
2
2
No, you are right, I just realized he was using a scalar function, not a table based function. I don't know if those can be called via SqlCommand set as StoredProcedure
– Scott Chamberlain
Jun 11 '13 at 14:53
No, you are right, I just realized he was using a scalar function, not a table based function. I don't know if those can be called via SqlCommand set as StoredProcedure
– Scott Chamberlain
Jun 11 '13 at 14:53
2
2
Refresh your page, I agree with you now.
– Scott Chamberlain
Jun 11 '13 at 14:57
Refresh your page, I agree with you now.
– Scott Chamberlain
Jun 11 '13 at 14:57
1
1
number of corrections: 1. your method has a void return type and, you are returning something else (return SAIDA;) 2. you have initialize an SqlDataAdapter and it is never used. 3. ExecuteScalar(); returns an object, needs type conversion.
– Sam Saarian
Dec 5 '17 at 22:18
number of corrections: 1. your method has a void return type and, you are returning something else (return SAIDA;) 2. you have initialize an SqlDataAdapter and it is never used. 3. ExecuteScalar(); returns an object, needs type conversion.
– Sam Saarian
Dec 5 '17 at 22:18
|
show 4 more comments
up vote
0
down vote
...
try
{
if (connectionState != ConnectionState.Open)
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "udfName";
cmd.CommandType = CommandType.StoredProcedure;
foreach (var cmdParam in sqlParams)
{
cmd.Parameters.Add(cmdParam);
}
var retValParam = new SqlParameter("RetVal", SqlDbType.Int)
{
//Set this property as return value
Direction = ParameterDirection.ReturnValue
};
cmd.Parameters.Add(retValParam);
cmd.ExecuteScalar();
retVal = retValParam.Value;
}
}
finally
{
if (connectionState == ConnectionState.Open)
conn.Close();
}
...
add a comment |
up vote
0
down vote
...
try
{
if (connectionState != ConnectionState.Open)
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "udfName";
cmd.CommandType = CommandType.StoredProcedure;
foreach (var cmdParam in sqlParams)
{
cmd.Parameters.Add(cmdParam);
}
var retValParam = new SqlParameter("RetVal", SqlDbType.Int)
{
//Set this property as return value
Direction = ParameterDirection.ReturnValue
};
cmd.Parameters.Add(retValParam);
cmd.ExecuteScalar();
retVal = retValParam.Value;
}
}
finally
{
if (connectionState == ConnectionState.Open)
conn.Close();
}
...
add a comment |
up vote
0
down vote
up vote
0
down vote
...
try
{
if (connectionState != ConnectionState.Open)
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "udfName";
cmd.CommandType = CommandType.StoredProcedure;
foreach (var cmdParam in sqlParams)
{
cmd.Parameters.Add(cmdParam);
}
var retValParam = new SqlParameter("RetVal", SqlDbType.Int)
{
//Set this property as return value
Direction = ParameterDirection.ReturnValue
};
cmd.Parameters.Add(retValParam);
cmd.ExecuteScalar();
retVal = retValParam.Value;
}
}
finally
{
if (connectionState == ConnectionState.Open)
conn.Close();
}
...
...
try
{
if (connectionState != ConnectionState.Open)
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "udfName";
cmd.CommandType = CommandType.StoredProcedure;
foreach (var cmdParam in sqlParams)
{
cmd.Parameters.Add(cmdParam);
}
var retValParam = new SqlParameter("RetVal", SqlDbType.Int)
{
//Set this property as return value
Direction = ParameterDirection.ReturnValue
};
cmd.Parameters.Add(retValParam);
cmd.ExecuteScalar();
retVal = retValParam.Value;
}
}
finally
{
if (connectionState == ConnectionState.Open)
conn.Close();
}
...
answered Nov 19 at 17:46
Yargicx
5401722
5401722
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17047057%2fcalling-sql-defined-function-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Are you having a problem? Would you like to tell us what it is?
– SLaks
Jun 11 '13 at 14:49
Possible duplicate of ExecuteScalar always returns null when calling a scalar-valued function
– GSerg
Aug 23 at 12:45