2013-05-07 30 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