【C#與MYSQL】在VS中使用MYSQL connect Net 串連本地MYSQL

來源:互聯網
上載者:User

【步驟描述】

1、建立VC工程,把MYSQL裝好,資料準備好

2、安裝好MYSQL  connect Net 

3、在工程中引用MYSQL資料庫物件

4、在代碼中寫串連和查詢的語句


【在工程中引用MYSQL資料庫物件】




【在代碼中寫串連和查詢的語句】

 

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using MySql.Data.MySqlClient;//使用串連和操作MySQL的方法,必須應用MySql.Data.MySqlClientnamespace FirstMVC2Prj.Controllers{    [HandleError]    public class HomeController : Controller    {        public ActionResult Index()        {            String myInfo = "歡迎使用 ASP.NET MVC! ";            ///////////////////擷取MYSQ看資料傳回值////////////////////////////            MySqlConnection myconn = null;            MySqlCommand  mycom = null;            MySqlDataReader myrec = null;            //連接字串拼裝            myconn = new MySqlConnection("Host = localhost;Database = test;Username = root;Password = fulei");            //串連            myconn.Open();            if(myconn.State.ToString()=="Open")            {                myInfo = myInfo + "<br> 串連MYSQL成功";            }                        //查詢命令賦值,可以寫多條語句,多條語句之間用;號隔開            mycom = new MySqlCommand("select * from persioninfo",myconn);            myrec = mycom.ExecuteReader();            //一次次讀,讀不到就結束            while (myrec.Read())            {                myInfo = myInfo + myrec["name"] + " " + myrec["sex"];            }            //////關閉相關對象            myrec.Close();            myconn.Close();            /////////////////////擷取MYSQ看資料傳回值////////////////////////////             ViewData["Message"] = myInfo;            return View();        }        public ActionResult About()        {            return View();        }    }}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.