繁体   English   中英

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

[英]How to insert information into a SQL Server Database with C#?

我正在创建一个表单程序,该程序使我可以插入有关部门接收的邮件(物理)的信息。 我是一个初学者,我在代码方面遇到问题。

我使用以下代码:

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 integer转换为int等。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM