こんにちは私はコードでいくつかの問題があります ボタンを使用してデータベースからクラスにデータを保存しますが、 "私の問題を解決してください。クラスを介してデータベースにデータを挿入することができません。 "無効な列名"
id接続クラス。
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using project_airline_oop.user.staft.new_member;
using project_airline_oop.user.schedules_flights;
namespace project_airline_oop.dbconnection
{
public static class dbconnection
{
private static bool connection;
private static string _query;
public static readonly string ConnectionString = "Data Source=LUKAAA\\SQLSERVER1;Initial Catalog=airline;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
public static SqlConnection GetConnection()
{
SqlConnection conn = null;
try
{
conn = new SqlConnection(ConnectionString);
return conn;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return conn;
}
public static bool insertlocalflight(local_scheduleflight ab)
{
connection = false;
using (var con = GetConnection())
{
try
{
con.Open();
_query = "insert into local_fligth_timinig (" +
"time_flightlocal," +
"local_destination," +
"flight_date," +
"local_flightname," +
"flight_no)" +
"values(" +
"@time_local," +
"@city_local," +
"@date_flight," +
"@local_airline," +
"@flight_number)";
var cmd = new SqlCommand(_query, con);
cmd.Parameters.AddWithValue("@time_local", ab.time_flightlocal);
cmd.Parameters.AddWithValue("@city_local", ab.local_destination);
cmd.Parameters.AddWithValue("@date_flight", ab.flight_date);
cmd.Parameters.AddWithValue("@local_airline", ab.local_flightname);
cmd.Parameters.AddWithValue("@flight_number",ab.flight_no);
cmd.ExecuteNonQuery();
connection = true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
return connection;
}
}
}
namespace project_airline_oop.user.schedules_flights
{
public class local_scheduleflight
{
public DateTime time_flightlocal { get; set; }
public string local_destination { get; set; }
public DateTime flight_date { get; set; }
public string local_flightname { get; set; }
public int flight_no { get; set; }
}
}
私のクラスであり、それがテーブル
column name datatype
time_local time(7)
city_local varchar(50)
date_flight date
local_airline varchar(50)
flight_number int
あるボタン方式
private void save_flightloacal1_Click(object sender, EventArgs e)
{
var localflight = new user.schedules_flights.local_scheduleflight
{
time_flightlocal = flight_time1lo.Value,
local_destination = local_airport.DisplayMember,
flight_date = Date_flight.Value,
local_flightname = local_airport.DisplayMember,
flight_no = int.Parse(flight_nolocal.Text),
};
if (dbconnection.dbconnection.insertlocalflight(localflight))
{
MessageBox.Show("insert first 1 slot");
}
else``
{
MessageBox.Show("error");
}
}
を保存していますあなたの挿入文
_query = "insert into local_fligth_timinig (" +
"time_flightlocal," +
"local_destination," +
"flight_date," +
"local_flightname," +
"flight_no)" +
中
正確なエラーとテーブル定義を追加します。 – Gusman
insertステートメント内の1つ以上の列がテーブルにありません。あなたのテーブル名のようなタイプミスかもしれません:local_fli ** gth ** _ timinig – Crowcoder
以下の方法を学ぶ時間 '1。パラメータ化されたクエリを学習します。2.これをストアドプロシージャに移動します。 – MethodMan