2014-11-05 109 views
0

如何創建存儲過程,這個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

你需要語法的SQL語句組成部分 – 2014-11-05 06:14:39

+0

是的,我想SQL語法部分@HardikParmar – JAB 2014-11-05 06:17:02

回答

0

這是您需要編寫的SQL代碼:

語法存儲過程:

Create Proc Proc_Name 
(
    @PARAM1 DATATYPE, 
    .... 
) 
AS 
BEGIN 
    BODY OF THE SP 
END 

這是建立在SQL存儲過程的方式。 因爲你有一堆查詢,你正在執行的單一鏡頭。 您還需要關注您的交易。 如果假設您的最後一個插入查詢得到了EXCEPTION那麼您首先需要回滾所有查詢&不應執行。 我也在存儲過程中做過回滾。

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(), '');   
       [email protected]_MSG = ERROR_MESSAGE() 
       ROLLBACK; 
      -- RE-THROW EXCEPTION FOR DIAGNOSTIC VISIBILITY 
       RAISERROR (@ERROR_MSG ,@SEVERITY, @STATE);   
       RETURN 

    END CATCH 

END 
+0

我想的OrderID在從「訂單」表@HardikParmar – JAB 2014-11-05 06:22:18

+0

@jabi「訂單明細」表顯示意味着你需要爲了從回此存儲過程要顯示訂單明細表我正確 – 2014-11-05 06:25:43

+0

它顯示錯誤@HardikParamar消息50000,級別16,狀態0,過程Orderssp,行37 INSERT語句與FOREIGN KEY約束「orderid_ref」衝突。衝突發生在數據庫「CustomersOrders」,表「dbo.Orders」,列'OrderID'中。 err src line:22 Orderssp – JAB 2014-11-05 06:29:59