我是asp.net开发人员。我正在执行存储过程,并使用ExecuteScaler方法获取列数读取。但没有成功,它给出了如下类型转换错误:
我有此代码:
protected int isValidUser()
{
try
{
return Convert.ToInt32(Mysql.ExecuteScalar("Support_UserLogin", AppGlobal.UserName, AppGlobal.Password));
}
catch (Exception ex)
{
AppGlobal.QLog.Enqueue(new clsLogType() { Message = System.Reflection.MethodBase.GetCurrentMethod().Name, ex = ex, logType = LogType.Error });
return 0 ;
}
}
这是我存储的proc代码:
USE [NEWBULKSMS]
GO
/****** Object: StoredProcedure [dbo].[Support_UserLogin] Script Date: 01/03/2019 2:12:10 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Support_UserLogin]
@Username varchar(100),
@Password varchar(100)
AS
BEGIN
BEGIN TRY
SET NOCOUNT ON;
-- Insert statements for procedure here
select count(*) from users where strUserName = @Username and strPassword = @password;
END TRY
BEGIN CATCH
EXEC [dbo].[usp_LogErrorHistory];
DECLARE @err VARCHAR(MAX)
SELECT @err = ERROR_MESSAGE()
RAISERROR('Error while getting : [dbo].[Support_UserLogin]', 16, 1, @err)
END CATCH
END
完成此操作后,我得到类似Failed to convert string to Int32
的错误
这怎么了......
---------------------------------已更新---------- ---------------------------
在SSMS中运行此存储的proc时,这将在无列名的情况下返回1。
USE [NEWBULKSMS]
GO
DECLARE @return_value int
EXEC @return_value = [dbo].[Support_UserLogin]
@Username = N'shalingajjar',
@Password = N'shalsoft$3433'
SELECT 'Return Value' = @return_value
GO
------------------------------------已更新------- -----------------------------
这里我包含ExecuteScaler代码的更多信息
#region ExecuteScalar
public static object ExecuteScalar(CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
using (SqlConnection cn = new SqlConnection(ConString))
{
cn.Open();
return ExecuteScalar(cn, commandType, commandText, commandParameters);
}
}
public static object ExecuteScalar(string spName, params object[] parameterValues)
{
if ((parameterValues != null) && (parameterValues.Length > 0))
{
SqlParameter[] commandParameters = GetSpParameterSet(spName);
AssignParameterValues(commandParameters, parameterValues);
return ExecuteScalar(CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return ExecuteScalar(CommandType.StoredProcedure, spName);
}
}
public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters);
object retval = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return retval;
}
private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters)
{
if(connection.State != ConnectionState.Open)
{
connection.Open();
}
command.Connection = connection;
command.CommandText = commandText;
if (transaction != null)
{
command.Transaction = transaction;
}
command.CommandType = commandType;
if (commandParameters != null)
{
AttachParameters(command, commandParameters);
}
return;
}
private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters)
{
foreach (SqlParameter p in commandParameters)
{
if ((p.Direction == ParameterDirection.InputOutput) && (p.Value == null))
{
p.Value = DBNull.Value;
}
command.Parameters.Add(p);
}
}
private static void AssignParameterValues(SqlParameter[] commandParameters, object[] parameterValues)
{
if ((commandParameters == null) || (parameterValues == null))
{
return;
}
if (commandParameters.Length < parameterValues.Length)
{
throw new ArgumentException("Parameter count does not match Parameter Value count.");
}
for (int i = 0, j = commandParameters.Length; i < j; i++)
{
if (i < parameterValues.Length)
{
if (commandParameters[i].DbType.ToString() == "String")
commandParameters[i].Value = (parameterValues[i] == null ? "" : parameterValues[i]);
else
commandParameters[i].Value = parameterValues[i];
}
else
{
break;
}
}
}
#endregion
答案 0 :(得分:1)
没有足够的声誉来发表评论。
尝试将您的代码更改为以下内容:
protected int isValidUser()
{
try
{
var returnedValue = Mysql.ExecuteScalar("Support_UserLogin", AppGlobal.UserName, AppGlobal.Password);
Console.Writeline(returnedValue);//this line is to view what was actually returned.
return Convert.ToInt32(returnedValue );
}
catch (Exception ex)
{
AppGlobal.QLog.Enqueue(new clsLogType() { Message = System.Reflection.MethodBase.GetCurrentMethod().Name, ex = ex, logType = LogType.Error });
return 0 ;
}
}
如果您进行调试,则可以更清楚地了解为什么该值不是有效整数(可能是长整数或其他)
答案 1 :(得分:1)
ExecuteScalar返回第一个结果集中第一行的第一列。 https://dev.mysql.com/doc/dev/connector-net/8.0/html/M_MySql_Data_MySqlClient_MySqlCommand_ExecuteScalar.htm
如果存储的proc中没有其他内容,它应该可以工作。但是我看到您在存储的过程中有注释“-在此处插入过程的语句”。那么这些可能会引起问题吗?
在SSMS中执行存储的proc的方式将返回与您期望的.Net代码不同的内容。我本来希望看到没有列名返回的1(即您的选择计数(*)...),而有返回值0的名字。
最后,如果存储的proc确实在执行您发布的操作