This question is locked. New answers and comments are not allowed.
hi.
sometimes I need to return multiple and different value from a Store Procedure. For example I want to insert a user to database , before doing this I have to check the Username . I do this :
ALTER PROCEDURE [dbo].[UserInsert] ( @UserName nvarchar(255) ,@UserPassword nvarchar(255) )
AS
BEGIN
IF ( (SELECT Count(UserId) from Users WHERE UserName = @UserName )= 0)
BEGIN
INSERT INTO Users ( UserName , UserFamily , UserPassword ,UserLoginName,
UserMobile,UserEmail,UserCredit, UserRole)
VALUES
( @UserName , @UserFamily , @UserPassword,@UserLoginName,
@UserMobile,@UserEmail,@UserCredit,'User')
RETURN 1;
END
ELSE RETURN -2;
END
--------------------------------------------
I change your ExecuteNonQuery code to can return specific value from my StoreProcedure . I copy method to EntitiesModel.cs .
this is my code :
private OAConnection GetConnection(out bool isNew)
{
IExtendedObjectScope scope = ((IExtendedObjectScope)GetScope());
OAConnection connection = scope.GetConnection(out isNew);
return connection;
}
public int ExecuteNonQuery2(string commandText, System.Data.CommandType commandType, params System.Data.Common.DbParameter[] parameters)
{
bool isNew = true;
OAConnection connection = null;
try
{
connection = GetConnection(out isNew);
if (connection.State != System.Data.ConnectionState.Open)
{
throw new InvalidOperationException("You need an open connection to perform the requested operation");
}
using (OACommand command = connection.CreateCommand())
{
command.CommandText = commandText;
command.CommandType = commandType;
if (parameters != null && parameters.Length != 0)
{
command.Parameters.AddRange(parameters);
}
OAParameter returnValue = new OAParameter("returnVal", SqlDbType.Int);
returnValue.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnValue);
command.ExecuteNonQuery();
command.Parameters.Clear();
return Convert.ToInt32(returnValue.Value);
}
}
finally
{
if (isNew && connection != null)
connection.Dispose();
}
}
but I have some Problem.
each time I rebuild the Entities all my code cleared and I have to paste it again and ...
I don't know if there are other way for doing this.
if there is please help me .
thanks a lot .
sometimes I need to return multiple and different value from a Store Procedure. For example I want to insert a user to database , before doing this I have to check the Username . I do this :
ALTER PROCEDURE [dbo].[UserInsert] ( @UserName nvarchar(255) ,@UserPassword nvarchar(255) )
AS
BEGIN
IF ( (SELECT Count(UserId) from Users WHERE UserName = @UserName )= 0)
BEGIN
INSERT INTO Users ( UserName , UserFamily , UserPassword ,UserLoginName,
UserMobile,UserEmail,UserCredit, UserRole)
VALUES
( @UserName , @UserFamily , @UserPassword,@UserLoginName,
@UserMobile,@UserEmail,@UserCredit,'User')
RETURN 1;
END
ELSE RETURN -2;
END
--------------------------------------------
I change your ExecuteNonQuery code to can return specific value from my StoreProcedure . I copy method to EntitiesModel.cs .
this is my code :
private OAConnection GetConnection(out bool isNew)
{
IExtendedObjectScope scope = ((IExtendedObjectScope)GetScope());
OAConnection connection = scope.GetConnection(out isNew);
return connection;
}
public int ExecuteNonQuery2(string commandText, System.Data.CommandType commandType, params System.Data.Common.DbParameter[] parameters)
{
bool isNew = true;
OAConnection connection = null;
try
{
connection = GetConnection(out isNew);
if (connection.State != System.Data.ConnectionState.Open)
{
throw new InvalidOperationException("You need an open connection to perform the requested operation");
}
using (OACommand command = connection.CreateCommand())
{
command.CommandText = commandText;
command.CommandType = commandType;
if (parameters != null && parameters.Length != 0)
{
command.Parameters.AddRange(parameters);
}
OAParameter returnValue = new OAParameter("returnVal", SqlDbType.Int);
returnValue.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnValue);
command.ExecuteNonQuery();
command.Parameters.Clear();
return Convert.ToInt32(returnValue.Value);
}
}
finally
{
if (isNew && connection != null)
connection.Dispose();
}
}
but I have some Problem.
each time I rebuild the Entities all my code cleared and I have to paste it again and ...
I don't know if there are other way for doing this.
if there is please help me .
thanks a lot .