嗨目前我正在嘗試使用SQLite構建表,但是我看不到調用我創建到我的主方法中的方法。我不明白爲什麼我不能在主要方法中調用它。我試圖讓我的方法addSQL私人和公共儘管我知道這並沒有真正影響很多,我仍然嘗試過。目前,當我嘗試用主要方法調用它時,它根本不會選擇我的方法。此外,在我的addSQL方法我得到一個錯誤使用的InsertCommand說,它並不在目前情況下無法在我的主要方法中調用addSQLmethod
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();
}
}
}
你是什麼錯誤得到? – 2013-05-07 05:08:16
對不起,我的代碼很具誤導性......目前當我嘗試在主要方法中調用它時,它根本不會選擇我的方法。同樣在我的addSQL方法中,當使用insertCommand時它說不存在當前上下文中出現錯誤 – DorkMonstuh 2013-05-07 05:10:35