A simple query result is used.
Sqlconnection
Sqlcommand
Sqldatareader
Table: tables
Stored Procedure: getproduct
==========================================================
Create procedure getproduct
(
@ Strname varchar (50)
)
As
Select @ strname = rtrim (@ strname) + '%'
Select sellid, productname, unitcost, number
From nation
Where (productname like @ strname)
Return
Go
========================================================== =
Flowchart when using datareader to process databases
1) Use the sqlcommand object to execute SQL statements
2) Place the data read by sqlcommand in datareader
3) process and display datareader data
Code:
<% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " Conn1.aspx. CS " Inherits = " Conn1 " %>
<! Doctype HTML public " -// W3C // dtd xhtml 1.0 transitional // en " " Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< HTML xmlns = " Http://www.w3.org/1999/xhtml " >
< Head runat = " Server " >
< Title > No title page </ Title >
</ Head >
< Body >
< Form ID = " Form1 " Runat = " Server " >
< Div >
& Nbsp; < ASP: textbox ID = " Textbox1 " Runat = " Server " > </ ASP: textbox >
< ASP: button ID = " Button1 " Runat = " Server " Onclick = " Button#click " Text = " Search " />
< BR />
< BR />
< ASP: Label ID = " Label1 " Runat = " Server " > </ ASP: Label > </ Div >
</ Form >
</ Body >
</ Html >
CS code:
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Public partial class conn1: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
Protected void button#click (Object sender, eventargs E)
{
Label1.text = ""; // clear the result
Sqlconnection myconn = new sqlconnection ("Server = localhost; database = onnetbooksell; uid = sa; Pwd = 123 ");
// Define command
Sqlcommand mycmd = new sqlcommand ();
Mycmd. Connection = myconn;
Mycmd. commandtype = commandtype. storedprocedure;
Mycmd. commandtext = "getproduct ";
// Add a parameter
Sqlparameter sqlparam = new sqlparameter ("@ strname", sqldbtype. nvarchar, 50 );
Sqlparam. value = textbox1.text;
Mycmd. Parameters. Add (sqlparam );
Myconn. open ();
// Execute and display
Sqldatareader myreader = mycmd. executereader ();
While (myreader. Read ())
{
Label1.text + = myreader ["productname"];
Label1.text + = "<br> ";
}
Myconn. Close ();
Myreader. Close ();
}
}