如何将数据库中的值添加到统一列表中

时间:2017-01-12 06:08:18

标签: c# sql unity3d

  • 我有一个数据库,我在其中创建了一个表 " CampWheelDenominationno",我有一个特定的行" quota"

  • 我需要选择" slno"相应的"配额"什么时候"配额" 变为零。

  • 我的SQL代码是

    "select slno from CampWheelDenomination where quota_allowded = 0"
    
  • 现在我需要添加这些" slno"到列表..我已经做了一个我自己的方法但是没有用

    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 ();
                }
            }
        }
    }
    

如何将这些slno从数据库添加到SlNOList

错误是无法隐式转换类型System.Collections.Generic.List<string>' to int&#39;在行返回时发生SlNoList

2 个答案:

答案 0 :(得分:0)

您正在尝试返回List但功能返回类型为int。按如下方式更改功能定义:

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

另外看一下你的代码,你可能应该在你的函数中有一个List并返回它。

示例:

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;
}

答案 1 :(得分: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 ();


                }
            }
        }
    }