How to Connect MySql for Windows Store Apps

MySql Connectivity for Windows Store Apps:

1.Open Visual Studio 2012

2.Click on  file-> new-> Project->Visual c#->windows store
MySQL-FOR-WINDOWS-STORE-APPS
3.Open Tools->LibraryPackageManager ->Manage NuGet Packages for Solution



MySQL-FOR-WINDOWS-STORE-APPS
4.Install MySql.Data


         If u installed successfully,you will find MySql reference in references as follows


MySQL-FOR-WINDOWS-STORE-APPS

5.Create Table in MySql using MySql console.
          create table sample(name varchar(10),city varchar(10));

6.Create User Interface as follows

MySQL-FOR-WINDOWS-STORE-APPS














7.Open Main.xaml.cs file



   Add Namespace using MySql.Data.MySqlClient;

8.Connection creation step
   
     Establish Connection using following code

      string constring = "SERVER=localhost;DATABASE=db;UID= root; PASSWORD =;";
            MySqlConnection con = new MySqlConnection(constring);

9.For Insertion write Following code:

            try
            {

                MySqlCommand cmd = con.CreateCommand();
                cmd.CommandText = "insert into sample (name,city) values ('" + name_tb.Text + "','" + city_tb.Text + "');";
                con.Open();
                cmd.ExecuteNonQuery();
            }

Main.xaml and Main.xaml.cs files are given below

Main.xaml:
<Page
    x:Class="Database.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Database"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <TextBox HorizontalAlignment="Left" Margin="618,121,0,0" TextWrapping="Wrap" Text="" Name="name_tb" FontSize="42" VerticalAlignment="Top" Width="336" Height="59"/>
        <TextBox HorizontalAlignment="Left" Margin="618,235,0,0" TextWrapping="Wrap" Text="" Name="city_tb" FontSize="42" VerticalAlignment="Top" Width="336" Height="55"/>
        <TextBlock Name="name_tblk" FontSize="42" HorizontalAlignment="Left" Margin="367,121,0,0" TextWrapping="Wrap" Text="Name:" VerticalAlignment="Top" Height="59" Width="152"/>
        <TextBlock Name="city_tblk" FontSize="42" HorizontalAlignment="Left" Margin="367,235,0,0" TextWrapping="Wrap" Text="City:" VerticalAlignment="Top" Height="55" Width="152"/>
        <Button Content="Submit" Name="submit_but" HorizontalAlignment="Left" Margin="722,358,0,0" VerticalAlignment="Top" Height="63" Width="232" Click="submit_but_Click"/>
        <TextBlock FontSize="48" Name="status" HorizontalAlignment="Left" Margin="127,539,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="1145" Height="107"/>

    </Grid>
</Page>





Main.xaml.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using MySql.Data.MySqlClient;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Database
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void submit_but_Click(object sender, RoutedEventArgs e)
        {
            string constring = "SERVER=localhost;DATABASE=db;UID= root; PASSWORD =;";
            MySqlConnection con = new MySqlConnection(constring);
            try
            {

                MySqlCommand cmd = con.CreateCommand();
                cmd.CommandText = "insert into sample (name,city) values ('" + name_tb.Text + "','" + city_tb.Text + "');";
                con.Open();
                cmd.ExecuteNonQuery();
                status.Text = "Inserted";
            }
            finally
            {
                con.Close();
            }
        }
    }
}

Try this and Check the inserted rows using MySql consoleHow to retrieve and update the data from MySql database will be Updated Soon......

Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment