Examples of common ADO.net objects

Source: Internet
Author: User

Demo of commonly used ADO.net objects

This routine demonstrates the usage of OleDbConnection, OleDbCommand, cmd. Parameters, OleDbDataReader, OleDbDataAdapter, DataSet, and other objects. This is the result of several days of research.

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. Data. OleDb;
Using System. IO;
Using System. Diagnostics;

Namespace WindowsFormsApplication5
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

* ****** Void button#click (object sender, EventArgs e) //
{

OleDbConnection cn = new OleDbConnection ("Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ Application. startupPath + "\ flList. mdb "+"; Persist Security Info = True ");
Cn. Open ();

OleDbCommand cmd = new OleDbCommand ();
Cmd. Connection = cn;
Cmd. CommandText = "insert into list (linktxt, url, shopname, bz, banner, html) values (?,?,?,?,?,?) ";
Cmd. Parameters. Add ("linktxt", OleDbType. VarChar );
Cmd. Parameters ["linktxt"]. Value = "的 ";
Cmd. Parameters. Add ("url", OleDbType. VarChar );
Cmd. Parameters ["url"]. Value = "http://www.hackpig.cn ";
Cmd. Parameters. Add ("shopname", OleDbType. VarChar );
Cmd. Parameters ["shopname"]. Value = "yipinxiang ";
Cmd. Parameters. Add ("BZ", oledbtype. varchar );
Cmd. Parameters ["BZ"]. value = "yipinxiang ";
Cmd. Parameters. Add ("banner", oledbtype. varchar );
Cmd. Parameters ["banner"]. value = "banner ";
Cmd. Parameters. Add ("html", oledbtype. varchar );

Filestream FS = new filestream (application. startuppath + "\ html.txt", filemode. Open, fileaccess. Read );
Byte [] buffer = new byte [fs. Length];
Fs. Read (buffer, 0, buffer. Length );
Fs. Close ();
Cmd. Parameters ["html"]. Value = Encoding. Default. GetString (buffer );

Cmd. Parameters. Add ("babysum", OleDbType. VarChar );
Cmd. Parameters ["babysum"]. Value = 333;
Cmd. Parameters. Add ("sellerxy", OleDbType. VarChar );
Cmd. Parameters ["sellerxy"]. Value = 35;
Cmd. Parameters. Add ("mjxy", OleDbType. VarChar );
Cmd. Parameters ["mjxy"]. value = 11;
Cmd. Parameters. Add ("shopdate", oledbtype. varchar );
Cmd. Parameters ["shopdate"]. value = "1988-11-13 ";
Cmd. Parameters. Add ("scrq", oledbtype. varchar );
Cmd. Parameters ["scrq"]. value = 41;
Cmd. Parameters. Add ("service", oledbtype. varchar );
Cmd. Parameters ["service"]. value = true;
Cmd. Parameters. Add ("wherefrom", oledbtype. varchar );
Cmd. Parameters ["wherefrom"]. value = "Wuhan, China ";
Cmd. Parameters. Add ("modulehtml", oledbtype. varchar );
Cmd. Parameters ["modulehtml"]. value = "modulehtml ";

Cmd. executenonquery ();
CN. Close ();
}

P rivate void button2_click (Object sender, eventargs E)
{

Oledbconnection Cn = new oledbconnection ("provider = Microsoft. jet. oledb.4.0; Data Source = "+ application. startuppath + "\ fllist. mdb "+"; persist Security info = true ");
CN. open ();

Oledbcommand cmd = new oledbcommand ();
Cmd. Connection = cn;
Cmd. commandtext = "select * from List ";

// Oledbdatareader the data reader is read-only
Oledbdatareader reader = cmd. executereader ();

While (reader. Read ())
{
// Debug. writeline (Reader [1]. tostring ());
Richtextbox1.text + = reader [0]. tostring ();
Richtextbox1.text + = "\ n ";
Richtextbox1.text + = reader [1]. tostring ();
Richtextbox1.text + = "\ n ";
RichTextBox1.Text + = reader [2]. ToString ();
RichTextBox1.Text + = "\ n ";
RichTextBox1.Text + = reader [3]. ToString ();
RichTextBox1.Text + = "\ n ";

}
Reader. Close ();

}

P rivate void button3_Click (object sender, EventArgs e)
{// DataBindings
Oledbconnection Cn = new oledbconnection ("provider = Microsoft. jet. oledb.4.0; Data Source = "+ application. startuppath + "\ fllist. mdb "+"; persist Security info = true ");
CN. open ();
Oledbcommand cmd = new oledbcommand ();
Cmd. Connection = cn;

Oledbdataadapter adapter1 = new oledbdataadapter ("select top 1 * From list", CN );
// Adapter1.selectcommand = "select * from List ";
Dataset dataset1 = new dataset ();

Adapter1.fill (dataset1, "list ");
Textbox1.databindings. Add ("text", dataset1, "list. BZ ");

 

}

P rivate void button4_click (Object sender, eventargs E)
{
// Read dataset data
Oledbconnection Cn = new oledbconnection ("provider = Microsoft. jet. oledb.4.0; Data Source = "+ application. startuppath + "\ fllist. mdb "+"; persist Security info = true ");
CN. open ();
OleDbCommand cmd = new OleDbCommand ();
Cmd. Connection = cn;

OleDbDataAdapter adapter1 = new OleDbDataAdapter ("select top 1 * from list", cn );
// Adapter1.SelectCommand = "select * from list ";
DataSet dataSet1 = new DataSet ();

Adapter1.Fill (dataSet1, "list ");
Debug. WriteLine (dataSet1.Tables [0]. Rows. Count );
String str1 = null;
DataTable table = dataSet1.Tables [0];
Foreach (DataRow row in table. Rows)
{
Foreach (DataColumn column in table. Columns)
{
Str1 + = row [column];
// Debug. WriteLine (str1 );
Str1 + = "\ n ";
}
Str1 + = "\ n ";
}
Richtextbox1.text = str1;

}
}
}
 

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.