DataReader read data in a forward-only form, one at a time

Source: Internet
Author: User

DataReader Common Properties FieldCount get the number of fields isclosed get the state True or Falsitem ({name,ordinal}) Gets or sets the field contents, name is the field name, ordinal is the field ordinal, rec Ordsaffected gets how many rows are affected after an insert delete or update is performed DataReader common Method Close () closes Getboolean (ordinal) Gets the contents of the Ordinal+1 column, The return value is a Boolean type, with GetByte (ordinal), getdecimal (ordinal) ... Getdatatypename (ordinal) Gets the source data type name of column ordinal+1 Getfiletype (ordinal) Gets the data type of the ordinal+1 column getname (ordinal) gets ordinal+ 1 column Field name getordinal (name) Gets the field name of name of the field column number GetValue (ordinal) Gets the contents of the Ordinal+1 column getvalues (values) gets all the field contents, and put the content in the values array, the array size is equal to the number of fields, some more efficient than GetValue () IsDBNull (orderinal) to determine whether the ordinal+1 column is null, return Booleanread () read the next data, If it is not, it will return false, by default, the pointer is the use case above the first record: (Simple landing interface) using System;using system.collections.generic;using System.componentmodel;using system.data;using system.data.oledb;using system.drawing;using System.Text;using           System.windows.forms;namespace windowsapplication4{Publicpartial class Form1:form {public Form1 () {       InitializeComponent (); }//DefinitionLINK OleDbConnection Conn;       Define command OleDbCommand cmd;       Define DataReader OleDbDataReader myreader;       Defines two variables used to store database link strings and command strings, string connstr, Selectcmd; private void Button1_Click (object sender, EventArgs e) {connstr = "Provider=microsoft.jet.oledb.4.0;datas           Ource=db.mdb ";           Selectcmd = "SELECT * from Yonghu where username= '" + textbox1.text+ "' and password= '" + TextBox2.Text + "'";           Instantiate the link and open conn = new OleDbConnection (CONNSTR); Conn.           Open ();           Instantiate CMD and develop execution statement with execute link cmd = new OleDbCommand (SELECTCMD, conn); Execute the query command and assign to DataReader myreader = cmd.           ExecuteReader (); /* Determine if the data is actually read in myreader, that is, if the query command has a result DataReader the Read method can move the data pointer to the next record, because the data pointer of DataReader is just above the first one, So the first time you call the Read method, the pointer actually points to the first record */if (myreader.       Read ()) {//myreader has data in it indicating that the given user name and password have a matching record in the database MessageBox.Show ("login succeeded");        If the login is successful, remove the user name and level fill in the text box below TextBox3.Text = myreader["username"].               ToString (); Textbox4.text = myreader["level"].           ToString (); } else {//myreader has no data in it, indicating that at least one of the given user names and passwords is wrong MessageBox.Show ("Login failed!!           "); }

You should call the Close method every time you finish using the DataReader object.

If the Command contains output parameters or return values, these output parameters or return values cannot be accessed until DataReader is closed.

Note that when DataReader is turned on, the DataReader will use Connectionexclusively. You will not be able to execute any commands on Connection (including creating another DataReader) until the original DataReader is closed.

           myreader. Close ();           Conn. Close ();                  }       private void Button2_Click (object sender, EventArgs e)       {           application.exit ();}}    }

DataReader reads data in a forward-only format, one at a time

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.