我有这个程序:
ALTER procedure dbo.scUsuarioCorrecto
@usrLogin nvarchar(15),
@usrPassword nvarchar(255)
AS
select case when exists
(select * from Usuario where usrLogin =
@usrLogin and usrPassword = @usrPassword
AND usrHabilitado = 1)
then CAST(1 as bit) else CAST(0 as bit) end as correcto;
我无法更改它,所以我必须解决它,我如何从 C# 中捕获该位,我已经连接到数据库并且我能够调用一些过程,但是我在这个过程中遇到了问题
尝试将参数设置为输出:
SqlParameter result = new SqlParameter("correcto", SqlDbType.Int)
{
Direction = ParameterDirection.Output
};
cmd.Parameters.Add(result);
但是我得到了“太多参数”错误,抱歉,如果我犯了一个愚蠢的错误,我今天真的开始使用 C#,我也使用了搜索按钮。
最佳答案
问题是,您正在执行存储过程并试图读取它,就好像有一个 Output parameter .如果您无法修改存储过程以提供输出参数,请将代码修改为如下所示:
using (var connection = new SqlConnection())
{
connection.ConnectionString = "Your Connection String";
using (var command = new SqlCommand())
{
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "scUsuarioCorrecto";
command.Parameters.AddWithValue("@usrLogin", "Your login");
command.Parameters.AddWithValue("@usrPassword", "Your password");
connection.Open();
using (var adapter = new SqlDataAdapter())
{
using (var ds = new DataSet())
{
adapter.SelectCommand = command;
adapter.Fill(ds);
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
var correcto = (bool)ds.Tables[0].Rows[0]["correcto"];
} else
{
// Something went wrong
}
}
}
}
}
关于c# - 从具有特定数据库的过程中获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30362990/