【问题标题】:Error converting data type varchar to float. C# VS将数据类型 varchar 转换为浮点数时出错。 C# VS
【发布时间】:2016-06-11 18:01:31
【问题描述】:

我有以下 SQL 语句,它给了我这个错误。

这不是我的代码!

“将数据类型 varchar 转换为浮点数时出错” 但我找不到错误。 我认为错误来自此代码:

        birthday = (monthComboBox.SelectedItem) + "-" + (dayComboBox.SelectedIndex + 1) + "-" + yearTextBox.Text;

        Int32 getIDBack = 0;

        string query = "insert into reservation(first_name, last_name, birth_day, gender, phone_number, email_address, number_guest, street_address, apt_suite,city, state, zip_code, room_type, room_floor, room_number, total_bill,payment_type, card_type, card_number,card_exp,card_cvc, arrival_time, leaving_time, check_in, break_fast, lunch, dinner, supply_status, cleaning, towel, s_surprise, food_bill) values('" + firstNameTextBox.Text +

            "', '" + lastNameTextBox.Text + "', '" + birthday + "', '" + genderComboBox.SelectedItem + "', '" + phoneNumberTextBox.Text + "', '" + emailTextBox.Text +

          "', '" + (qtGuestComboBox.SelectedIndex + 1) + "', '" + addLabel.Text + "', '" + aptTextBox.Text + "', '" + cityTextBox.Text +

          "', , '" + zipComboBox.Text + "', '" + roomTypeComboBox.SelectedItem + "', '" + floorComboBox.SelectedItem +

          "', '" + roomNComboBox.SelectedItem + "', '" + finalizedTotalAmount + "', '" + paymentType +

          "', '" + CardType + "','" + paymentCardNumber + "','" + MM_YY_Of_Card + "','" + CVC_Of_Card + "', '" + dateTimePicker1.Text + "', '" + dateTimePicker2.Text + "','" + checkin +

          "', '" + breakfast + "','" + lunch + "','" + dinner + "', '" + foodStatus + "', '" + Convert.ToInt32(cleaning) + "', '" + Convert.ToInt32(towel) + "', '" + Convert.ToInt32(surprise) + "','" + foodBill + "');";
        query += "SELECT CAST(scope_identity() AS int)";
        SqlConnection connection = new SqlConnection(Hotel_Manager.Properties.Settings.Default.frontend_reservationConnectionString);

        SqlCommand query_table = new SqlCommand(query, connection);
        try
        {
            connection.Open();
            getIDBack = (Int32)query_table.ExecuteScalar();

            string userID = Convert.ToString(getIDBack);
            SendSMS(getIDBack);
            MetroFramework.MetroMessageBox.Show(this, "Entry successfully inserted into database. " + "\n\n" +
                "Provide this unique ID to the customer." + "\n\n" +
            "USER UNIQUE ID: " + userID, "Report", MessageBoxButtons.OK, MessageBoxIcon.Question);

            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        ComboBoxItemsFromDataBase();
        LoadForDataGridView();
        reset_frontend();
        GetOccupiedRoom();
        ReservedRoom();
        getChecked();
    }

【问题讨论】:

  • 将所有这些代码减少到相关的代码行

标签: sql visual-studio-2015


【解决方案1】:

这通常意味着一个(或多个)值是数字,而不是字符串。如果+ 表达式中的任何值是数字,则+ 被解释为加法而不是字符串连接。

一种方法是找到有问题的值(例如finalizedTotalAmount),然后将它们包装在转换中:cast(finalizedTotalAmount as varchar(255))

更好的方法是使用参数化查询,其中值作为类型化参数传入。这也更安全,因为它可以防止 SQL 注入。

【讨论】:

    猜你喜欢
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    相关资源
    最近更新 更多