Small Example of datareader application (the basic process of database access operations is 2.0 passed)

Source: Internet
Author: User

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 ();

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.