Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
指定的强制转换在C#和MySQL中无效_C#_Mysql - Fatal编程技术网

指定的强制转换在C#和MySQL中无效

指定的强制转换在C#和MySQL中无效,c#,mysql,C#,Mysql,我怎样才能解决这个问题?我试图阻止重复出席,但我无法解决这个问题。我试着从日期开始 if(label1.Text != "" && label2.Text != "" && label3.Text != "") { try { string c = @"(select count(*)from Attendance where Date='"+l

我怎样才能解决这个问题?我试图阻止重复出席,但我无法解决这个问题。我试着从日期开始

            if(label1.Text != "" && label2.Text != "" && label3.Text != "")
        {
            try
            {
                string c = @"(select count(*)from Attendance where Date='"+label1.Text+"')";
                MySqlCommand cmd = new MySqlCommand("INSERT INTO Attendance (Name,Date,empIn)VALUES(@Name,@Date,@empIn)", con);              
                con.Open();
                MySqlCommand cmdc = new MySqlCommand(c,con);
                int count = (int)cmdc.ExecuteScalar();
                if(count > 0)
                {

                    MessageBox.Show("This data is already IN");
                }
                else
                {
                    cmd.Parameters.Add("@Name", MySqlDbType.VarChar).Value = label3.Text;
                    cmd.Parameters.Add("@Date", MySqlDbType.VarChar).Value = label1.Text;
                    cmd.Parameters.Add("@empIn", MySqlDbType.VarChar).Value = label2.Text;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Attendance Inserted");
                }

                con.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }


        }

看起来您正在寻找MySql语法。注意sql参数:如果查询需要
DateTime
@Date
参数),请提供
DateTime
DateTime.ParseExact
等):


我试着从date
cmd.Parameters.Add(“@date”,MySqlDbType.VarChar)转换他=>为什么将日期存储为
varchar
Date
列的数据类型是什么?@tetsuyamamoto varchar先生tetsuyaJust一条建议:最好将日期值存储为
Date
datetime
,而不是
varchar
,因为它更容易转换为C#
datetime
结构。由于命令试图将字符串转换为日期格式,可能会发生无效强制转换。Dmitry Bychenko错误为System.FormatException:“字符串未被识别为有效的日期时间”。@Hei Sciff:您应该使用正确的格式(请参见
//TODO:
注释)。在提供的代码中,我假设格式为
“d/M/yyyy”
,例如
14/1/2019
。如果您使用不同的
DateTime
表示,您应该更改格式。@Hei-Sciff:根据wiki(),您可以使用月-日-年格式,如果您使用的是
“M-d-yyyy”
string sql = 
  @"insert ignore Attendance(
      Name,
      Date,
      empIn)
    values(
     @Name,
     @Date,
     @empIn)";

//DONE: wrap IDisposable into using
using (MySqlCommand cmd = new MySqlCommand(sql, con)) {
  cmd.Parameters.Add("@Name", MySqlDbType.VarChar).Value = label3.Text;
  // If Date is DateTime, provide DateTime, not string
  //TODO: provide the right date time format here
  cmd.Parameters.Add("@Date", MySqlDbType.DateTime).Value = 
    DateTime.ParseExact(label1.Text, "d/M/yyyy", CultureInfo.InvariantCulture);
  cmd.Parameters.Add("@empIn", MySqlDbType.VarChar).Value = label2.Text;

  if (cmd.ExecuteNonQuery() > 0)
    MessageBox.Show("Attendance Inserted");
  else
    MessageBox.Show("This data is already IN");
}