1、通過Visual Stdio新建.net項目
(1)新建項目
(2)選擇項目配置
(3)項目結構
(4)新建一個Controller,名稱要取HomeController
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace WebApplication1.Controllers { public class HomeController : Controller { // GET: Home public String Index() { return "Hello world"; } } }
測試:
2、創建ODBC數據源
(1)下載ODBC數據源
(2)配置ODBC數據源
添加相關的信息后點擊測試:
3、.net連接MySQL數據庫
方式一:
(1)添加引用
(2)添加一個Web窗體
(3)設計頁面
選擇工具:
(4)連接數據源(該數據源是前面已經配置了的數據源)
雙擊SQL數據庫
(5)測試
(6)運行生成的頁面即可
(7)生成的代碼如下
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="students.aspx.cs" Inherits="WebApplication1.students" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="stu"> <Columns> <asp:BoundField DataField="studentno" HeaderText="studentno" InsertVisible="False" SortExpression="studentno" /> <asp:BoundField DataField="birthday" HeaderText="birthday" SortExpression="birthday" /> <asp:BoundField DataField="classno" HeaderText="classno" SortExpression="classno" /> <asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" /> <asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" /> <asp:BoundField DataField="sname" HeaderText="sname" SortExpression="sname" /> <asp:BoundField DataField="score" HeaderText="score" SortExpression="score" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="stu" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="select *from student"> </asp:SqlDataSource> </div> </form> </body> </html>
方式二:
在程序中進行書寫獲取數據庫連接相關的代碼,獲取到數據庫連接並操作數據庫
(1)引用MySql.Data
(2)書寫代碼獲取數據庫連接,操作數據庫
namespace WebApplication1.Controllers { public class HomeController : Controller { public MySqlDataReader Index() { String constr = "server=127.0.0.1;user=root;password=root; database=student;"; MySqlConnection mycn = new MySqlConnection(constr); try { mycn.Open(); Console.WriteLine("已經建立連接"); } catch (MySqlException ex) { Console.WriteLine(ex.Message); } MySqlCommand mycm = new MySqlCommand("select * from student", mycn); MySqlDataReader dr = mycm.ExecuteReader(); while (dr.Read()) { if (dr.HasRows) { Response.Write(dr.GetString("sname") + "<br/>"); } } mycn.Close(); return dr; } } }
(3)測試
方式三:
(1)書寫連接數據庫的配置文件
<connectionStrings> <add name="DBConnection" connectionString="server=localhost; user id=root; password=root; database=student; pooling=true;" providerName="MySql.Data.MySqlClient" /> </connectionStrings>
(2)書寫獲取數據庫連接的工具類
namespace WebApplication1.utils { public class ConnectionUtils { public static MySqlConnection CreateConn() { string _conn = WebConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString; MySqlConnection conn = new MySqlConnection(_conn); return conn; } } }
(3)書寫測試類
namespace WebApplication1.Controllers { public class HomeController : Controller { public MySqlDataReader Index() { MySqlConnection mycn = ConnectionUtils.CreateConn(); try { mycn.Open(); Console.WriteLine("已經建立連接"); } catch (MySqlException ex) { Console.WriteLine(ex.Message); } MySqlCommand mycm = new MySqlCommand("select * from student", mycn); MySqlDataReader dr = mycm.ExecuteReader(); while (dr.Read()) { if (dr.HasRows) { Response.Write(dr.GetString("sname") + "<br/>"); } } mycn.Close(); return dr; } } }
(4)測試