asp.net web项目连接mysql数据库
2013-11-07 19:24
323 查看
asp.net连接mysql数据库
首先需要一个MySql.Data.dll文件
点击下载
1.创建一个ASP.NET空Web应用程序
2.添加引用
3.添加web窗体
4.在生成的代码中引用using MySql.Data.MySqlClient;
5.连接mysql
连接字符串:Database:数据库的名字;DataSource:数据库地址;User Id:用户名;Password:密码
bind()方法是将查询结果显示到gridview控件中(以后也能用于刷新)
aspx代码
其中主要是gridview控件,因为要将数据库中的数据显示出来,所以得根据数据来改变
这是我的查询结果:
首先需要一个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控件,因为要将数据库中的数据显示出来,所以得根据数据来改变
这是我的查询结果:
相关文章推荐
- AspNetPager控件的简单使用
- ASP.NET MVC 检测开放重定向攻击方法
- ASP.Net中使用ClientID获取服务端控件ID
- ASP.NET状态管理的总结
- ASP.NET中URL Rewrite的具体实现方法
- ASP.NET学习路线图浅谈
- ASP.NET控件绑定数据源
- asp.net显示图片到指定的Image控件中 具体实现
- .net使用Aspose.Words进行Word替换操作的实现代码
- asp.net 生成验证码
- Telerik ASP.NET Ajax RadGrid - 如何实现双击行触发ItemCommand
- Asp.net 关于错误提示 类型“XXX1”在未被引用的程序集中定义,必须添加对程序集XXX2的引用
- 在ASP.Net中实现RSA加密的方法
- ASP.NET MVC3 HtmlHelper用法大全
- PDF在线预览 (flexpaper+swftools+saveaspdfandxps)
- ASP.NET实现页面传值的几种方法小结
- ASP.NET数据绑定技术
- Asp.net 配置web.Config 在出错时跳转到相应页面
- asp.net mvc 在 post 和 get 方式下的 ModelBinder
- OPENGL|ES第五天,Adjusting to the Screen’s Aspect Ratio