Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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#_Sqlite - Fatal编程技术网

C# 索引超出范围。必须为非负数且小于集合的大小。参数名称:索引SQLite

C# 索引超出范围。必须为非负数且小于集合的大小。参数名称:索引SQLite,c#,sqlite,C#,Sqlite,当前,我在尝试构建SQL表时遇到一个错误。上面的标题“索引超出范围。必须为非负且小于集合大小”中说明了该错误。 参数名称:index“我不明白为什么它会抛出这个,但是代码本身似乎还可以 static void Main(string[] args) { ServiceReference1.ControlPumpsClient WCFSQL = new SQLserver.ServiceReference1.ControlPumpsClient(); str

当前,我在尝试构建SQL表时遇到一个错误。上面的标题“索引超出范围。必须为非负且小于集合大小”中说明了该错误。 参数名称:index“我不明白为什么它会抛出这个,但是代码本身似乎还可以

static void Main(string[] args)
    {
        ServiceReference1.ControlPumpsClient WCFSQL = new SQLserver.ServiceReference1.ControlPumpsClient();
        string[] SQLData = new string[6];
        while (WCFSQL.getData()[0] == null) { }

        if (WCFSQL.getData()[0] != null)
        {
            SQLData = WCFSQL.getData();
        }


        SQLiteConnection myConnection = new SQLiteConnection("Data source=test.db; Version=3;");
        myConnection.Open();
        /// If this is not the first time the program has run, 
        /// the table 'cars' will already exist, so we will remove it
        SQLiteCommand tableDropCommand = myConnection.CreateCommand();
        tableDropCommand.CommandText = "drop table Records";
        try
        {
            tableDropCommand.ExecuteNonQuery();
        }
        catch (SQLiteException ex) // We expect this if the table is not present
        {
            Console.WriteLine("Table 'Records' does  not exist");
        }

        /// Now create a table called 'records'
        SQLiteCommand tableCreateCommand = myConnection.CreateCommand();
        tableCreateCommand.CommandText = "create table Records (ID int, FuelType varchar(10), Price float, TankVolume int)";
        tableCreateCommand.ExecuteNonQuery();

        /// Now insert some data.
        /// First, create a generalised insert command
        SQLiteCommand insertCommand = myConnection.CreateCommand();
        insertCommand.CommandText = "insert into Records (ID, FuelType, Price, TankVolumes) values (@id, @fueltype, @price, @volume)";
        insertCommand.Parameters.Add(new SQLiteParameter("@id"));
        insertCommand.Parameters.Add(new SQLiteParameter("@fueltype"));
        insertCommand.Parameters.Add(new SQLiteParameter("@price"));
        insertCommand.Parameters.Add(new SQLiteParameter("@tankvolume"));
        addSQL(insertCommand, int.Parse(SQLData[0]), SQLData[1], float.Parse(SQLData[2]), double.Parse(SQLData[3]));

        /// Now, create a comand to read from the database;
        SQLiteCommand listAllRecordsCommand = myConnection.CreateCommand();
        listAllRecordsCommand.CommandText = "select * from Records";
        SQLiteDataReader reader = listAllRecordsCommand.ExecuteReader();

        /// Iterate over the results and print them
        while (reader.Read())
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(reader["ID"] + ",");
            sb.Append(reader["FuelType"] + ",");
            sb.Append(reader["Price"] + ",");
            sb.Append(reader["Volumes"] + ",");
            Console.WriteLine(sb.ToString());
        }

        myConnection.Close();

        /// Wait for a keypress
        Console.ReadLine();
    }

    public static void addSQL(SQLiteCommand insertCommand, int pumpID, string fuel, float price, double volume){
         /// Now, set the values for the insert command and add two records
        insertCommand.Parameters["@id"].Value = pumpID;
        insertCommand.Parameters["@fueltype"].Value = fuel;
        insertCommand.Parameters["@price"].Value = price;
        insertCommand.Parameters["@volume"].Value = volume;
        insertCommand.ExecuteNonQuery();
    }

}
错误发生在以黄色突出显示的此行上

insertCommand.Parameters["@volume"].Value = volume;
试试这个

SQLiteCommand insertCommand = myConnection.CreateCommand();
        insertCommand.CommandText = "insert into Records (ID, FuelType, Price, TankVolumes) values (@id, @fueltype, @price, @tankvolume)";
        insertCommand.Parameters.Add(new SQLiteParameter("@id"));
        insertCommand.Parameters.Add(new SQLiteParameter("@fueltype"));
        insertCommand.Parameters.Add(new SQLiteParameter("@price"));
        insertCommand.Parameters.Add(new SQLiteParameter("@tankvolume"));
        addSQL(insertCommand, int.Parse(SQLData[0]), SQLData[1], float.Parse(SQLData[2]), double.Parse(SQLData[3]));





public static void addSQL(SQLiteCommand insertCommand, int pumpID, string fuel, float price, double volume){
         /// Now, set the values for the insert command and add two records
        insertCommand.Parameters["@id"].Value = pumpID;
        insertCommand.Parameters["@fueltype"].Value = fuel;
        insertCommand.Parameters["@price"].Value = price;
        insertCommand.Parameters["@tankvolume"].Value = volume;
        insertCommand.ExecuteNonQuery();
    }

您已经创建了一个名为
“@tankvolume”
的参数,但在向参数集合添加值时,您尝试使用名为
“@volume”
的参数。此参数不存在,因此您会得到异常。只要使用正确的名称,错误就会消失

或者您可以使用AddWithValue方法

    insertCommand.Parameters.AddWithValue("@id", Convert.ToInt32(SQLData[0]));
    insertCommand.Parameters.AddWithValue("@fueltype",SQLData[1]));
    insertCommand.Parameters.AddWithValue("@price", Convert.ToSingle(SQLData[2]));
    insertCommand.Parameters.AddWithValue("@tankvolume", Convert.ToDouble(SQLData[3]));

我还将尝试在变量数组SQLData上添加一些错误检查,如果由于某种原因您没有返回四个有效值,那么您的代码将再次崩溃。

您将其拼写为@tankvolume