如何在sqlite c#中使用文本文件创建数据库表?

wn9m85ua  于 11个月前  发布在  SQLite
关注(0)|答案(1)|浏览(137)

我想用c#创建sqlite数据库的表。但是,我想使用一个文本文件来存储将在系统中创建的模型名称。我如何使用c#建立它?
我已经成功地读取了文件内容。但是,在使用字符串创建表时遇到了问题。

Customer
CustomerAgent
CustomerAddress

字符串
一个类的例子:

Table["Customer"]
public class Customer
{
    [AutoIncrement, PrimaryKey, Unique] public int UniqueId { get; set; }
    public int AccountNumber { get; set; }
}


这是我如何尝试从文本文件中读取类名:

try
{
    string filePath = "models.txt";

    string[] modelNames = File.ReadAllLines(filePath);

    foreach (string modelName in modelNames)
    {
        Console.WriteLine($"Creating table for model: {modelName}");
        // Here I have to create table in database normally I do like this: SqliteConnection.CreateTable<ClassName>();
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Error: {ex.Message}");
}

acruukt9

acruukt91#

可以使用Reflection

foreach (string modelName in modelNames)
{
    Console.WriteLine($"Creating table for model: {modelName}");
    // You may add necessary controls etc.
    SqliteConnection.CreateTable<Type.GetType(modelName)>();
}

字符串

相关问题