【发布时间】: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 的值传递给数据库
【问题讨论】:
-
你同时拥有
myCommand2和myCommand;错别字? -
奇怪。你确定你没有指向另一个数据库吗?因为代码似乎是正确的。
-
这是同一个数据库。 @KlausGütter 大声笑谢谢。没看到。
-
正如 Klaus 所说,您可能应该在任何地方使用 commd 2,因为您的 myCommand 之前必须使用其他参数进行初始化
-
您将所有参数从第一个 SP 传递到第二个。您要么需要清除它们,要么使用不同的命令。另请参阅下面提到的关于使用
AddWithValue的博客文章
标签: c# asp.net sql-server