C # Connect to an Access database, C # Connect to an Excel database, C # Connect SQL Server database, C # connect MySQL database summary

Source: Internet
Author: User

Sophomore writing next semester, now graduated one months, finishing

Well, the previous writing is very naïve, only self-understanding, online a lot of, probably the meaning of the next bar

mysql=========================================

http://user.qzone.qq.com/652768664/blog/1347859952

To connect MySQL needs to install MySQL, in the MySQL C-disk installation directory has a connection lib, will change the Lib Add reference with the project OK;

 static            void Main (string[] args) {list<string> lstdb =mysqlconn ();            foreach (string s in Lstdb) {Console.WriteLine (s);        } console.read (); } public static list<string> Mysqlconn () {list<string> lstconn = new list<string&            gt; ();            Mysqlconnection objconn = new Mysqlconnection (); objconn.connectionstring = "server=127.0.0.1; User Id=root; password=000000;            Persist Security info=true;database=test ";            objConn.Open ();            Mysqlcommand objcmd = new Mysqlcommand ("SELECT * from Maya", objconn);            Mysqldatareader dr = Objcmd.executereader (); while (Dr. Read ()) {Lstconn. ADD (Dr[0].            ToString ());            } objconn.close ();        return lstconn; }    }


access==========================================

http://user.qzone.qq.com/652768664/blog/1334636397

Access2007 and Access 2003 have different connection strings, one ADO version 12.0 and one is 4.0;

1,

 Public list<string> Access2007 (string accdbpath,string tablename) {list<string> Lstdata =            New List<string> ();            String strconn = @ "Provider=microsoft.ace.oledb.12.0;data source=" + Accdbpath + "'";            OleDbConnection conn = new OleDbConnection (strconn); Conn.            Open ();            String cmdtext = "SELECT * from" + tablename+ "";            OleDbDataAdapter apdate = new OleDbDataAdapter (CMDTEXT, conn);            DataTable table = new DataTable (); Apdate.            Fill (table); if (table. Rows.Count > 0) {for (int irow = 0; irow < table. Rows.Count; irow++) for (int icol = 0; icol < table. Columns.count; icol++) Lstdata. Add ("Row:" + IRow + "Column:" + Icol + "ColumnName:" + table. Columns[icol]. ColumnName + "Value:" + table. Rows[irow][table. Columns[icol]. CoLumnname]);        } return lstdata; }
2,

Namespace message Board _access database _{public partial class _default:system.web.ui.page {string strconnection = "Provid er=microsoft.jet.oledb.4.0;        Data source=f:\\ message board. mdb ";        string sex; protected void Page_Load (object sender, EventArgs e) {OleDbConnection coon = new OleDbConnection (Strcon            Nection); Coon.            Open (); Response.Write ("Access database connection Status display:" + coon.)            State); Coon.            Close ();            Label1.text=datetime.now.tostring ();            if (radiobutton1.checked) {sex = "male";}        else {sex = "female";}        } protected void button1_click (object sender, EventArgs e) {TextBox2.Text = ""; } public void Seek (string str) {using (OleDbConnection coon = new OleDbConnection (strconnection)                ) {OleDbCommand cmd = new OleDbCommand (str, coon); try {coon.                    Open (); OleDbDataReader R= cmd.                    ExecuteReader (); while (R.read ()) TextBox3.Text = string.                Format ("message person: {0}; Gender: {1}; message content: {2}; message time {3}", R[0], r[1], r[2],r[4]); } catch (Exception ee) {Response.Write (EE.                Message); }}} public void Insert (string str) {using (OleDbConnection coon = new OLE DB                Connection (strconnection)) {OleDbCommand cmd = new OleDbCommand (str, coon); try {coon.                    Open (); Cmd.                    ExecuteNonQuery (); Response.Write ("<script>window.alert (' data added successfully! ');                                    </script> "); } catch (Exception ee) {Response.Write (EE.                Message); }}} protected void Button2_Click (object sender, EventArgs e) {Seek ("Select* from message board WHERE name = ' "+ TextBox1.Text.Trim () +" ' "); } protected void Button3_Click (object sender, EventArgs e) {Insert ("INSERT into message board (name, gender, message content, left The words time) VALUES (' "+textbox1.text.trim () +" ', ' "+sex.            Trim () + "', '" +textbox2.text.trim () + "', '" +label1.text+ "')");        Button2_Click (sender,e); }    }}


excel==========================================

http://user.qzone.qq.com/652768664/blog/1350139919

Class Excelinandout
{
Reading an Excel document
</summary>
<param name= "Path" > File name </param>
<returns> Returns a dataset </returns>
Public DataSet exceltods (string Path)
{
String strconn = "provider=microsoft.jet.oledb.4.0;" + "Data source=" + Path + ";" + "Extended properties=excel 8.0;";
OleDbConnection conn = new OleDbConnection (strconn);
Conn. Open ();
String strexcel = "";
OleDbDataAdapter mycommand = null;
DataSet ds = null;
Strexcel = "SELECT * FROM [sheet1$]";
mycommand = new OleDbDataAdapter (Strexcel, strconn);
ds = new DataSet ();
Mycommand.fill (ds, "Table1");
return DS;
}
}

SQL Server and sqllite==========================================

A server name is. A server name of. \SQLExpress

Related Article

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.