我遇到了输入datetime.now.toshortstring()的问题,其中mysql使用数据yyyy-MM-dd的语法,并且使用dd-mm-yyyy,并且它导致一个名为“”的错误在MySql.Data.dll 中发生了类型为'MySql.Data.MySqlClient.MySqlException'的未处理异常其他信息:第1行的列'DATA'的日期值不正确:'29 -02-2016' “由错误的日期格式导致的sql错误
下面是代码:
DialogResult dr = MessageBox.Show("Deseja mesmo gravar estas alterações?", "Confirmação", MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (dr == DialogResult.Yes)
{
string con = "server=localhost;userid=root;password=12345;persistsecurityinfo=True;database=portaria";
string query = "Insert into entradas(id_veiculo,empresa_visitante,empresa_visitar,nome_condutor,visitado,ncartao,data,hora,obs) values('"+ msktxtmat.Text +"','"+ txtempvis.Text +"','"+ comboBox1.SelectedValue +"', '"+ txtnomecondutor.Text +"','"+ txtpessoavisitar.Text +"', '"+ txtncartao.Text +"', '"+ DateTime.Now.ToShortDateString() +"', '"+ DateTime.Now.ToShortTimeString() +"','"+ txtobs.Text +"');";
MySqlConnection Con = new MySqlConnection(con);
MySqlCommand Command = new MySqlCommand(query, Con);
MySqlDataAdapter sda = new MySqlDataAdapter();
DataTable dt = new DataTable();
sda.SelectCommand = Command;
sda.Fill(dt);
}
是啊,我知道,我应该使用参数化querys但actualy我不想浪费时间用参数化重建这段代码,我只是坚持使用常规的形式。
在此先感谢
使用参数,这将解决您的格式问题,它也将为您节省SQL注入。 – Habib
'我知道我应该使用参数化查询,但实际上我不想浪费时间'所以相反,你正在浪费你的时间与字符串日期,格式化问题,类型转换和那个可怕的SQL字符串摔跤。 – Plutonix
我知道它,但由于这是一个垃圾的事情只是为了测试这种方法,我不会botter做任何参数:p这只是教育建议,向我的朋友们展示语言,这将是更多的他们很容易理解这种方法。而且我总是在我的项目中使用参数,但不会在我不会发布的项目上提前感谢! –