-1

您好目前我正在尝试使用 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();

       }

    }
}
4

4 回答 4

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

   }
于 2013-05-07T05:10:38.047 回答
1

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();
}
于 2013-05-07T05:10:50.967 回答
1

将 addSql() 方法设为静态

于 2013-05-07T05:11:35.410 回答
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();
}
于 2013-05-07T05:14:05.310 回答