svigo
Posts: 2
Joined: Fri Aug 14, 2015 8:37 am

Raspi2 Win 10 IOT C# + MySql

Fri Aug 14, 2015 8:46 am

Hello to anyone,

Is it possible to run a C# project that connects to MySql DB with Raspi2 + Win10 IOT.

Which driver for MySql can connect should I use, since it is an ARM CPU.

Thanks for help

Svigo

User avatar
DougieLawson
Posts: 35347
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Raspi2 Win 10 IOT C# + MySql

Fri Aug 14, 2015 12:21 pm

The only possible way to connect would be with ODBC. I can't find any documentation on whether ODBC is supported in the Win10 IoT SDK. So you'll need to ask Microsoft for that.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

GerritV
Posts: 91
Joined: Fri May 01, 2015 4:16 pm
Location: St Catharines, ON
Contact: Website

Re: Raspi2 Win 10 IOT C# + MySql

Fri Aug 14, 2015 12:41 pm

There are a few choices, but ODBC seems your best option. https://dev.mysql.com/doc/connector-odb ... ation.html has information on building the connector from source.

UndergroundFun
Posts: 44
Joined: Wed Jun 17, 2015 12:57 pm

Re: Raspi2 Win 10 IOT C# + MySql

Fri Aug 14, 2015 1:16 pm

It might be simpler to just create a WebAPI service, or some intermediate layer to handle that rather than trying to connect directly.

Or depending on what you're doing with it, to provide a client that hits a web application.

dangfrick
Posts: 2
Joined: Sat Aug 15, 2015 1:08 am

Re: Raspi2 Win 10 IOT C# + MySql

Sat Aug 15, 2015 1:14 am

I'm not sure what your requirements are, but if you're open to it you could just use SQLite. There's a decent guide here:

http://blog.chrisbriggsy.com/Using-SQLI ... r-Preview/

Yacko1975
Posts: 1
Joined: Sat Aug 15, 2015 11:44 pm

Re: Raspi2 Win 10 IOT C# + MySql

Sat Aug 15, 2015 11:55 pm

I did this using the MySQL Connector for .Net located at https://dev.mysql.com/downloads/connector/net/

I downloaded the .Net & Mono Version and referenced the RT version in my Universal Windows C# application. I have MariaDB running on my Synology Diskstation and have it saving Tempatures every 15 minutes using the below function. This library does not support SSL so I had to disable it in my connection string which I have also included below.

ConnectionString

Code: Select all

private const string csMySQL = "Server=192.168.1.150;Database=IoTApps;Uid=IoTApplication;Pwd=xxxx;SslMode=None;";
Function to save to Mysql (MariaDB in my case)

Code: Select all

    public void InsertTemp(int LocationUID, double TempatureC)
    {
      using (MySqlConnection dbConn = new MySqlConnection(csMySQL))
      {
        using (MySqlCommand dbCmd = new MySqlCommand("App_Tempature_Insert", dbConn))
        {
          dbCmd.CommandType = CommandType.StoredProcedure;
          dbCmd.Parameters.Add("LocationUID", MySqlDbType.Int32).Value = LocationUID;
          dbCmd.Parameters.Add("TempatureC", MySqlDbType.Double).Value = TempatureC;
                    
          dbConn.Open();
          dbCmd.ExecuteNonQuery();

        }
      }

    }

svigo
Posts: 2
Joined: Fri Aug 14, 2015 8:37 am

Re: Raspi2 Win 10 IOT C# + MySql

Mon Aug 17, 2015 6:58 am

Thanks for answer, I will try and give feedback.

Best regards,
Švigo

ppumkin
Posts: 82
Joined: Tue May 29, 2012 10:22 pm

Re: Raspi2 Win 10 IOT C# + MySql

Tue Nov 03, 2015 11:32 am

Did this solution work?

ricl
Posts: 657
Joined: Wed Aug 26, 2015 11:55 am

Re: Raspi2 Win 10 IOT C# + MySql

Tue Nov 03, 2015 12:50 pm

There is also

47. Does Windows 10 Core IoT support SQL

A Developer's Guide to Windows 10: (10) SQLite Local Database
https://channel9.msdn.com/Series/A-Deve ... dows-10/10

Andy Wigley's Blog about it
http://andywigleyblog.azurewebsites.net/?p=721

and on GitHub
https://github.com/Windows-XAML/201505- ... SQLiteDemo
ricl : F/gamma = ma : Law ii(a) : https://climatedatablog.wordpress.com/2016/01/02/an-energy-challenge-2016/ #AnEnergyChallenge2016

mf1040
Posts: 1
Joined: Tue Nov 24, 2015 7:14 pm

Re: Raspi2 Win 10 IOT C# + MySql

Tue Nov 24, 2015 7:48 pm

Raspberry Pi 2- remote connection, recording on mySQL.db via the Internet

My example now works. Raspberry's IP address should be open to MySQL Server
Thanks for help

Mihael and Svigo

Code: Select all

// Copyright (c) Microsoft. All rights reserved.

using System;
using Windows.Devices.Gpio;
using TouchPanels.Devices;
//using System.Collections.Generic;
//using System.IO;
//using System.Linq;
//using System.Runtime.InteropServices.WindowsRuntime;
//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;
//using System.Data;
//using System.Data.Common;
//using System.Diagnostics;
using Windows.UI.Xaml.Media;
using System.Threading;
using Windows.Devices.I2c;


