欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net利用存儲過程實現(xiàn)模糊查詢示例分享

 更新時間:2014年01月19日 15:09:41   作者:  
這篇文章主要介紹了asp.net利用存儲過程實現(xiàn)模糊查詢的示例,大家參考使用吧

復制代碼 代碼如下:

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 cn = new SqlConnection("Server=localhost;Database=TestDB;Trusted_Connection=True;"))
{
    cn.Open();
    string str = "關鍵字";
    //str = null;
    SqlCommand cmd = new SqlCommand("SearchCustomer", cn);
    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();
    cn.Close();
}

相關文章

最新評論