C# can';t在我的main方法中调用addSQLmethod

C# can';t在我的main方法中调用addSQLmethod,c#,sqlite,C#,Sqlite,您好,目前我正在尝试使用SQLite构建一个表,但是我看不到如何调用我在主方法中创建的方法。我真的不明白为什么不能在main方法中调用它。我已经尝试将我的addSQL方法私有化和公有化,尽管我知道这对我的影响不大,但我仍然在尝试。目前,当我尝试在main方法中调用它时,它根本不会选择我的方法。另外,在我的addSQL方法中,当使用insertCommand时,我得到一个错误,说它在当前上下文中不存在 using System; using System.Collections.Generic;

您好,目前我正在尝试使用SQLite构建一个表,但是我看不到如何调用我在主方法中创建的方法。我真的不明白为什么不能在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();

       }

    }
}

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

   }

您的
addSQL
是一个实例方法,不能直接从静态方法调用实例方法。将
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();
}

将addSql()方法设为静态

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

您遇到了什么错误?很抱歉,我的代码有误导性…当前,当我尝试在main方法中调用它时,它根本不会选择我的方法。另外,在我的addSQL方法中,我在使用insertCommand时出错,说它在当前上下文中不存在。我尝试了此方法,但SqlLiteCommand以红色突出显示,说名称空间名称无法使用found@DorkMonstuh很抱歉打字错误修好了。