배경 : ASP.NET 응용 프로그램을 다시 작성하고 있습니다. 현재 MS SQL 데이터베이스를 사용하고 있지만 동시에 MySQL도 지원해야합니다.MySqlCommand.ExecuteNonQuery()가 MySqlParameter를 채우지 않음
대부분의 작업이 완료되었습니다. - MySQL Connector/Net을 사용하여 C# (Visual Studio 2010)에서 MySQL 데이터베이스 작업을 할 수 있지만 특정 SQL 쿼리에 문제가 있습니다. 여기
"SELECT @username=username FROM t_Users WHERE [email protected]"
내 코드입니다 : 이것은 내가 실행해야 SQL 쿼리입니다
public static int ExecuteSqlNonQuery(string i_szQuery)
{
bool bConnectionWasOpen = false;
AbstractConnection dbConnection = CMSSQLInstanceCreator.CreateConnection();
if(dbConnection.IsMySQL())
{
MySqlConnection aSqlConnection = dbConnection.GetMysConnection();
try
{
if (aSqlConnection.State == System.Data.ConnectionState.Closed)
aSqlConnection.Open();
else
if (aSqlConnection.State == System.Data.ConnectionState.Open)
bConnectionWasOpen = true;
else
throw new ApplicationException("Connection not available!");
List<MySqlParameter> aParameters1 = new List<MySqlParameter>();
aParameters1.Add(new MySqlParameter("@username", MySqlDbType.VarString, 128));
aParameters1.Add(new MySqlParameter("@id", MySqlDbType.Int32));
aParameters1[0].Direction = System.Data.ParameterDirection.Output;
aParameters1[1].Direction = System.Data.ParameterDirection.Input;
aParameters1[1].Value = (int)1;
MySqlCommand aSqlCommand = new MySqlCommand(i_szQuery, aSqlConnection);
aSqlCommand.CommandType = System.Data.CommandType.Text;
aSqlCommand.CommandTimeout = 0;
aSqlCommand.Parameters.Clear();
aSqlCommand.Parameters.AddRange(aParameters1.ToArray());
int iResult = aSqlCommand.ExecuteNonQuery();
if(iResult <= 0)
Debug.WriteLine("aResult <= 0: " + i_szQuery);
return iResult;
}
catch (Exception ex)
{
throw new ApplicationException("Cannot execute query!\nReason: '" + ex.Message + "'", ex);
}
finally
{
if (!bConnectionWasOpen && aSqlConnection.State == System.Data.ConnectionState.Open)
aSqlConnection.Close();
}
}
else
{
//... almost the same for MS SQL
}
}
문제는 출력 매개 변수가 @username (System.Data.ParameterDirection.Output) 쿼리의 값으로 채워지지 않습니다.
다른 검색어 (예 :
UPDATE t_Users SET [email protected]_password WHERE ([email protected]_password AND [email protected])
모든 것이 정상입니다. 거기에 많은 값을 반환
SELECT @username=username, @is_blocked=is_blocked, @full_name=full_name, @must_change_pwd=must_change_pwd ...
같은 많은 다른 쿼리가 나는 대부분의 코드를 다시 작성하지 않는 한
나는 스칼라를 반환 할 수 없습니다.
같은 코드가 MS SQL과 잘 동작하기 때문에 MySQL에만 문제가있는 것처럼 보입니다.
MySqlParameter가 쿼리에서 값을로드하게하려면 어떻게해야합니까?