2017-01-12 57 views
1
  • 我有我創建了一個表 「CampWheelDenominationno」,我有一個特定行「配額」如何從數據庫中統一添加值列表

  • 我需要數據的基礎上當「配額」 變爲零時,相應「配額」的「slno」。

  • 我的SQL代碼,這樣做是

    "select slno from CampWheelDenomination where quota_allowded = 0" 
    
  • 現在我需要添加這些「slno」到list..i已經做的方法我自己,但t不與工作

    public int checkCodeinDb() 
    { 
    
    
        using (IDbConnection dbConnection = new SqliteConnection (connectionString)) 
        { 
         dbConnection.Open(); 
         using (IDbCommand dbCmd = dbConnection.CreateCommand()) 
         { 
          string sqlQuery = "select slno from CampWheelDenomination where quota_allowded = 0"; 
          dbCmd.CommandText = sqlQuery; 
          using (IDataReader reader = dbCmd.ExecuteReader()) 
          { 
           while (reader.Read()) 
           { 
            //slnolist is a list i have created at begining 
           SlNoList.Add (reader.GetString (0)); 
           } 
    
           return slnolist ; //error at here 
    
          foreach (string st in SlNoList)// i have done this to check whether the slno is added to the list. 
          { 
           print (st); 
          } 
    
    
           dbConnection.Close(); 
           reader.Close(); 
          } 
         } 
        } 
    } 
    

如何從DATABSE添加這些slno到SlNOList

錯誤是不能隱轉換類型System.Collections.Generic.List<string>' to int'發生在線返回SlNoList

+1

什麼是錯誤? –

+0

如果你正在定義'SlNoList',你是否實例化了這個變量? –

+0

slnolist是我之前創建的一個列表 –

回答

0

您試圖嘗試返回List但您的函數返回類型爲int。改變你的函數定義如下:

public List<string> checkCodeinDb() 
{ 
///your usual code here 

也期待你的代碼,你可能應該有你的函數內部列表和返回。

例子:

List<string> slnolist = checkCodeinDb(); 

public List<string> checkCodeinDb() 
{ 
List<string> data; 
///your usual code here and add your database returned value to data variable and finally return it 
return data; 
} 
+0

使用'slnolist.addAll(checkCodeinDb());'當列表已經實例化時更安全嗎?只是要求個人學習:) – Nico

0

,並從數據庫中添加值列表,我們可以使用此代碼

public void checkCodeinDb() 
{ 


     using (IDbConnection dbConnection = new SqliteConnection (connectionString)) 
     { 
      dbConnection.Open(); 
      using (IDbCommand dbCmd = dbConnection.CreateCommand()) 
      { 
       string sqlQuery = "select slno from CampWheelDenomination where quota_allowded = 0"; 
       dbCmd.CommandText = sqlQuery; 
       using (IDataReader reader = dbCmd.ExecuteReader()) 
       { 
        while (reader.Read()) 
        { 




        string ad = reader ["slno"].ToString(); 

        SlNoList.Add (ad); 

        } 






       foreach (string st in SlNoList) 
       { 
        print (st); 
       }  





        dbConnection.Close(); 
        reader.Close(); 


       } 
      } 
     } 
    } 
+0

這適用於我100% –

相關問題