Ciao a tutti, sto facendo un po di sperimentazione con SQL Server 2008.
A tal proposito ho creato una Stored Procedure che si occupa di inserire in tabella e di ritornare l'ID del record appena inserito
USE [NomeDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Inserisci_I]
@ID_I INT OUTPUT,
@Me VARCHAR(20),
@Me_Ca VARCHAR(20),
@De VARCHAR(255),
@C_T_I INT,
@In VARCHAR(255),
@CP VARCHAR(10),
@Cm VARCHAR(100),
@C_P INT,
@Te VARCHAR(20) = NULL,
@Fa VARCHAR(20) = NULL,
@Ma VARCHAR(100) = NULL
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO Istituti
([Me],
[Me Ca],
[De],
[C T I],
[In],
[CP],
[Cm],
[C P],
[Te],
[Fa],
[Ma])
VALUES (@Me,
@Me_Ca,
@De,
@C_T_I,
@In,
@CP,
@Co,
@C_P,
@Te,
@Fa,
@Ma);
SET @ID_I = (SELECT @@IDENTITY);
END
Se chiamo la Stored Procedure da Management Studio funziona correttamente, senza alcun problema.
Mentre se utilizzo il seguente codice da una pagina asp (VS2005)
string strConnection = Application["StrConnection"].ToString();
SqlConnection myConnection = new SqlConnection(strConnection);
myConnection.Open();
SqlCommand myCommand = new SqlCommand("Inserisci_I", myConnection);
myCommand.Parameters.Add("@Me", SqlDbType.VarChar).Value = "3";
myCommand.Parameters.Add("@Me_Ca", SqlDbType.VarChar).Value = "2";
myCommand.Parameters.Add("@De", SqlDbType.VarChar).Value = "3";
myCommand.Parameters.Add("@C_T_I", SqlDbType.Int).Value = 4;
myCommand.Parameters.Add("@In", SqlDbType.VarChar).Value = "5";
myCommand.Parameters.Add("@CP", SqlDbType.VarChar).Value = "6";
myCommand.Parameters.Add("@Co", SqlDbType.VarChar).Value = "7";
myCommand.Parameters.Add("@C_P", SqlDbType.Int).Value = 8;
myCommand.Parameters.Add("@Te", SqlDbType.VarChar).Value = "9";
myCommand.Parameters.Add("@Fa", SqlDbType.VarChar).Value = "10";
myCommand.Parameters.Add("@Ma", SqlDbType.VarChar).Value = "11";
SqlParameter IDIst = new SqlParameter("@ID_I", SqlDbType.Int);
IDIst.Direction = ParameterDirection.Output;
myCommand.Parameters.Add(IDIst);
myCommand.ExecuteNonQuery();
TextBox1.Text = myCommand.Parameters["@ID_I"].Value.ToString();
myConnection.Close();
mi ritorna l'errore "Incorrect syntax near 'Inserisci_I'.
Dove sbaglio?
Grazie, ciao