asp.net利用存储过程实现模糊查询示例分享
USE[TestDB]
GO
/******Object: Table[dbo].[tblCustomer] ScriptDate:01/18/201422:01:53******/
SETANSI_NULLSON
GO
SETQUOTED_IDENTIFIERON
GO
CREATETABLE[dbo].[tblCustomer](
[id][int]IDENTITY(1,1)NOTNULL,
[name][nvarchar](100)NULL,
[dat][date]NULL
)ON[PRIMARY]
GO模糊查询
CREATEPROCEDURESearchCustomer
--Addtheparametersforthestoredprocedurehere
@namenvarchar(100)
AS
SELECT*FROMdbo.tblCustomerWHEREnameLIKE"%"+@name+"%"
GO
using(SqlConnectioncn=newSqlConnection("Server=localhost;Database=TestDB;Trusted_Connection=True;"))
{
cn.Open();
stringstr="关键字";
//str=null;
SqlCommandcmd=newSqlCommand("SearchCustomer",cn);
cmd.CommandType=CommandType.StoredProcedure;
DataTabledt=newDataTable();
SqlDataAdapterda=newSqlDataAdapter(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();
}
相关文章