ストアドプロシージャと@@ identityを使用して、リストボックスからデータベースへのすべての選択項目とともに異なるフィールドを追加しようとしています。選択したすべての項目をaspリストボックスからデータベースに挿入する
ただし、最初に選択した値のみがテーブルに挿入されます。 forループを使用しようとすると、ストアドプロシージャに引数が多すぎるというエラーが発生します。
私はasp.netを初めて使っていて、可能な限り助けてくれたでしょう。ストアドプロシージャ
:
ALTER Procedure [dbo].[InsertProcessUnit]
(
@ProcessUnitName Varchar(50),
@ProcessUnitNumber int,
@ProcessUnitInspected bit,
@RefineryAreaName Varchar(50),
@System_id int
)
As
Begin
BEGIN TRANSACTION
INSERT INTO Process_Units (Process_Unit_Name, Process_Unit_Number, Process_Unit_Inspected, Refinery_Area_id)
VALUES (@ProcessUnitName, @ProcessUnitNumber,@ProcessUnitInspected,(SELECT Refinery_Area_id from Refinery_Area where Refinery_Area_Name Like @RefineryAreaName))
DECLARE @DataID int;
SET @DataID = @@IDENTITY
INSERT INTO PU_Systems (Process_Unit_id, System_id) VALUES (@DataID, @System_id)
COMMIT
End
Aspx.cs code:
try
{
connection.Open();
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "InsertProcessUnit";
command.Parameters.AddWithValue("@ProcessUnitNumber", PUNumberTxtbox.Text);
command.Parameters.AddWithValue("@ProcessUnitName", PUNameDdl.SelectedItem.Value);
command.Parameters.AddWithValue("@ProcessUnitInspected", PUInspectedDdl.SelectedItem.Value);
command.Parameters.Add("@RefineryAreaName", SqlDbType.Text).Value = RefAreaNameTxtbox.Text;
command.Parameters.AddWithValue("@System_id", ListBox1.SelectedItem.Value);
command.ExecuteNonQuery();
connection.Close();
Label1.Text = "Successfully Inserted !!";
}
forループまたはforeachループを使用して、すべての反復で 'SqlParameter'をクリアする必要があるようです。リストボックスは反復処理されないので、デフォルトではDBに処理される最初の利用可能なデータが取り出されます。 –