执行程序时如何纠正错误“字符串未被识别为有效的DateTime”?

时间:2018-12-31 15:14:07

标签: c# winforms ms-access

如何更正此错误“字符串未被识别为有效的DateTime”? 尝试保存数据时发生错误 尝试过这样的事情也没有运气(“ Checkin_Time”):

"this.Text="1/01/2019";
DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);"

将方法更改为更简化的排序方式...

谢谢

  try
    {
    string thisDay1 = DateTime.Now.ToString();


    Bitmap bm = new Bitmap(UpdateName);

    OleDbCommand cmd = new OleDbCommand("INSERT INTO [IMGs] (UPC, Checkin_Time, Photo) values (?, ?, ?)", Conn);

    byte[] image_bytes = ImageToBytes(bm, ImageFormat.Png);

    OleDbParameter param = new OleDbParameter();
    param.OleDbType = OleDbType.Binary;
    param.ParameterName = "Image";
    param.Value = image_bytes;
    cmd.Parameters.Add(param);

    OleDbParameter param1 = new OleDbParameter();
    param1.OleDbType = OleDbType.VarChar;
    param1.ParameterName = "UPC";
    param1.Value = textBox1.Text;
    cmd.Parameters.Add(param1);

    OleDbParameter param2 = new OleDbParameter();
    DateTime date = DateTime.ParseExact(DateTime.Now.ToString(),"yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
    param2.OleDbType = OleDbType.DBTime;//
    param2.ParameterName = "Checkin_Time";
    param2.Value = date;//TRIED USING THE "thisDay1" DIRECTLY & GOT SAME ERROR|FIELD IN DB IS SET AS Date/Time
    cmd.Parameters.Add(param2);


    // Execute the command (with no return value).
    cmd.Connection = Conn;
    Conn.Open();
    cmd.ExecuteScalar();
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    finally
    {
    if (Conn.State != ConnectionState.Closed) Conn.Close();
    }

1 个答案:

答案 0 :(得分:0)

您的字符串提供程序应如下所示:

OleDbConnection con1 = new OleDbConnection(@" provider=Microsoft.ace.Oledb.12.0; data source=C:\fotos\IMRDATABASE.accdb; Persist Security Info=False");

有关连接字符串的更多信息,请参见this