Yaz  Font K   lt Yaz  Font B y lt

Web.Config Dosyasına MySql Database Bağlantısını Ekleyerek GridView Nesnesinde My Sql Database deki Verileri Göstermek

 

Merhaba arkadaşlar bu makalemizde Web config dosyasında connectionString kısmına Mysql bağlantı komut satırını ekleyeceğiz. Web config e yazdığımız bu komut satırıyla mysql veritabanına bağlantımızı gerçekleştirip, verileri gridview da gösterimini sağlayacağız. 

 

 

Resim1

Şekil 1

 

Resim2

Şekil 2

 

WebForm1.aspx.cs

 

using MySql.Data.MySqlClient;

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace gridview_mysql_webconfig

{ 

        public partial class WebForm1 : System.Web.UI.Page

    {

        MySqlDataAdapter da;

        MySqlCommand cmd;

        DataTable dt;

        protected void Page_Load(object sender, EventArgs e)

        {

            MySqlConnection con = new MySqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["MySqlConnectionString"].ConnectionString);

 

            string sql = "Select * From worldclassic";

 

            da = new MySqlDataAdapter(sql, con);

            dt = new DataTable();

 

            con.Open();

 

            da.Fill(dt);

 

            con.Close();

 

            GridView1.DataSource = dt;

 

            GridView1.DataBind();

 

        }

    }

}

 

 

WebForm1.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="gridview_mysql_webconfig.WebForm1" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" Font-Names="Segoe UI" Font-Size="Large" ForeColor="Black" GridLines="Vertical" Height="415px" Width="546px">

                <AlternatingRowStyle BackColor="White" />

                <FooterStyle BackColor="#CCCC99" />

                <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />

                <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />

                <RowStyle BackColor="#F7F7DE" />

                <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />

                <SortedAscendingCellStyle BackColor="#FBFBF2" />

                <SortedAscendingHeaderStyle BackColor="#848384" />

                <SortedDescendingCellStyle BackColor="#EAEAD3" />

                <SortedDescendingHeaderStyle BackColor="#575357" />

            </asp:GridView>

        </div>

    </form>

</body>

</html>

 

Web.Config

 

<?xml version="1.0" encoding="utf-8"?>

<!--

  For more information on how to configure your ASP.NET application, please visit

   https://go.microsoft.com/fwlink/?LinkId=169433

  -->

<configuration>

  <system.web>

    <compilation debug="true" targetFramework="4.7.2" />

    <httpRuntime targetFramework="4.7.2" />

  </system.web>

 

<connectionStrings>

<add name="MySqlConnectionString" connectionString="Server=localhost;Database=mysql_db;Uid=root;Pwd='2344*';AllowUserVariables=True;UseCompression=True;"

providerName="MySql.Data.MySqlClient" />

</connectionStrings>

       

  <system.codedom>

    <compilers>

      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />

      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />

    </compilers>

  </system.codedom>

  <runtime>

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

      <dependentAssembly>

        <assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />

        <bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />

      </dependentAssembly>

      <dependentAssembly>

        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />

        <bindingRedirect oldVersion="0.0.0.0-6.0.1.0" newVersion="6.0.1.0" />

      </dependentAssembly>

      <dependentAssembly>

        <assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />

        <bindingRedirect oldVersion="0.0.0.0-4.0.4.0" newVersion="4.0.4.0" />

      </dependentAssembly>

    </assemblyBinding>

  </runtime>

</configuration>

 

 

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN