2013-05-07 78 views
-3

目前我在嘗試構建我的SQL表時出現錯誤,錯誤在標題「索引超出範圍內指出」,必須是非負數並且小於集合的大小 參數名稱索引:」我不明白爲什麼它拋出這個然而本身似乎確定的代碼...指數超出範圍。必須是非負數且小於集合的大小。參數名稱:索引SQLite

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; 
+2

您已拼寫爲@tankvolume – Cris 2013-05-07 06:57:25

回答

1

您已經創建了一個名爲"@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上檢查,如果由於某種原因你沒有得到四後衛有效值代碼將再次崩潰。

0

高亮出現的錯誤嘗試此

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(); 
    } 
相關問題