asp.net連接mysql數據庫
首先需要一個MySql.Data.dll文件 點擊下載
1.創建一個ASP.NET空Web應用程序
2.添加引用
3.添加web窗體
4.在生成的代碼中引用using MySql.Data.MySqlClient;
5.連接mysql
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
namespace DataRefresh
{
public partial class WebForm1 : System.Web.UI.Page
{
MySqlConnection mySqlConn;//mysql連接對象
protected void Page_Load(object sender, EventArgs e)
{
string connStr = "Database=school;Data Source=localhost;User Id=root;Password=123";//連接字符串
mySqlConn = new MySqlConnection(connStr);
mySqlConn.Open();
bind();
}
public void bind()
{
//創建MySqlDataAdapter對象執行查詢
MySqlDataAdapter DataAdapter = new MySqlDataAdapter("select * from student", mySqlConn);
DataSet dataset = new DataSet();
// 填充DataSet對象
DataAdapter.Fill(dataset, "student");
//將數據顯示在gridview中
GridView1.DataSource = dataset;
GridView1.DataKeyNames = new string[] { "s_no" };//主鍵
GridView1.DataBind();
}
連接字符串:Database:數據庫的名字;DataSource:數據庫地址;User Id:用戶名;Password:密碼
bind()方法是將查詢結果顯示到gridview控件中(以後也能用於刷新)
aspx代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DataRefresh.WebForm1" %>
<!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 style="height: 34px">
<form id="form1" runat="server">
<div id="container">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Size="12px" Width="591px" CellPadding="4" ForeColor="#333333" GridLines="None"
OnRowDeleting="GridView1_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowCommand="GridView1_RowCommand"
OnRowEditing="GridView1_RowEditing">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="s_no" HeaderText="學號" />
<asp:BoundField DataField="s_name" HeaderText="名字" />
<asp:BoundField DataField="s_age" HeaderText="出生日期" DataFormatString="{0:d}" HtmlEncode="False" />
<asp:BoundField DataField="s_sex" HeaderText="性別" />
<asp:CommandField HeaderText="編輯" ShowEditButton="true"/>
<asp:CommandField HeaderText="刪除" ShowDeleteButton="true">
</asp:CommandField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Size="12px" HorizontalAlign="Center" Font-Bold="True" ForeColor="White" />
<RowStyle HorizontalAlign="Center" BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle HorizontalAlign="Center" BackColor="#284775" ForeColor="White" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
<p>
<asp:Button ID="Btn_add" runat="server" Text="添加" OnClick="Btn_add_Click" />
</p>
</form>
</body>
</html>
其中主要是gridview控件,因爲要將數據庫中的數據顯示出來,所以得根據數據來改變
這是我的查詢結果: