我正在使用带有Mvvm Light和Mysql数据库的C#开发应用程序。当我使用对象的参数作为DateTime对象插入数据库时,我收到以下错误:
System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
at System.Convert.ToInt32(Object value)
at MySql.Data.Types.MySqlInt32.MySql.Data.Types.IMySqlValue.WriteValue(MySqlPacket packet, Boolean binary, Object val, Int32 length)
at MySql.Data.MySqlClient.MySqlParameter.Serialize(MySqlPacket packet, Boolean binary, MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.Statement.SerializeParameter(MySqlParameterCollection parameters, MySqlPacket packet, String parmName, Int32 parameterIndex)
at MySql.Data.MySqlClient.Statement.InternalBindParameters(String sql, MySqlParameterCollection parameters, MySqlPacket packet)
at MySql.Data.MySqlClient.Statement.BindParameters()
at MySql.Data.MySqlClient.PreparableStatement.Execute()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
以下是我的视图模型代码(proj.ProjectStartDate是一个日期时间对象):
DateTime startDate = projectStartDateProperty?? DateTime.Now;
string startStr = startDate.ToString("d");
DateTime projStart = Convert.ToDateTime(startStr);
DateTime endDate = projectEndDateProperty ?? DateTime.Now;
string endStr = endDate.ToString("d");
DateTime projEnd = Convert.ToDateTime(endStr);
proj.ProjectName = _projName;
proj.ProjectStartDate = projStart;
proj.ProjectEndDate = projEnd;
proj.ProjectNotes = _projNotes;
ProjectDbInteraction.InsertProject(_projName, proj);
在proj.ProjectEndDate设置断点,结果是
以下是插入MySql数据库的模型代码:
string connStr = "server=*****;database=" + projName + ";user=*****;port=*****;password=*****;";
MySqlConnection conn = new MySqlConnection(connStr);
MySqlCommand cmd;
string queryInsertintoProjTable;
try
{
conn.Open();
cmd = new MySqlCommand("INSERT INTO " + projName +
".project(ProjectID,ProjectName,ProjectStartDate,ProjectEndDate,ProjectNotes) VALUES("
+ "(?projectid, ?projectnamecolumn,?projectstartdatecolumn,?projectenddatecolumn,?projectnotescolumn);", conn);
cmd.Parameters.AddWithValue("?projectid", MySqlDbType.Int32).Value = proj.ProjectID;
cmd.Parameters.AddWithValue("?projectnamecolumn", MySqlDbType.VarChar ).Value = proj.ProjectName;
cmd.Parameters.AddWithValue("?projectstartdatecolumn", proj.ProjectStartDate.ToString("yyyy-MM-dd HH:mm:ss"));
cmd.Parameters.AddWithValue("?projectenddatecolumn", proj.ProjectEndDate.ToString("yyyy-MM-dd HH:mm:ss"));
cmd.Parameters.AddWithValue("?projectnotescolumn", MySqlDbType.VarChar).Value = proj.ProjectNotes;
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
那么我需要做些什么来解决这个问题呢?这是我第一次使用MySql。
答案 0 :(得分:0)
所以我发现了这个问题。我的日期时间实际上已经是正确的格式。不幸的是,在参数化我的查询时,Enum MySqlDbType.DateTime抛出了一个错误的错误,所以我只是在AddValue中传递了日期。
string connStr = "server=****;database=" + projName + ";user=***;port=****;password=****;";
MySqlConnection conn = new MySqlConnection(connStr);
MySqlCommand cmd;
try
{
conn.Open();
cmd = new MySqlCommand("INSERT INTO " + projName +
".project(ProjectID,ProjectName,ProjectStartDate,ProjectEndDate,ProjectNotes) VALUES"
+ "(?projectid, ?projectnamecolumn,?projectstartdatecolumn,?projectenddatecolumn,?projectnotescolumn);", conn);
cmd.Parameters.AddWithValue("?projectid", proj.ProjectID);
cmd.Parameters.AddWithValue("?projectnamecolumn", proj.ProjectName);
cmd.Parameters.AddWithValue("?projectstartdatecolumn", proj.ProjectStartDate.ToString("yyyy-MM-dd HH:mm:ss"));
cmd.Parameters.AddWithValue("?projectenddatecolumn", proj.ProjectEndDate.ToString("yyyy-MM-dd HH:mm:ss"));
cmd.Parameters.AddWithValue("?projectnotescolumn", proj.ProjectNotes);
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
一旦我从AddWithValue中移除了枚举,它就可以正常工作。