簡體   English   中英

如何創建一個存儲過程,該存儲過程將數據從Visual Studio應用程序插入表中

[英]How to create a stored procedure that inserts data into a table from a Visual Studio Application

我所提供的代碼是按鈕單擊事件中現在擁有的代碼,但是我不需要使用直接輸入SQL語句的方式來使用存儲過程。 因此,基本上我需要將此代碼從Visual Studio 2010轉換為SQL Server 2008中的存儲過程。

command.Connection = conn;
        command.CommandType = CommandType.Text;
        command.CommandText = **"INSERT INTO Customer VALUES(" + txtCustID.Text + ",'"+        txtFirstName.Text + "', '" + txtSurname.Text + "', " + txtAge.Text + ")";**
        command.Connection.Open();

        adapter.InsertCommand = command;
        adapter.InsertCommand.ExecuteNonQuery();

        command.Connection.Close();
        Clear();

這是您需要做的。 注意我還沒有測試過此代碼,但是您應該很親近。 您可能需要使用智能感知進行調整。

1)在SQL Server Management Studio中,通過執行CTRL-N打開“新查詢編輯器”窗口

2)從以下開始每個步驟

CREATE Procedure ProcedureName

--Add Any Parameters that you will be passing in this way:
@parameterName datatype
--Add any additional parameters as needed by adding a comma at the end of the previous line
--And adding a new one : @parameterName datatype, @parameterName2 datatype

AS

BEGIN
--Here is where your write your stored procedure:
INSERT INTO Customer (custId, firstname, surname, age) 
VALUES(@custId,@firstname, @surname, @age)
END

3)在SQL Server Management Studio中,執行腳本。 現在,您的存儲過程已保存在SQL中。您始終可以使用exec sprocName parameter1, parameter 2...對其進行調用。 如果您需要再次將其拉回以進行編輯,只需進入Programmability > Stored Procedures ,找到該過程,然后右鍵單擊並MODIFY 如果您點擊執行,它將進行您的更改。

4)在您的.NET代碼中執行以下操作:

    command.Connection = conn;
    command.CommandType = CommandType.StoredProcedure; //Look up the actual code using intellisense as I dont recall what CommandType a stored Procedure is.
    command.paramaters.addwithvalue (@age, txtAge.text)  //using this as an example. Youw ill actually need to put in the same variables declared in SQL and where they correspond to on the screen.

 command.Connection.Open();

    adapter.InsertCommand = command;
    adapter.InsertCommand.ExecuteNonQuery();

    command.Connection.Close();
    Clear();

如果您注意到,則有一個parameters.addwithvalue部分。 這是將參數傳遞給存儲過程的最佳方法。 沒有理由擔心撇號(當然,您仍然需要逗號)。 它更加安全,並且將降低SQL Injection Attack風險(人們將代碼輸入到您的應用中並使之對SQL Server造成嚴重危害的一種方式)

希望這對您有幫助...祝您好運!

您只需在Sql MAnagement Studio的“新建查詢”窗口中編寫過程代碼即可創建存儲過程。 例如,您的代碼可能是:

    -- drop the procedure if it exists
    -- in order to recreate it
    if object_id('usp_newCustomer') is not null
        drop procedure usp_newCustomer
    go
    -- procedure new customer 
    create procedure usp_NewCustomer 
            -- parameters of the procedure
            @custID integer,
            @firstName nvarchar(32),
            @surName nvarchar(32),
            @age integer
    as
    -- code of the procedure
    begin
        -- insert into cutomer the values passed as parameters
        insert into Customer( custId, firstName, surName, age )
            values (@custId, @firstNAme, @surName, @age );
    end

在您的應用程序中,而不是執行插入(在command.text中),您可以使用以下命令來調用過程:

exec usp_NewCustomer 1, 'Johny', 'BeGood', 85

您可以嘗試以下方法之一

首先,使用“公共語言運行時集成”創建和運行SQL Server存儲過程,然后從應用程序中以EXEC procedureName身份執行此過程。

要么

而是使用sql Server Management Studio創建您的存儲過程,並從您的應用程序執行該過程,例如

exec storedProcedureName

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM