不能在我的main方法中调用addSQLmethod

时间:2013-05-07 05:06:58

标签: c# sqlite

您好目前我正在尝试使用SQLite构建一个表但是我无法将我创建的方法调用到我的main方法中。我真的不明白为什么我不能在main方法中调用它。我试图让我的方法addSQL私有和公开但是我知道这并没有真正影响我仍然尝试过。当我尝试在main方法中调用它时,它根本不会选择我的方法。同样在我的addSQL方法中,当使用insertCommand说它在当前上下文中不存在时会出现错误

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();

       }

    }
}

4 个答案:

答案 0 :(得分:1)

addSql不是静态的,因此要按原样调用该方法,您需要一个类Program的实例。要解决您的问题,只需使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 :(得分: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();
}

答案 2 :(得分:1)

将addSql()方法设为静态

答案 3 :(得分: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();
}