Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建动态sqlite表创建和插入函数_C#_Android_Sql_Sqlite_Xamarin - Fatal编程技术网

C# 创建动态sqlite表创建和插入函数

C# 创建动态sqlite表创建和插入函数,c#,android,sql,sqlite,xamarin,C#,Android,Sql,Sqlite,Xamarin,我有大约8-9个函数用于填充从SQL到Sqlite的表,我正在尝试创建一个动态函数,在这个函数上我将只传递一些参数,它将创建Sqlite表(如果它不存在),在一个循环中创建多个我想要插入到特定表中的类型的实例,设置它的属性,然后插入它。以下是示例函数: private bool ReloadItemsFromServer() { try { using (GS.cnn) { S

我有大约8-9个函数用于填充从SQL到Sqlite的表,我正在尝试创建一个动态函数,在这个函数上我将只传递一些参数,它将创建Sqlite表(如果它不存在),在一个循环中创建多个我想要插入到特定表中的类型的实例,设置它的属性,然后插入它。以下是示例函数:

private bool ReloadItemsFromServer()
    {
        try
        {
            using (GS.cnn)
            {
                SqlCommand command = new SqlCommand("rsp_FillItem_MobileDevice;", GS.cnn);
                command.CommandType = System.Data.CommandType.StoredProcedure;
                GS.cnn.Open();
                SqlDataReader reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "DataBase.PremierAndroid");
                    using (var sqliteConn = new SQLiteConnection(dbPath))
                    {
                        sqliteConn.CreateTable<Item>();

                        while (reader.Read())
                        {
                            {
                                var newItem = new Item();
                                newItem.ItemID = reader.GetInt32(0);
                                newItem.ItemBaseID = reader.GetInt32(1);
                                newItem.BrandID = reader.GetInt32(2);
                                newItem.Issue = reader.GetInt32(3);

                                sqliteConn.Insert(newItem);
                            }
                        }
                    }
                }
                else
                {
                    _dlgAlert = new AlertDialog.Builder(this).Create();
                    _dlgAlert.SetMessage(Resources.GetString(Resource.String.NoRowsForItemFound));
                    _dlgAlert.SetTitle(Resources.GetString(Resource.String.Error));
                    _dlgAlert.SetButton("OK", delegate { });
                    _dlgAlert.Show();
                    return false;
                }
                reader.Close();
            }
            return true;
        }
        catch (Exception ex)
        {
            _dlgAlert = new AlertDialog.Builder(this).Create();
            _dlgAlert.SetMessage(ex.Message);
            _dlgAlert.SetTitle(Resources.GetString(Resource.String.Error));
            _dlgAlert.SetButton("OK", delegate { });
            _dlgAlert.Show();
            return false;
        }
        finally
        {
            if (GS.cnn.State != ConnectionState.Closed)
            {
                GS.cnn.Close();
            }
        }
    }

感谢您

让反射和继承这样的帮助:

class Caller{

    // see how it sends objects to method 'processAllClasses' with different classes
    void main(){    
        ItemClass1 i1 = new ItemClass1();
        i1.setItemsB(1,2);
        processAllClasses(i1);

        ItemClass2 i2 = new ItemClass2();
        i2.setItemsB(10, 20);
        processAllClasses(i2);
    }

    //using reflection and inherit to process different classes, obj is what need to be processed on.
    //alternative way:
    //get members from the type, then assign values to them one by one
    void processAllClasses(ParentClass myobjvalue)
    { 
        Type realtype = myobjvalue.GetType();
        ParentClass myobj = new ParentClass(); 
        MethodInfo mi = realtype.GetMethod("setItemsA");
        object obj = Activator.CreateInstance(realtype);
        Object[] parameter = new Object[] { myobjvalue };
        mi.Invoke(obj, parameter); 
    }
}



class ParentClass {

}


class ItemClass1: ParentClass
{
    int ItemID;
    int ItemBaseID;
    public void setItemsA(ItemClass1 itemclass1)
    {
        this.ItemID = itemclass1.ItemID;
        this.ItemBaseID = itemclass1.ItemBaseID;
    }
    public void setItemsB(int ItemID, int ItemBaseID)
    {
        this.ItemID = ItemID;
        this.ItemBaseID = ItemBaseID;
    }

}

