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

本文关键字:调用 addSQLmethod 方法 我的 不能 | 更新日期: 2023-09-27 18:12:19

嗨,目前我正试图使用SQLite建立一个表,但是我看不到调用我的方法,我创建了我的主要方法。我真的不明白为什么不能在主方法中调用它。我试图使我的方法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();
       }
    }
}

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

您的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不是静态的,因此要按原样调用该方法,您需要一个类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();
   }

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