你好,我有一些问题代码 我使用按钮来保存从类数据库中的数据,但我一直有我有错误“无效列名“请解决我的问题。我不能过班。那显示错误“无效列名”插入数据库中的数据
该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; }
}
}
,这是保存按钮的方法
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");
}
}
即表
column name datatype
time_local time(7)
city_local varchar(50)
date_flight date
local_airline varchar(50)
flight_number int
加入确切的错误,也是你的表定义。 – Gusman
插入语句中的一个或多个列不在表中。这可能是一个类似于你的表名的错字:local_fli ** gth ** _ timinig – Crowcoder
了解如何执行以下操作'1。学习参数化查询,2.移动到这个存储的Procedure' – MethodMan