【问题标题】:How to pass GUID parameters to SQL Server through a stored procedure?如何通过存储过程将 GUID 参数传递给 SQL Server?
【发布时间】:2021-04-28 08:27:25
【问题描述】:

我正在迈出编程的第一步,所以我对此有点陌生。提前感谢您提供的帮助。

代码是这样的:

SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["CET47ConnectionString"].ConnectionString);
        SqlCommand myCommand = new SqlCommand();


        myCommand.Parameters.AddWithValue("@nome", nome.Value);
        myCommand.Parameters.AddWithValue("@data_de_nascimento", Convert.ToDateTime(data_de_nascimento.Value));
        myCommand.Parameters.AddWithValue("@rua", rua.Value);
        myCommand.Parameters.AddWithValue("@localidade", localidade.Value);
        myCommand.Parameters.AddWithValue("@concelho", concelho.Value);
        myCommand.Parameters.AddWithValue("@codigo_postal", codigopostal1.Value + " - " + codigopostal2.Value);
        myCommand.Parameters.AddWithValue("@pais", pais.Value);
        myCommand.Parameters.AddWithValue("@telefone", telf.Value);
        myCommand.Parameters.AddWithValue("@telemovel", telem.Value);
        myCommand.Parameters.AddWithValue("@email", email.Value);
        myCommand.Parameters.AddWithValue("@nif", nif.Value);

        SqlParameter val_output = new SqlParameter();
        val_output.ParameterName = "@retorno";
        val_output.Direction = ParameterDirection.Output;
        val_output.SqlDbType = SqlDbType.Int;

        myCommand.Parameters.Add(val_output);

        myCommand.CommandType = CommandType.StoredProcedure;
        myCommand.CommandText = "inserir_candidato";

        myCommand.Connection = myConn;
        myConn.Open();
        myCommand.ExecuteNonQuery();

        int valor_retornado = Convert.ToInt32(myCommand.Parameters["@retorno"].Value);

        myConn.Close();

        if (valor_retornado == 0)
        {
            Lbl_message.Text = "O utilizador já existe";

        }
        else
        {
            string caminho = ConfigurationSettings.AppSettings.Get("PathFicheiros");// string que aponta para localhost
            string caminhoPDFs = ConfigurationSettings.AppSettings.Get("PathFicheirosPDFs");// string que aponta para local fisico do ficheir
            string pdfTemplate = caminhoPDFs + "Template\\template.pdf";
            //Response.Write(pdfTemplate);
            //Response.End();

            Guid g = Guid.NewGuid();

            string nomePDF = g + ".pdf";
            string newFile = caminhoPDFs + nomePDF;

            PdfReader pdfr = new PdfReader(pdfTemplate);

            PdfStamper pdfst = new PdfStamper(pdfr, new FileStream(newFile, FileMode.Create));

            AcroFields pdfform = pdfst.AcroFields;

            pdfform.SetField("nome", nome.Value);// o nome é o atributo que esta na template.
            pdfform.SetField("data_de_nascimento", data_de_nascimento.Value);
            pdfform.SetField("rua", rua.Value);
            pdfform.SetField("localidade", localidade.Value);
            pdfform.SetField("concelho", concelho.Value);
            pdfform.SetField("codigo_postal", codigopostal1.Value + "-" + codigopostal2.Value);
            pdfform.SetField("pais", pais.Value);
            pdfform.SetField("telefone", telf.Value);
            pdfform.SetField("telemovel", telem.Value);
            pdfform.SetField("email", email.Value);
            pdfform.SetField("contribuinte", nif.Value);

            pdfst.Close();

SqlConnection myConn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["CET47ConnectionString"].ConnectionString);
SqlCommand myCommand2 = new SqlCommand();

myCommand2.Parameters.AddWithValue("@nif", nif.Value);
myCommand2.Parameters.AddWithValue("@gui", g);

myCommand2.CommandType = CommandType.StoredProcedure;
myCommand2.CommandText = "registro_inscricao";
myCommand2.Connection = myConn2;

myConn2.Open();
myCommand2.ExecuteNonQuery();
myConn2.Close();

存储过程

