将数据类型varchar转换为float时出错。 C#VS.

时间:2016-06-11 18:01:32

标签: sql visual-studio-2015

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

这不是我的代码!

"将数据类型varchar转换为float"但我无法找到错误。 我发现错误来自这段代码:

        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();
    }

1 个答案:

答案 0 :(得分:0)

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

一种方法是查找有问题的值(例如finalizedTotalAmount),然后将它们换成转化:cast(finalizedTotalAmount as varchar(255))

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