簡體   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