using System.Linq;
using System.Threading.Tasks;

using Windows.Foundation;

using Windows.UI.Xaml.Automation.Peers;
using Windows.UI.Xaml.Automation.Provider;
using Windows.UI.Xaml.Navigation;


namespace MySql_Connect
{

    public sealed partial class MainPage : Page
    {
        //server=212.44.99.2;user id=tomassho_mps;database=tomassho_mps;persistsecurityinfo=True
        private const string csMySQL = "host=xxx.44.99.xxx;" +
                                         "database=ts-servis_IoT;" +
                                         "user id=ts-servis_IoT;" +
                                         "password=xxxxjz00;" +
                                         "CharSet=utf8mb4;" +
                                         "persist security info=True;";
        public DispatcherTimer timer;
        //private Timer periodicTimer;
        int x = 25;
        double y = 23.5;
        int z = 1;
        long count = 0;
        long count2 = 0;
        public int instruc = 0;
        //int q = 0;
        private const int LED_PIN = 5;
        private GpioPin pin;
        private GpioPinValue pinValue;
        private SolidColorBrush redBrush = new SolidColorBrush(Windows.UI.Colors.Red);
        private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray);
        const string CalibrationFilename = "TSC2046";
        private Tsc2046 tsc2046;
        private TouchPanels.TouchProcessor processor;
        private Point lastPosition = new Point(double.NaN, double.NaN);

        MySqlCommand mcd;

        
        public MainPage()

        {

            this.InitializeComponent();

           
            
    }

    // do
    // {

    //} while (true);
    //*****






    private void InitGPIO()
        {
            var gpio = GpioController.GetDefault();

            // Show an error if there is no GPIO controller
            if (gpio == null)
            {
                pin = null;
                GpioStatus.Text = "There is no GPIO controller on this device.";
                return;
            }

            pin = gpio.OpenPin(LED_PIN);
            pinValue = GpioPinValue.High;
            pin.Write(pinValue);
            pin.SetDriveMode(GpioPinDriveMode.Output);

            GpioStatus.Text = "GPIO pin initialized correctly.";

        }
        //  private void TimerCallback(object state)
        //  {
        //     z = 1;

        //  }
        public void Timer_Tick1(object sender, object e)
        {
            if (pinValue == GpioPinValue.High)
            {
                z = 1;
                pinValue = GpioPinValue.Low;
                pin.Write(pinValue);
                //LED.Fill = redBrush;
            }
            else
            {
                pinValue = GpioPinValue.High;
                pin.Write(pinValue);
                // LED.Fill = grayBrush;
            }
            if (z == 1)
            {
                //timer.Start();
                x = x + 1;
                y = y + 0.42;
                InsertTemp(x, y);
                z = 0;
                TimerStatus.Text = "Writing to MySQL DB!!! ";
            }
            else
            {
                TimerStatus.Text = "Waiting 5000ms!!! ";

                HelloMessage.Text = "Instruc.nr- " + count2 + ": " + instruc / 5 + " Instructs/s";
                instruc = 0;
            }
            if (x > 100)
            {
                x = 10;
            }
            if (y > 100)
            {
                y = 10;
            }
            //throw new NotImplementedException();
            // switch (z)
            // {
            //    case 0:

            //        break;
            //    case 1:

            //       break; 
            //   default:

            //       break;
            //}

        }

        // private void Timer_Tick(object sender, object e)
        //  {
        //      z = 1;
        //  }

        public void InsertTemp(int LocationUID, double TempatureC)
        {
            using (MySqlConnection dbConn = new MySqlConnection(csMySQL))
            {
                dbConn.Open();

                {

                    {
                        string q = "INSERT INTO App_Tempature_Insert(LocationUID, TempatureC)" +
                                  "VALUES('" + LocationUID + "', '" + TempatureC + "')";
                        ExecuteQuery(q);
                    }

                }
            }
        }

        public void ExecuteQuery(string q)
        {
            try
            {
                using (MySqlConnection dbConn = new MySqlConnection(csMySQL))
                {
                    dbConn.Open();
                    mcd = new MySqlCommand(q, dbConn);
                    if (mcd.ExecuteNonQuery() == 1)
                    {
                        HelloMessage.Text = "Query Executed";
                        count2 = count2 + 1;
                        instruc = instruc + 11;
                        for (count = 1; count <= 60000000; count++)
                        {
                            instruc = instruc + 2;
                            // if (pin != null)
                            // {
                            //     break; // TODO: might not be correct. Was : Exit For
                            // }
                            // HelloMessage.Text = "Pin: " + pin;
                            //if (formatedTime != 5)
                            //{
                            //    break; // TODO: might not be correct. Was : Exit For
                            //}
                        }
                    }
                    else
                    {
                        HelloMessage.Text = "Query Not Executed";
                    }
                }
            }
            catch (Exception ex)
            {
                HelloMessage.Text = (ex.Message);
            }
            finally
            {
                using (MySqlConnection dbConn = new MySqlConnection(csMySQL))
                {
                    dbConn.Close();
                }
            }
        }

        private void ClickMe_Click(object sender, RoutedEventArgs e)
        {
            HelloMessage.Text = "Hello, Windows 10 IoT Core!";
        }

        private void GpioStatus_SelectionChanged(object sender, RoutedEventArgs e)
        {

        }

      
        //*************************
    }

}












Return to “Windows 10 for IoT”