我正在尝试从C#中读取以下存储过程。
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#中,我抛出以下异常。
Object of type 'System.Int32' cannot be converted to type 'System.Boolean'.即使我指定返回一个布尔值,它还是给了我一个Int。我也尝试过使用tinyint(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; }
}
}编辑1
我的类正在尝试以bool值的形式读取stuff1Column和stuff2Column,因为属性就是这样的。
编辑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;
}编辑3
我有另一个表,它存储了一个tinyint(1)值,MySqlDataReader自动将其解释为一个布尔值。所以,我认为这与它是一个存储过程有关,而事实上它不是一个实际的存储值,可能吗?
发布于 2013-01-11 17:01:13
我错了,在MySQL中没有C#的布尔类型,所以您只能使用数字类型并在C#中将返回的值计算为ntegers。
因此,保持布尔类型并修改C#代码,以求返回的布尔值( False为0,True为1)。
https://stackoverflow.com/questions/14282980
复制相似问题