-1

こんにちは現在、SQLite を使用してテーブルを作成しようとしていますが、作成したメソッドをメイン メソッドに呼び出すことができません。メインメソッドで呼び出せない理由がよくわかりません。メソッドaddSQLをプライベートおよびパブリックにしようとしましたが、これが実際にはあまり影響しないことはわかっていますが、まだ試しました。現在、メイン メソッドでメソッドを呼び出そうとすると、メソッドがまったく選択されません。また、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静的にするか、クラスのインスタンスを介して呼び出します。

コードのもう 1 つの問題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 に答える
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 に答える