如何创建从Visual Studio应用程序将数据插入表中的存储过程

时间:2013-11-24 05:10:52

标签: c# sql sql-server stored-procedures

我给出的代码就是我现在在按钮单击事件中所拥有的代码,但不是直接输入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();

3 个答案:

答案 0 :(得分:3)

这是你需要做的。注意我没有测试过这个代码,但你应该关闭。您可能需要使用intellisense进行调整。

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。如果您点击执行,它将进行您所做的更改。

.NET代码中的

4)执行此操作:

    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做坏事)

我希望这有助于......祝你好运!

答案 1 :(得分:-1)

您只需通过在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

并且在您的应用程序中而不是执行insert(在command.text中),您可以使用以下命令调用您的过程:

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

答案 2 :(得分:-1)

您可以尝试以下方法之一

首先使用公共语言运行时集成创建并运行SQL Server存储过程,然后从应用程序执行此过程EXEC procedureName

使用sql server management studio创建存储过程,并执行应用程序中的过程,如

exec storedProcedureName

由于