输入字符串的格式不正确executereader

时间:2017-05-16 20:15:28

标签: c# mysql

我正在尝试执行下面的c#class bu我收到的错误是

输入字符串的格式不正确

行:MySqlDataReader reader = cmd.ExecuteReader();

我搜索过并发现了很多这样的错误,但是我无法找到这里发生的事情,请帮助我吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Windows.Forms;


namespace Bestival_Shop
{
    class Customer
    {
        public string Reservation_number { get; set; }
        public string First_Name { get; set; }
        public string Last_Name { get; set; }
        public double Deposit { get; set; }

        public void GetCustomer()
        {
            string ConnStr = ConfigurationManager.ConnectionStrings["ConnSet"].ConnectionString;
            string cmdstr = @"SELECT first_name,
                                     last_name,
                                     deposit_money
                                FROM customers 
                                WHERE reservation_number = @p_reservation_number";
            //try
            //{
                using (MySqlConnection conn = new MySqlConnection(ConnStr))
                using (MySqlCommand cmd = new MySqlCommand(cmdstr, conn))
                {
                    conn.Open();
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = cmdstr;
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@p_reservation_number", MySqlDbType.VarChar).Value = Reservation_number.ToString();
                    MySqlDataReader reader = cmd.ExecuteReader(); 

                    while (reader.Read())
                    {
                        First_Name =reader.GetValue(0).ToString();
                        Last_Name = reader.GetValue(1).ToString();
                        Deposit = (double)reader.GetValue(2);
                    }
                }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
        }
    }
}

在课堂召唤之下

private void btn_Shop_NewOrder_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrWhiteSpace(tb_Shop_ReservNum.Text))
    {
        Customer customer = new Customer();
        customer.Reservation_number = tb_Shop_ReservNum.Text;
        customer.GetCustomer();
        label_Shop_Info.Text = "Customer " + customer.First_Name + " " + customer.Last_Name + " Has Dipost money " + customer.Deposit;
    }
    else
    {
        MessageBox.Show("Please Enter A Reservation number", "Notification", MessageBoxButtons.OK,MessageBoxIcon.Information);
    }
}

1 个答案:

答案 0 :(得分:0)

我建议您查看要在SQL表中阅读的列。假设您想将deposit_money读为double,但是,如果Deposit_Money列中只有一条记录不是数字格式,则读者将无法将其转换为任何数字格式。尝试在SQL中执行Select语句,以查看要读取的列中是否有任何记录具有奇怪的数据,例如" N / A","#" ...如果SQL中的表源与Excel有关,那么会发生很多事情。