C# 插入LocalDB Visual Studio 2012

C# 插入LocalDB Visual Studio 2012,c#,winforms,visual-studio,visual-studio-2012,sql-server-2012-localdb,C#,Winforms,Visual Studio,Visual Studio 2012,Sql Server 2012 Localdb,我有一个问题,我找不到解决方案,那就是在VisualStudioWindows窗体App-C中向LocalDB插入数据 实际上,我正在使用一个类库,并在我的Windows窗体应用程序中添加了对它的引用 问题是,当我想向本地数据库添加新数据时,它不会显示任何错误,但是,我没有看到任何数据插入到数据库中 这是我在类库中的代码,这是一种通用方法,可用于将任何数据添加到数据库中的任何表中 public virtual void Add(Item item) { int c

我有一个问题,我找不到解决方案,那就是在VisualStudioWindows窗体App-C中向LocalDB插入数据 实际上,我正在使用一个类库,并在我的Windows窗体应用程序中添加了对它的引用 问题是,当我想向本地数据库添加新数据时,它不会显示任何错误,但是,我没有看到任何数据插入到数据库中

这是我在类库中的代码,这是一种通用方法,可用于将任何数据添加到数据库中的任何表中

    public virtual void Add(Item item)
    {
        int count = 0;
        //create the first part of the Add SQL string
        string addString = "INSERT INTO " + table + "(";

        //add each field name from Item UpdateFields to the string
        foreach (KeyValuePair<string, string> field in item.UpdateFields)
        {
            addString += "[" + field.Key + "]";
            count++;
            //add a comma when end of UpdateFields collection is reached
            if (count < item.UpdateFields.Count)
            { addString += ", "; }

        }

        //start second part of Add string
        addString += ") VALUES(";
        count = 0;
        //add each value from Item UpdateFields to the string
        foreach (KeyValuePair<string, string> field in item.UpdateFields)
        {
            if (field.Value != null)
            { addString += "'" + field.Value.ToString() + "'"; }
            else
            { addString += "NULL"; }
            count++;
            //add a comma when end of UpdateFields collection is reached
            if (count < item.UpdateFields.Count)
            { addString += ", "; }
        }
        //add bracket at end of Add string
        addString += ")";

        command.CommandText = addString;

        try
        {
            command.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            item.Valid = false;
            item.ErrorMessage = ex.Message;
        }


    }
下面是我的字符串连接

new SqlConnection(Properties.Settings.Default.TestConnectionString);  

提前感谢

显示创建命令的代码部分。另外,请注意,必须使用参数而不是字符串连接,否则可能会出现SQL注入攻击命令=connection.CreateCommand;command.CommandText=从+表格中选择*;reader=command.ExecuteReader;这就是我使用命令的方式不,它不是。您正在显示选择数据的命令,并在插入中查找错误。没有准确的信息,谁也帮不上忙。另外,请包括您的连接字符串,而不是从中加载的属性。我已经复制了问题中的insert,关于连接,我是这样使用它的,因为当将其更改为路径时,发生了许多错误,并且使用这样的语句解决了这些错误
new SqlConnection(Properties.Settings.Default.TestConnectionString);