2013-01-11 26 views
1

我想從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#我拋出以下異常。

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

我的階級是試圖在stuff1Columnstuff2Column改爲布爾值,因爲這是性質是什麼。

編輯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

我已經(1)存儲在具有一個TINYINT另一個表值MySqlDataReader會自動將其解釋爲一個布爾值。所以我相信這是一個存儲過程,它可能不是一個實際的存儲值?

+1

你能分享你的C#代碼嗎? –

+0

它不會解決您的問題,但你可以用'CASE WHEN stuff1.ID1 IS NOT NULL THEN IsTrue運算 ELSE IsFalse END而是採用2'AS「stuff1Column'' WHERE' – cheesemacfly

回答

2

我錯了,在MySQL中沒有C#布爾類型的等價物,所以您只能使用NUMERIC類型並在C#中將返回值計算爲ntegers。

因此,請保持BOOLEAN類型並修改您的C#代碼以評估返回的BOOLEAN值(所以0代表假,1代表真)。

+0

我嘗試切換到BIT和其然後給我以下錯誤。 'System.UInt32'類型的對象不能轉換爲'System.Boolean'類型。' – meanbunny

+0

@meanbunny我們可以看到你的C#代碼嗎? – cheesemacfly

+0

我添加了上面的代碼 – meanbunny

相關問題