欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

asp.net web项目连接mysql数据库

程序员文章站 2022-07-11 08:27:21
...

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控件,因为要将数据库中的数据显示出来,所以得根据数据来改变

 

这是我的查询结果:

asp.net web项目连接mysql数据库

https://blog.csdn.net/u010940770/article/details/14455987?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-4&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-4

 

相关标签: WEB SERVER