分号的奇怪错误

Weird error with semicolon

我有一段来自我的 类 的代码,当我将鼠标悬停在“//错误在这里”“} 预期”旁边的分号上时,但所有括号都已关闭,我没有知道为什么会造成这种情况,我尝试重建但没有任何变化

using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SQLite.Net;
using SQLite.Net.Platform.WinRT;

    namespace HomeAutomation
    {
        public class MainCode
        {
            static string path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");
            static SQLiteConnection conn = new SQLiteConnection(new SQLitePlatformWinRT(), path)
            {
            conn.CreateTable<User>;//ERROR IS HERE
            }
        }

        public class User
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    }

这个conn.CreateTable<User>;//ERROR IS HERE应该是

conn.CreateTable<User>();//solved

你忘了括号

您正在使用 object initiationalization。你不要在其中使用分号 b/c 分号表示命令的终止点。不允许在初始化过程中结束语句。您将用逗号分隔要初始化的每个字段,然后在最后一个花括号后结束语句。

编辑

再次查看代码后,您似乎不应该使用对象初始化。此语法用于在对象上初始化 属性。您需要将这两个语句分开。即

static SQLiteConnection conn = new SQLiteConnection(new SQLitePlatformWinRT(), path); // End initialization statement
static MainCode()
{
   conn.CreateTable<User>;//Initialize in static constructor
}

不确定这是否有帮助,但也许可以尝试:

conn.CreateTable<User>("SELECT somethingHere FROM somewhere");

认为 你在本应使用 using

时使用关键字 static 做了什么

看看您要实现的目标,我建议您的代码可能需要类似于:

using SQLiteConnection conn = new SQLiteConnection(new SQLitePlatformWinRT(), path) { conn.CreateTable<User>();//ERROR IS HERE }

我改变了什么? 我已将单词 static 更改为 using 并将括号添加到 conn.CreateTable<User>()

的末尾

希望对您有所帮助!