【发布时间】:2014-01-27 13:33:17
【问题描述】:
我正在创建一个表单程序,允许我插入关于 邮件(物理),在一个部门收到。我是初学者 我的代码有问题。
我使用这个代码:
conn = new SqlConnection(conexionString);
conn.Open();
comando = new SqlCommand(insertarBD, conn);
comando.CommandText = (@"INSERT INTO correspondencia_FFAA (no_reg, fecha_cre, hora_crea, corres_tipo, \n" +
"num_corres, dfecha, origen, enviar_a, estado_corres, asunto_corres, mens_corres, usuario, recibido) " +
"values(\n" +
" @registro,\n" +
" @fecha_creacion,\n" +
" @hora_creacion,\n" +
" @tipo_corres,\n" +
" @numero,\n" +
" @dfecha,\n" +
" @origen,\n" +
" @enviar_a, \n" +
" @estado, \n" +
" @asunto, \n" +
" @mensaje, \n" +
" @usuario, \n" +
" @recibo)");
comando.Parameters.Add("@registro",SqlDbType.BigInt).Value = txtNoReg.Text;
comando.Parameters.Add("@fecha_creacion", SqlDbType.Date).Value = txtFechCrea.Text;
comando.Parameters.Add("@hora_creacion", SqlDbType.Time).Value = txtHorCrea.Text;
comando.Parameters.Add("@tipo_corres", SqlDbType.VarChar, 30).Value = cbbTipo.Text;
comando.Parameters.Add("@numero", SqlDbType.BigInt).Value = txtNo.Text;
comando.Parameters.Add("@dfecha", SqlDbType.DateTime).Value = txtDF.Text;
comando.Parameters.Add("@origen", SqlDbType.VarChar, 35).Value = txtOrigen.Text;
comando.Parameters.Add("@enviar_a", SqlDbType.VarChar, 35).Value = txtDestino.Text;
comando.Parameters.Add("@estado", SqlDbType.VarChar, 20).Value = cbbEstado.Text;
comando.Parameters.Add("@asunto", SqlDbType.VarChar, 100).Value = txtAsunto.Text;
comando.Parameters.Add("@mensaje", SqlDbType.VarChar, 500).Value = rtxtMensaje.Text;
comando.Parameters.Add("@usuario", SqlDbType.VarChar, 40).Value = txtUsuario.Text;
comando.Parameters.Add("@recibo", SqlDbType.VarChar, 40).Value = txtRecibido.Text;
但是当我尝试运行ExecuteNonQuery 命令时,它会显示下一个错误:
System.FormatException:输入字符串格式不正确。
任何帮助将不胜感激。
【问题讨论】:
-
1) 您是否受制于某种技术? | 2)如果可以,请提供完整的错误代码和堆栈跟踪,它可能会提供更多信息。
-
您是否正在做任何事情来确保您的 DateTime 值(来自文本框)是有效的日期/时间,并且您的 BitInts(来自文本框)是整数?
-
添加断点并获取命令的内容 - 然后看看是否可以在 sql server management studio 中运行它...
-
您指定插入为逐字字符串文字 (
@"...")。我相信,此类文字中唯一有效的转义序列是""插入单个"字符。所以\n以\n的形式通过。
标签: c# sql sql-insert