2

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

string a = "Select * from EmpTable Where EmpName Like @SearchItem";

using (SqlCommand SqlCommand = new SqlCommand(" "+ a +" ", myDatabaseConnection))
{
     SqlCommand.Parameters.AddWithValue("@SearchItem", "%" + textBox1.Text + "%");
}
4

4 回答 4

0

你的意思是你想要字符串消失并调用一个存储过程而不是里面有这个SQL代码?如果是这样的话..

假设 SQL Server 2008 管理工作室..

在管理工作室展开你的“数据库”,然后展开“可编程性” ... 右键单击创建新的存储过程并删除里面的所有代码。

然后输入以下内容。

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

单击运行/执行并刷新您的数据库,如果您查看存储过程文件夹,您应该会看到您的新存储过程。

然后在您的(假定为 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;
}

更好的方法是强输入所有这些。所以我会创建一个模型,无论你现在的字段和类型是什么,然后创建一个列表并遍历该列表以从此函数绑定。但是正如您所看到的基础知识,这应该只检查语法即可。

于 2013-07-25T09:32:20.533 回答
0

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

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();
于 2013-07-25T09:33:45.657 回答
0

存储过程:

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 
}
于 2013-07-25T09:25:36.030 回答
-1

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

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)}) {

}
于 2013-07-25T09:17:22.010 回答