如何使用C#插入信息到SQL Server数据库



我正在创建一个表单程序,该程序允许我插入有关邮件(物理),该部门已收到。我是初学者我对代码有问题。

我使用此代码:

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:输入字符串不是正确的格式。

任何帮助将不胜感激。

尝试此

您无法通过转换将零值转换为有意义的值。 您最好的选择是先检查NULL,然后分配一个值 0(或其他)到结果(无论convert以发送其 结果也)。

int result=0;
DateTime val;
TimeSpan Timeval;
String str ="INSERT INTO correspondencia_FFAA (no_reg, fecha_cre, hora_crea, corres_tipo,num_corres, dfecha, origen, enviar_a, estado_corres, asunto_corres, mens_corres, usuario,recibido)values(@registro,@fecha_creacion,@hora_creacion,@tipo_corres,@numero,@dfecha, @origen,@enviar_a,@estado,@asunto,@mensaje,@usuario,@recibo)";
comando.CommandText= str;
if(int.TryParse(txtNoReg.Text,out result))
{
    comando.Parameters.Add("@registro",SqlDbType.BigInt).Value = txtNoReg.Text;
}
if(DateTime.TryParse(txtFechCrea.Text,out val))
{
    comando.Parameters.Add("@fecha_creacion", SqlDbType.Date).Value = txtFechCrea.Text;
}
if(TimeSpan.TryParse(txtHorCrea.Text,out TimeVal))
{
    comando.Parameters.Add("@hora_creacion", SqlDbType.Time).Value = txtHorCrea.Text;
}
if(int.TryParse(cbbTipo.Text,out result))
{
    comando.Parameters.Add("@tipo_corres", SqlDbType.VarChar, 30).Value = cbbTipo.Text;
}
if(int.TryParse(txtNo.Text,out result))
{
    comando.Parameters.Add("@numero", SqlDbType.BigInt).Value = txtNo.Text;
}
if(DateTime.TryParse(txtDF.Text,out val))
{
    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;

您遇到的问题是,您必须将整数转换为txtNoReg.Text中的int等。

请尝试Convert.ToDateTime(txtDate.text)Convert.ToInt32(txtNo.Text)

最新更新