【问题标题】:i cant insert data in database through classes .that show error "invalid column name"我无法通过类在数据库中插入数据。显示错误“列名无效”
【发布时间】:2017-05-04 22:29:41
【问题描述】:

你好,我的代码有问题 我使用按钮将数据从类保存到数据库,但我一直有错误“列名无效”,请解决我的问题。

那个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");
        }
    }

enter image description here

那是桌子

column name       datatype
time_local        time(7)
city_local        varchar(50) 
date_flight       date
local_airline     varchar(50)
flight_number     int

【问题讨论】:

  • 添加确切的错误以及您的表定义。
  • 插入语句中的一列或多列不在表中。这可能是一个错字,就像您的表名一样:local_fligth_timinig
  • 是时候学习如何执行以下操作了1. learn Parameterized Query, 2. Move this into a Stored Procedure
  • @MethodMan,虽然这不会通过我的代码审查,但这不是参数化查询吗?
  • 这里看起来像是一个错字cmd.Parameters.AddWithValue("@flight_number",ab.flight_no);

标签: c# database winforms


【解决方案1】:

插入语句中的字段名称

_query = "insert into local_fligth_timinig (" +
                    "time_flightlocal," +
                    "local_destination," +
                    "flight_date," +
                    "local_flightname," +
                    "flight_no)" +

与表格中的字段名称不匹配

column name       datatype
time_local        time(7)
city_local        varchar(50) 
date_flight       date
local_airline     varchar(50)
flight_number     int

改成

_query = "insert into local_fligth_timinig (" +
                    "time_local," +
                    "city_local," +
                    "date_flight," +
                    "local_airline," +
                    "flight_number)" +

看起来也像是表名的拼写错误,但这可能是您在数据库中的拼写方式。

【讨论】:

  • 但是我有一个班级我想通过班级保存数据。并在值中告诉我我输入了什么?
  • 类和变量名保持不变。只是插入语句中的数据库字段名称需要更改,因此它们与数据库结构匹配。
  • 我明白你想要你说什么,但我有点困惑。请帮我一个忙,请让我插入查询以查看我的代码并传递值
  • 替换我告诉你要替换的 6 行。
猜你喜欢
  • 2020-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
  • 2013-03-02
  • 2016-09-24
  • 1970-01-01
相关资源
最近更新 更多