こんにちは現在、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();
}
}
}