【问题标题】:How to insert information into a SQL Server Database with C#?如何使用 C# 将信息插入 SQL Server 数据库?
【发布时间】: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


【解决方案1】:

试试这个

您无法通过 Convert 将空值转换为有意义的值。 您最好的选择是首先检查 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;

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      • 2019-11-22
      • 2012-01-31
      相关资源
      最近更新 更多