VS2010 connecting the MySQL code section

Source: Internet
Author: User
Tags mysql code

A database is like a water source, storing a lot of data. The connection is like a faucet that extends into the water and is connected to it. The command is like a pump that provides power and execution for pumping, and then returns the water to the pipe above. DataAdapter through the engine and sends water to the reservoir. DataReader the water to the user. The DataSet object is a large reservoir, even if disconnected, the water still exists. The DataTable object is a separate pool in the reservoir.

1, first establish a connection with the database

<span style= "FONT-SIZE:18PX;" ></span><pre name= "code" class= "CSharp" >mysqlconnection mycon=new mysqlconnection ("Data Source= Localhost;initial Catalog=mysql; User Id=root; Password=root ");/* Creates a Connection object mycon. "Data source=localhost;initial catalog=mt; User Id=root; Password=root "is the information that connects the database. Data Source is host name, Initial catalog database name, User ID username, PassWord password */mycon. Open ();//Opening database if (Mycon.state==connectionstate.open) {  }//Deciding whether to open the database

2, command operation to the database (there are two ways)

Mysqlcommand my_com = My_con. CreateCommand (); My_com.commandtext = sqlstr;//Execute the dsql statement?/* Creates a new command object under the Mycon connection. */mysqlcommand my_com = new Mysqlcommand (sqlstr, My_con);

The command object has two methods of executing the SQL statement, ExecuteNonQuery (), executing the SQL statement, and returning the number of rows affected, when sending an add, delete, or change command. ExecuteReader (), executes the SQL statement, and generates a Mysqldatareader instance that contains the data, sending a query command.

3.DataReaderObject

<span style= "FONT-SIZE:18PX;" >mysqldatareader My_read = my_com. ExecuteReader (); the ExecuteReader () method of the/*command object can generate DataReader objects. If you want to read the data, you can pass the read () method. The Read () method advances the DataReader object to the next record */while (My_read. Read ())  {       if (my_read[1]. ToString ()! = "" && my_read[1]. ToString ()!=null)       Cobox. Items.Add (My_read[1]. ToString ());  } </span>

4. DataAdapter Object

<span style= "FONT-SIZE:18PX;" >mysqldataadapter da = new Mysqldataadapter ("SELECT * from Logontable", mycon);//Create another method is </span>
<span style= "FONT-SIZE:18PX;" >mysqlcommand my_com = new Mysqlcommand (sqlstr, My_con); Mysqldataadapter da=new Mysqldataadapter ();d A. selectcommand= My_com;dataset mydataset = new DataSet ();//Create a Datasetda.fill (myDataSet); Data in//dataadapter populate dataset To send water from the water to the reservoir for preservation. DataTable TBL = mydataset.tables[0];//Gets the contents of the table in the database </span>


Using system;using system.collections.generic;using system.linq;using system.text;using MySql.Data.MySqlClient;             Using mysql.data;using System.data;namespace lianxi{class Program {static void Main (string[] args) { Mysqlconnection mycon = new Mysqlconnection ("Data source=localhost;initial catalog=mt; User id= Rootm;            Password=rootm "); Mycon.           Open ();           Mysqlcommand mycmd = new Mysqlcommand ("INSERT INTO mytable (id,name) VALUES (' 2 ', ' Zhao ')", mycon); if (myCMD.           ExecuteNonQuery () > 0) {Console.WriteLine ("Data card inserted successfully");            } mysqldataadapter da = new Mysqldataadapter ("SELECT * FROM MyTable", mycon);//Create a Mysqldataadapter object DataSet myDataSet = new DataSet ();//Create a DataSet da.           Fill (mydataset); data in//dataadapter populate a dataset that sends water from the water to the reservoir to save the DataTable TBL = mydataset.tables[0];//Gets the first table foreach (DataColumn col in tbl. Columns) {ConsolE.writeline (Col. ColumnName);//print column name} DataRow irow = tbl.                              rows[0];//gets the first line of information Console.WriteLine (irow["Name"]);//print the information for each column in the first row Console.WriteLine (irow["Pass"]);           Mysqlcommand msc = new Mysqlcommand ("SELECT * FROM MyTable", mycon); Mysqldatareader MDR = MSC. ExecuteReader ();//executes the SQL statement and generates an instance of the Mysqldatareader object that contains the data mysqldatareader from the reservoir to the user's water pipe while (MDR. Read ())//print out the user name and password for each user {Console.WriteLine (mdr["name"].               ToString ()); Console.WriteLine (mdr["Pass").           ToString ()); }                    }    }}

VS2010 connecting the MySQL code section

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.