2013-05-07 59 views
-1

嗨目前我正在嘗試使用SQLite構建表,但是我看不到調用我創建到我的主方法中的方法。我不明白爲什麼我不能在主要方法中調用它。我試圖讓我的方法addSQL私人和公共儘管我知道這並沒有真正影響很多,我仍然嘗試過。目前,當我嘗試用主要方法調用它時,它根本不會選擇我的方法。此外,在我的addSQL方法我得到一個錯誤使用的InsertCommand說,它並不在目前情況下無法在我的主要方法中調用addSQLmethod

using System; 
using System.Collections.Generic; 
using System.Data.SQLite; 
using System.Linq; 
using System.Text; 

namespace SQLserver 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      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 (50), TankVolume int)"; 
      tableCreateCommand.ExecuteNonQuery(); 

      /// Now insert some data. 
      /// First, create a generalised insert command 
      SQLiteCommand insertCommand = myConnection.CreateCommand(); 
      insertCommand.CommandText = "insert into cars (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("@volume")); 
      addSQL(); 
     } 

     public void addSQL() 
     { 
      /// Now, set the values for the insert command and add two records 
      insertCommand.Parameters["@id"].Value = 1; 
      insertCommand.Parameters["@manufacturer"].Value = "Ford"; 
      insertCommand.Parameters["@model"].Value = "Focus"; 
      insertCommand.Parameters["@seats"].Value = 5; 
      insertCommand.Parameters["@doors"].Value = 5; 
      insertCommand.ExecuteNonQuery(); 

     } 

    } 
} 
+0

你是什麼錯誤得到? – 2013-05-07 05:08:16

+0

對不起,我的代碼很具誤導性......目前當我嘗試在主要方法中調用它時,它根本不會選擇我的方法。同樣在我的addSQL方法中,當使用insertCommand時它說不存在當前上下文中出現錯誤 – DorkMonstuh 2013-05-07 05:10:35

回答

1

您的addSQL是一個實例方法,您不能直接從Static方法調用實例方法。要麼使addSql爲靜態,要麼通過類的實例調用它。

其他問題在您的代碼中是insertCommand對該方法不可見。你可以將它作爲參數傳遞給你的方法,否則它將不能編譯。所以,你可以定義你的方法是靜態的,如:

public static void addSQL(SQLiteCommand insertCommand) 
{ 
    /// Now, set the values for the insert command and add two records 
    insertCommand.Parameters["@id"].Value = 1; 
    insertCommand.Parameters["@manufacturer"].Value = "Ford"; 
    insertCommand.Parameters["@model"].Value = "Focus"; 
    insertCommand.Parameters["@seats"].Value = 5; 
    insertCommand.Parameters["@doors"].Value = 5; 
    insertCommand.ExecuteNonQuery(); 
} 
1

addSql存在時,也不是一成不變的,所以調用該方法,因爲它是你需要的類節目的一個實例。爲了解決你的問題,只需讓addSql成爲一個靜態方法。

public static void addSQL() 
    { 
     /// Now, set the values for the insert command and add two records 
     insertCommand.Parameters["@id"].Value = 1; 
     insertCommand.Parameters["@manufacturer"].Value = "Ford"; 
     insertCommand.Parameters["@model"].Value = "Focus"; 
     insertCommand.Parameters["@seats"].Value = 5; 
     insertCommand.Parameters["@doors"].Value = 5; 
     insertCommand.ExecuteNonQuery(); 

    } 
1

讓addSql()方法爲靜態

0

讓你的addSQL方法靜態和採取SQLiteCommand參數:

... 
    addSQL(insertCommand); 
} 

public static void addSQL(SQLiteCommand insertCommand) 
{ 
    /// Now, set the values for the insert command and add two records 
    insertCommand.Parameters["@id"].Value = 1; 
    insertCommand.Parameters["@manufacturer"].Value = "Ford"; 
    insertCommand.Parameters["@model"].Value = "Focus"; 
    insertCommand.Parameters["@seats"].Value = 5; 
    insertCommand.Parameters["@doors"].Value = 5; 
    insertCommand.ExecuteNonQuery(); 
} 
+0

我試過這種方法,但SqlLiteCommand以紅色高亮顯示,名稱空間名稱無法找到 – DorkMonstuh 2013-05-07 05:45:26

+0

@DorkMonstuh對不起。錯字。修復。 – Dan 2013-05-07 06:50:16