一、經過Visual Stdio新建.net項目html
(1)新建項目數據庫
(2)選擇項目配置ide
(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"; } } }
測試:ui
二、建立ODBC數據源spa
(1)下載ODBC數據源.net
(2)配置ODBC數據源設計
添加相關的信息後點擊測試:3d
三、.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>
方式二:
在配置文件中書寫數據源有關的配置
<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;
Initial Catalog=aspnet-Sanlogic.MSTWebsite-20140219093639;
Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Sanlogic.MSTWebsite-20140219093639.mdf" providerName="System.Data.SqlClient" /> <add name="DBConnection" connectionString="server=localhost; user id=root; password=root; database=student; pooling=true;" providerName="MySql.Data.MySqlClient" /> </connectionStrings>
經過讀取配置文件來獲取鏈接