asp.net利用存储过程实现模糊查询示例分享

网络编程 2021-07-04 22:40www.168986.cn编程入门
这篇文章主要介绍了asp.利用存储过程实现模糊查询的示例,大家参考使用吧

代码如下:

USE [TestDB]
GO

/ Object:  Table [dbo].[tblCustomer]    Script Date: 01/18/2014 22:01:53 /
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tblCustomer](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [name] [nvarchar](100) NULL,
 [dat] [date] NULL
) ON [PRIMARY]

GO模糊查询

代码如下:

CREATE PROCEDURE SearchCustomer
 -- Add the parameters for the stored procedure here
 @name nvarchar(100)

AS
 SELECT FROM dbo.tblCustomer WHERE name LIKE '%'+@name+'%'
GO

代码如下:

using (SqlConnection = new SqlConnection("Server=localhost;Database=TestDB;Trusted_Connection=True;"))
{
    .Open();
    string str = "关键字";
    //str = null;
    SqlCommand cmd = new SqlCommand("SearchCustomer", );
    cmd.CommandType = CommandType.StoredProcedure;
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.SelectCommand.Parameters.Add("@name", SqlDbType.NVarChar).Value = str;
    da.Fill(dt);
    Debug.Assert(dt.Rows.Count > 0);
    GridView1.DataSource=dt;
    GridView1.Bind();
    .Close();
}

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by