C#에서 다음 저장 프로 시저를 읽으려고합니다.mysql 쿼리가 항상 부울 대신 int 값을 반환하는 이유는 무엇입니까?
DECLARE IsTrue boolean;
DECLARE IsFalse boolean;
set IsTrue = true;
set IsFalse = false;
SELECT
stuff.ID1
,stuff.ID2
,stuff.ID3
,stuff.ID4
,CASE
WHEN stuff1.ID1 IS NOT NULL THEN IsTrue
WHEN stuff1.ID1 IS NULL THEN IsFalse
END AS 'stuff1Column'
,CASE
WHEN stuff2.ID1 IS NOT NULL THEN IsTrue
WHEN stuff2.ID1 IS NULL THEN IsFalse
END AS 'stuff2Column'
FROM myStuff.stuff
LEFT JOIN myStuff.stuff1 ON stuff.ID1 = myStuff.stuff1.ID1
LEFT JOIN myStuff.stuff2 ON stuff2.ID1 = myStuff.stuff2.ID1
ORDER BY stuff.ID1 DESC;
기본적으로 C#에서는 다음 예외가 발생합니다. 나는 그것이 Int
대신에 저를주고있다 부울 값을 반환 지정하고 있지만
Object of type 'System.Int32' cannot be converted to type 'System.Boolean'.
. 나는 또한 이것을 사용하여 tinyint (1)을 시도했으나 여전히 작동하지 않았다. 내 클래스는 그 속성이 무엇 때문에 부울 값으로 stuff1Column
및 stuff2Column
읽을려고 1
public class Stuff {
private int _ID1;
private int _ID2;
private int _ID3;
private int _ID4;
private bool _stuff1Column;
private bool _stuff2Column;
public int ID1 {
get { return _ID1; }
set { _ID1 = value; }
}
public int ID2 {
get { return _ID2; }
set { _ID2 = value; }
}
public int ID3 {
get { return _ID3; }
set { _ID3 = value; }
}
public int ID4 {
get { return _ID4; }
set { _ID4 = value; }
}
public bool Stuff1Column {
get { return _stuff1Column; }
set { _stuff1Column = value; }
}
public bool Stuff2Column {
get { return _stuff2Column; }
set { _stuff2Column = value; }
}
}
편집 : 여기
는 클래스입니다. 편집 2은 그것을 읽는 C# 코드이다.
public static List<T> Read<T>(T data, string procedure) { List<T> collection = new List<T>(); PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); List<PropertyInfo> propertiesToSearch = new List<PropertyInfo>(); foreach (PropertyInfo prop in properties) { if (prop.GetCustomAttributes(false).Any(x => x.GetType() == typeof(DataParameter))) propertiesToSearch.Add(prop); } MySqlConnection connection = new MySqlConnection(Properties.Settings.Default.MySqlConnection); MySqlCommand command = new MySqlCommand(procedure, connection); command.CommandType = CommandType.StoredProcedure; foreach (PropertyInfo property in propertiesToSearch) { var parameterName = "@" + property.Name; var value = property.GetValue(data, null); command.Parameters.AddWithValue(parameterName, value); } connection.Open(); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { T item = Activator.CreateInstance<T>(); foreach (PropertyInfo property in propertiesToSearch) { if (reader[property.Name] is MySql.Data.Types.MySqlDateTime) { property.SetValue(item, (DateTime)(MySql.Data.Types.MySqlDateTime)reader[property.Name], null); } else { Type test = reader[property.Name].GetType(); property.SetValue(item, reader[property.Name], null); } } collection.Add(item); } reader.Close(); connection.Close(); return collection; }
가 EDIT가 3
I는 TINYINT을 가진 다른 테이블 (1)의 값
을 저장 한 MySqlDataReader는이를 자동으로 부울 값으로 해석합니다. 그래서 나는 이것이 저장된 프로 시저와 실제로 저장된 값이 아니라는 사실과 관련이 있다고 생각합니다.
C# 코드를 공유 할 수 있습니까? –
그것은 문제를 해결하지 않을하지만 당신은'사용할 수 있습니다 CASE – cheesemacfly