CREATE PROCEDURE registro_inscricao
    @nif as int,
    @gui as uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;

    BEGIN
        UPDATE registos
        SET registo = @gui
        WHERE nif = @nif
    END
END

我得到这个错误:

System.Data.SqlClient.SqlEXception: '过程或函数 registro_inscricao 指定了太多参数'

已经修复了一个错误。谢谢@克劳斯。但仍然无法将 guid 的值传递给数据库

【问题讨论】:

  • 你同时拥有myCommand2myCommand;错别字?
  • 奇怪。你确定你没有指向另一个数据库吗?因为代码似乎是正确的。
  • 这是同一个数据库。 @KlausGütter 大声笑谢谢。没看到。
  • 正如 Klaus 所说,您可能应该在任何地方使用 commd 2,因为您的 myCommand 之前必须使用其他参数进行初始化
  • 您将所有参数从第一个 SP 传递到第二个。您要么需要清除它们,要么使用不同的命令。另请参阅下面提到的关于使用 AddWithValue 的博客文章

标签: c# asp.net sql-server


【解决方案1】:

您可以尝试以下方法。

   using (SqlConnection con = new SqlConnection(dc.Con)) {
        using (SqlCommand cmd = new SqlCommand("registro_inscricao", con)) {
          cmd.CommandType = CommandType.StoredProcedure;
    
            cmd.Parameters.Add("@nif", SqlDbType.Int).Value = nif.Value;
            cmd.Parameters.Add("@gui", SqlDbType.UniqueIdentifier).Value = g;

          con.Open();
          cmd.ExecuteNonQuery();
        }
      }

【讨论】:

  • 为什么要将类型指定为SqlDbType.VarChar,而不是根据过程定义(SqlDbType.IntSqlDbType.UniqueIdentifier)的正确类型?
  • @KlausGütter,不。感谢您的更正。它应该根据正确的类型。已更正。
  • 好的,它现在可以工作了。在我纠正了@KlausGütter 提到的错字之后,我忘记了保存代码。现在正在工作。再次感谢大家的帮助。
【解决方案2】:

正如@xantos 所说,您在上面提供的代码确实有效,但是 - 您没有包括变量的声明方式,如果“g”绝对是 GUID,或者您尝试传递包含 GUID 作为属性,就像 @nif 的 Value 属性一样?

我重构了您的 C# 代码如下,以包含变量定义(并在完成后处理 SQL 对象 - 自己清理的好习惯):

static void TestDB()
{
    var nif = new { Value = 100 };
    Guid g = Guid.NewGuid();

    using (SqlConnection myConn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["CET47ConnectionString"].ConnectionString))
    {
        myConn2.Open();
        using (SqlCommand myCommand2 = new SqlCommand())
        {
            myCommand2.Parameters.AddWithValue("@nif", nif.Value);
            myCommand2.Parameters.AddWithValue("@gui", g);

            myCommand2.CommandType = CommandType.StoredProcedure;
            myCommand2.CommandText = "registro_inscricao";
            myCommand2.Connection = myConn2;

            myCommand2.ExecuteNonQuery();
        }
        myConn2.Close();
    }
}

我还根据您的定义创建了一个示例存储过程,如下所示:

CREATE PROCEDURE registro_inscricao
    @nif as int,
    @gui as uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;

    BEGIN
        PRINT 'GUID: ' + CAST(@gui AS NVARCHAR(50))
        --UPDATE registos
        --SET registo = @gui
        --WHERE nif = @nif
    END
END

结果是,第一次就成功了。

您的问题似乎不在上面的代码中,您更有可能是在尝试传递集合或对象。

检查 nif.Value 是单个 INT 值而不是集合(例如 INT[])

检查 g 是单个 GUID 值(例如,它未定义为集合类型 GUID[] 或包含您的 GUID { SomeProperty = GUID }

的属性

【讨论】:

  • 我在这里声明 GUID:Guid g = Guid.NewGuid();我用它来关联到pdf。这段代码的最后一部分是发送带有 pdf 附件的电子邮件,其中 guid 是对注册的引用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-20
  • 2013-07-06
  • 1970-01-01
  • 2017-02-19
  • 1970-01-01
  • 2014-08-29
  • 1970-01-01
相关资源
最近更新 更多