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