class ItemClass2 : ParentClass
{
    int ContragentID;
    int ContragentTypeID;
    public void setItemsA(ItemClass2 itemclass2)
    {
        this.ContragentID = itemclass2.ContragentID;
        this.ContragentTypeID = itemclass2.ContragentTypeID;
    }
    public void setItemsB(int ContragentID, int ContragentTypeID)
    {
        this.ContragentID = ContragentID;
        this.ContragentTypeID = ContragentTypeID;
    }

}
private bool LoadDataFromServer(string procedure, Type passedType)
    {
        try
        {
            using (GS.cnn)
            {
                SqlCommand command = new SqlCommand(procedure, GS.cnn);
                command.CommandType = System.Data.CommandType.StoredProcedure;
                GS.cnn.Open();
                SqlDataReader reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "DataBase.PremierAndroid");
                    using (var sqliteConn = new SQLiteConnection(dbPath))
                    {
                        sqliteConn.CreateTable(passedType);

                        while (reader.Read()) //to do > i++ bla bla
                        {
                            {
                                var newItem = Activator.CreateInstance(passedType);

                                //newItem - loop through properties and set = reader.get(int/string)(i)

                                sqliteConn.Insert(newItem);
                            }
                        }
                    }
                }
                else
                {
                    _dlgAlert = new AlertDialog.Builder(this).Create();
                    _dlgAlert.SetMessage(Resources.GetString(Resource.String.NoRowsForItemFound));
                    _dlgAlert.SetTitle(Resources.GetString(Resource.String.Error));
                    _dlgAlert.SetButton("OK", delegate { });
                    _dlgAlert.Show();
                    return false;
                }
                reader.Close();
            }
            return true;
        }
        catch (Exception ex)
        {
            _dlgAlert = new AlertDialog.Builder(this).Create();
            _dlgAlert.SetMessage(ex.Message);
            _dlgAlert.SetTitle(Resources.GetString(Resource.String.Error));
            _dlgAlert.SetButton("OK", delegate { });
            _dlgAlert.Show();
            return false;
        }
        finally
        {
            if (GS.cnn.State != ConnectionState.Closed)
            {
                GS.cnn.Close();
            }
        }
    }
class Caller{

    // see how it sends objects to method 'processAllClasses' with different classes
    void main(){    
        ItemClass1 i1 = new ItemClass1();
        i1.setItemsB(1,2);
        processAllClasses(i1);

        ItemClass2 i2 = new ItemClass2();
        i2.setItemsB(10, 20);
        processAllClasses(i2);
    }

    //using reflection and inherit to process different classes, obj is what need to be processed on.
    //alternative way:
    //get members from the type, then assign values to them one by one
    void processAllClasses(ParentClass myobjvalue)
    { 
        Type realtype = myobjvalue.GetType();
        ParentClass myobj = new ParentClass(); 
        MethodInfo mi = realtype.GetMethod("setItemsA");
        object obj = Activator.CreateInstance(realtype);
        Object[] parameter = new Object[] { myobjvalue };
        mi.Invoke(obj, parameter); 
    }
}



class ParentClass {

}


class ItemClass1: ParentClass
{
    int ItemID;
    int ItemBaseID;
    public void setItemsA(ItemClass1 itemclass1)
    {
        this.ItemID = itemclass1.ItemID;
        this.ItemBaseID = itemclass1.ItemBaseID;
    }
    public void setItemsB(int ItemID, int ItemBaseID)
    {
        this.ItemID = ItemID;
        this.ItemBaseID = ItemBaseID;
    }

}

class ItemClass2 : ParentClass
{
    int ContragentID;
    int ContragentTypeID;
    public void setItemsA(ItemClass2 itemclass2)
    {
        this.ContragentID = itemclass2.ContragentID;
        this.ContragentTypeID = itemclass2.ContragentTypeID;
    }
    public void setItemsB(int ContragentID, int ContragentTypeID)
    {
        this.ContragentID = ContragentID;
        this.ContragentTypeID = ContragentTypeID;
    }

}