转换为存储过程

本文关键字:存储过程 转换 | 更新日期: 2023-09-27 18:25:03

如何将其保存到存储过程中?并在c#中运行存储过程?

string a = "Select * from EmpTable Where EmpName Like @SearchItem";
using (SqlCommand SqlCommand = new SqlCommand(" "+ a +" ", myDatabaseConnection))
{
     SqlCommand.Parameters.AddWithValue("@SearchItem", "%" + textBox1.Text + "%");
}

转换为存储过程

存储过程:

CREATE PROCEDURE SearchSP
 @SearchItem nvarchar(MAX)
AS
BEGIN
    Select * 
    from EmpTable 
    Where EmpName Like '%' + @SearchItem + '%'
END
GO

代码:

using (SqlCommand cmd= new SqlCommand("SearchSP", myDatabaseConnection))
{
     cmd.CommandType = CommandType.StoredProcedure;
     cmd.Parameters.AddWithValue("@SearchItem", textBox1.Text);
     //rest of the code 
}

你的说法是,你希望字符串消失,并调用一个包含此SQL代码的存储过程?如果是这样的话。。

假定SQL server 2008管理工作室。。

在management studio中,展开"数据库",然后展开"可编程性"。。。右键单击"创建新存储过程"并删除其中的所有代码。

然后键入以下内容。

USE [YourDataBaseName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[AnyName] 
@SearchItem as nvarchar(MAX)
AS
Select * from EmpTable Where EmpName Like @SearchItem

单击run/execute并刷新数据库,如果查看存储的proc文件夹,应该会看到新的存储过程。

然后在您的(假定C#.Net)代码中。

public string GetUserFinal(string _userInput)
{
    string temp = "";
    using (SqlConnection myConn = new SqlConnection(Conn))
    {
        using (SqlCommand myCommand = new SqlCommand())
        {
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "dbo.your procedure name";
            myCommand.Parameters.AddWithValue("@SearchItem", _userInput);
            SqlDataReader reader = myCommand.ExecuteReader();
            myCommand.Connection = myConn;
            While (reader.read())
            {
                //populate a string? or do something else?
            }
        }
    }
    return temp;
}

一个更好的方法是强烈地键入所有这些内容。所以我会创建一个模型,不管你现在的字段和类型是什么,然后创建一个列表,并迭代该列表,从这个函数绑定。但是,正如您所看到的,只要检查语法就可以了。

创建一个存储过程,如下所示:

Create Procedure dbo.Test
@SearchItem
AS
BEGIN
BEGIN TRY
IF EXISTS(SELECT 1 FROM dbo.Table WHERE EmpName LIKE '%'+@SearchItem+'%')
      SELECT * FROM dbo.Table WHERE EmpName LIKE '%'+@SearchItem+'%'
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS 'ErrorNumber', ERROR_MESSAGE() AS 'ErrorMessage', ERROR_SEVERITY() AS 'ErrorSeverity', ERROR_STATE() AS 'ErrorState', ERROR_LINE() AS 'ErrorLine'
RETURN ERROR_NUMBER()
END CATCH
END

现在你可以在代码后面引用这个sp如下:

SqlConnection xconn=new SqlConnection(Write your connection string);
SqlCommand xcommand=new SqlCommand("Test",xconn);
xcommand.CommandType=CommandType.StoredProcedure;
xcommand.Parameters.AddWithValue("@SearchItem",DbType.String,txtBox1.Text);
xconn.Open();
xCommand.ExecuteNonQuery();
xconn.Close();

这将是您存储的过程(尚未测试代码):

CREATE PROCEDURE TestStoredProc 
    @SearchItem nvarchar(MAX)
AS
BEGIN
    SET NOCOUNT ON;
    Select * 
    from EmpTable 
    Where EmpName Like '%' + @SearchItem + '%'
END
GO

然后在代码端这样做:

using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand("TestStoredProc", conn) { 
CommandType = CommandType.StoredProcedure, Parameters.AddWithValue("@SearchItem",textbox.Text)}) {
}