[英]Failed to convert string to Int32
我是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 ;
}
}
這是我存儲的過程代碼:
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中返回無列名的我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
沒有足夠的聲譽來發表評論。
嘗試將代碼更改為以下形式:
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 ;
}
}
如果您進行調試,則可以更清楚地了解為什么該值不是有效的整數(可能是長整數或其他形式)
ExecuteScalar返回第一個結果集中第一行的第一列。 https://dev.mysql.com/doc/dev/connector-net/8.0/html/M_MySql_Data_MySqlClient_MySqlCommand_ExecuteScalar.htm
如果存儲的過程中沒有其他內容,則它應該可以工作。 但是我看到您在存儲的過程中有注釋“-在此處插入過程的語句”。 那么這些可能會引起問題嗎?
您在SSMS中執行存儲的proc的方式將返回與您期望的.Net代碼不同的內容。 我期望看到沒有列名返回的1(即您的選擇計數(*)...),而返回了0的名字返回值。
最后,如果存儲的過程完全按照您發布的內容進行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.