如何为此c#语句创建存储过程
String Orders = "INSERT INTO Orders VALUES('" + DDLCustomerID.SelectedValue + "','" + Convert.ToInt32(TxtNetPrice.Text) + "');" + " SELECT SCOPE_IDENTITY();";
SqlCommand command = new SqlCommand(Orders, Connection);
command.CommandType = CommandType.Text;
Connection.Open();
int intID = Convert.ToInt32(command.ExecuteScalar());
String Orderdetails1 = "INSERT INTO OrderDetails VALUES(" + intID + ",'" + DDLProduct1.SelectedItem + "','" + Convert.ToInt32(TxtPrice1.Text) + "','" + Convert.ToInt32(TxtQuantity1.Text) + "','" + Convert.ToInt32(TxtTotalPrice1.Text) + "')";
SqlCommand Command1 = new SqlCommand(Orderdetails1, Connection);
Command1.CommandType = CommandType.Text;
Command1.ExecuteNonQuery();
String Orderdetails2 = "INSERT INTO OrderDetails VALUES(" + intID + ",'" + DDLProduct2.SelectedItem + " ','" + Convert.ToInt32(TxtPrice2.Text) + "','" + Convert.ToInt32(TxtQuantity2.Text) + "','" + Convert.ToInt32(TxtTotalPrice2.Text) + "')";
SqlCommand Command2 = new SqlCommand(Orderdetails2, Connection);
Command2.CommandType = CommandType.Text;
Command2.ExecuteNonQuery();
String Orderdetails3 = "INSERT INTO OrderDetails VALUES(" + intID + ",'" + DDLProduct3.SelectedItem + " ','" + Convert.ToInt32(TxtPrice3.Text) + "','" + Convert.ToInt32(TxtQuantity3.Text) + "','" + Convert.ToInt32(TxtTotalPrice3.Text) + "')";
SqlCommand Command3 = new SqlCommand(Orderdetails3, Connection);
Command3.CommandType = CommandType.Text;
Command3.ExecuteNonQuery();
Response.Write("<script>alert('Successfully Inserted');</script>");
Connection.Close();
如何为此c#语句创建存储过程 我在SQL SERVER中创建的表是
CREATE TABLE Customers
(
CustomerID INT IDENTITY(1, 1) PRIMARY KEY,
FirstName NVARCHAR(45),
LastName NVARCHAR(45),
Address NVARCHAR(45)
)
CREATE TABLE Orders
(
OrderID INT IDENTITY(1, 1) PRIMARY KEY,
CustomerID INT FOREIGN KEY REFERENCES Customers(CustomerID),
TotalPrice INT
)
CREATE TABLE OrderDetails
(
OrderID INT FOREIGN KEY REFERENCES Orders(OrderID),
ProductName NVARCHAR(45),
Quantity NVARCHAR(45),
Price NVARCHAR(45),
TotalPrice INT
)
我想为此语句创建存储过程
答案 0 :(得分:0)
这是您需要编写的SQL代码:
存储过程的语法:
Create Proc Proc_Name
(
@PARAM1 DATATYPE,
....
)
AS
BEGIN
BODY OF THE SP
END
这是在SQL中创建存储过程的方法。
因为你有大量的查询,你正在一次性执行。
您还可以处理您的交易。
如果假设您的上一个插入查询得到EXCEPTION
,那么您首先需要回滚所有查询&amp;不应该被执行。
我还在存储过程中完成了回滚。
CREATE PROC Procedure_Name
(
@Customer_id INT,
@TxtNetPrice FLOAT,
@DDLProduct1 INT,
@TxtQuantity1 FLOAT,
@TxtTotalPrice1 FLOAT,
@TxtPrice1 FLOAT,
@intID INT
)
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Orders VALUES(@Customer_id,@TxtNetPrice)
DECLARE @S_ID int;
SET @S_ID = (SELECT SCOPE_IDENTITY())
INSERT INTO
OrderDetails
VALUES(@intID,@DDLProduct1,@TxtPrice1,@TxtQuantity1,@TxtTotalPrice1)
COMMIT
END TRY
BEGIN CATCH
DECLARE @ERROR_MSG NVARCHAR(MAX), @SEVERITY INT, @STATE INT
SELECT @SEVERITY = ERROR_SEVERITY(), @STATE = ERROR_STATE()
, @ERROR_MSG = ERROR_MESSAGE() + ' err src line: ' + CAST( ERROR_LINE() AS NVARCHAR(20)) + ' ' + ISNULL(ERROR_PROCEDURE(), '');
--@ERROR_MSG = ERROR_MESSAGE()
ROLLBACK;
-- RE-THROW EXCEPTION FOR DIAGNOSTIC VISIBILITY
RAISERROR (@ERROR_MSG ,@SEVERITY, @STATE);
RETURN
END CATCH
END