Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last month.
Improve this question
Please advise me. I am trying to develop Windows Application in WPF with Mysql server database.
Which approach I have to use to create connection establish on the Mysql database?
1) Entity Framework
OR
2) ADO.NET
OR
3) Other one
The Admin computer need to install mysql database on his computer and then access the mysql database to another (10 or more) client systems which are under network connected.
Here, just one MySql database inside admin computer only. And all other client machines are accessing MySql database by admin's computer IP address. The IP connection string is like this:-
#"Data Source=10.0.0.101
One of other matter is, the customer demanding that, when installing .exe file on client machine, then should have to automatically install mysql server database (ie, xampp or other ect..) also.
How can I do this?
I have tried, ADO.NET C# instead EF to connect mysql for wpf windows application. like that :-
public void AddUserList( UserSetUp user)
{
dbConnection.Open();
string query = "INSERT INTO Tble_UserSetUp (FirstName, SurName, Email, PhoneNumber, UserName, Password,Computer_Name,IP_Address,CreatedBy, CreatedDate,IsActive,Function,Department) VALUES (#FirstName, #SurName, #Email, #PhoneNumber, #UserName, #Password,#Computer_Name,#IP_Address,#CreatedBy, #CreatedDate,#IsActive,#Function,#Department)";
MySqlCommand insertSQL = new MySqlCommand(query, (MySqlConnection)dbConnection);
insertSQL.Parameters.AddWithValue("#FirstName", user.FirstName);
insertSQL.Parameters.AddWithValue("#SurName", user.SurName);
insertSQL.Parameters.AddWithValue("#Email", user.Email);
insertSQL.Parameters.AddWithValue("#PhoneNumber", user.PhoneNumber);
insertSQL.Parameters.AddWithValue("#UserName", Encrypt(user.UserName));
insertSQL.Parameters.AddWithValue("#Password", Encrypt(user.Password));
insertSQL.Parameters.AddWithValue("#Computer_Name", user.Computer_Name);
insertSQL.Parameters.AddWithValue("#IP_Address", user.IP_Address);
insertSQL.Parameters.AddWithValue("#CreatedBy", user.CreatedBy);
insertSQL.Parameters.AddWithValue("#CreatedDate", user.CreatedDate);
insertSQL.Parameters.AddWithValue("#IsActive", user.IsActive);
insertSQL.Parameters.AddWithValue("#Function", user.Function);
insertSQL.Parameters.AddWithValue("#Department", user.Department);
try
{
insertSQL.ExecuteNonQuery();
dbConnection.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Is it a bad idea instead of use Entity Framework or another one?
public partial class MainWindow : Window
{
/*mysql
*
* 1. add nuget mysql.data.dll(desktop application)
* 2. using MySql.Data.MySqlClient;
* 3. code like bellow , you can try
*/
private StringBuilder sb = new StringBuilder();
public MainWindow()
{
InitializeComponent();
mysql();
tbx.Text = sb.ToString();
}
private void mysql()
{
try
{
var connstr = "Server=localhost;Uid=root;Pwd=123456;database=world";
using (var conn = new MySqlConnection(connstr))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from city where countryCode= #ID";
cmd.Parameters.AddWithValue("#ID", "100");
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var ii = reader.FieldCount;
for (int i = 0; i < ii; i++)
{
if (reader[i] is DBNull)
sb.AppendLine("null");
else
sb.AppendLine(reader[i].ToString());
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
Reference: Official MySQL documentation - Introduction to Connector/NET Programming
Step 0: Setup MySql and Create a database
Step 1: Create WPF Project
Step 2: Install MySql.Data package using Nuget package manager
(Visual Studio) Project> (click) Manage NuGet packages
Select Browse tab> search Mysql.data > Select Mysql.data package> Install
Step 3: In my case I created the connection within MainWindow.xaml.cs file.
To consume MySql.Data and MySql.Data.MySqlClient
using MySql.Data;
using MySql.Data.MySqlClient;
Full Code (relevent to the question): MainWindow.xaml.cs file
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace EAppartments
{
public partial class MainWindow : Window
{
string connStr = "server=localhost;user=root;database=YourDBName;password=yourpassword";
//Connect to MySql when app starts
//you can use OnStartUp event as well
public MainWindow()
{
InitializeComponent();
connectToMySql();
}
private void connectToMySql()
{
MySqlConnection conn = new MySqlConnection(connStr);
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
//close connection if you want
conn.Close();
Console.WriteLine("Done.");
}
}
}
Useful resources:
Execute MySql commands using WPF app
Lifecycle of WPF application
Related
Please help.
I have device MC3200 Zebra(motorola)(Windows Embedded version 7, build 2864). This device is connecting to network and see SQL server(ping is OK). I used it Visual Studio 2008, c#, SmartDevicePrj, .NET CF 3.5
But after start application on device will be displayed message:
Unknown connection option in connection string: initial catalog.
Some idea how to repair it?
Many thanks for your help.
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
namespace SmartDeviceProject1
{
public partial class Form1 : Form
{
public SqlCeConnection msConn;
public string strCon = "";
public SqlCeCommand command;
public Form1()
{
InitializeComponent();
strCon = "Data Source=server007; Initial Catalog=FMPredlis; User ID=mistr; Password=heslo;";
try
{
msConn = new SqlCeConnection(strCon);
msConn.Open();
MessageBox.Show("Připojeno");
}
catch (SqlCeException ex)
{
MessageBox.Show("Chyba" + ex.Message);
msConn.Close();
}
}
}
}
I think you are mixing 2 things.
Either use The SqlConnection object, then you can connect to a SQL Server using a connection string found on this page:
https://www.connectionstrings.com/sql-server/
Or use like in your code the SqlCEConnection, then you connect to a SQL Lite (local file), and your connection string should be like on this page:
https://www.connectionstrings.com/sqlite/
But in your sample you are using SqlCEConnection to connect to a sql server, that wont work.
Grtz
Hey Members i am new to C# Language and Visual Studio Platform recently i am learning how to connect access Database with visual Studio and first time with same code i have connected with database but after some time when i compiled again then there is error given in Title.
why this is happening ?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Clinic_Management_System
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void Login_Load(object sender, EventArgs e)
{
try
{
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users
\Hassan Gillani\Documents\Clinic Management System.accdb; Persist Security Info = False; ";
connection.Open();
label1.Text = "Connected to Clinic Management System Database";
connection.Close();
}
catch (Exception exp)
{
MessageBox.Show("Error " + exp);
}
}
}
}
please visit given like to view screen short
http://s33.postimg.org/5ltm4dtnj/Error.png
Using the verbatim character (#) and splitting your path in the middle in not a good idea.
Spaces counts in paths so the filename used for your connection is
C:\Users \Hassan Gillani\Documents\Clinic Management System.accdb;
If you try to use File.Exists on this string you get false as result.
Do not split your connection string in the middle of the path
connection.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\Users\Hassan Gillani\Documents\Clinic Management System.accdb;
Persist Security Info = False; ";
I have MYSQL database from Hosting provider net4.in and i am unable to access database.
My code is as below :
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class dummy : System.Web.UI.Page
{
OdbcConnection conn;
string Address = "Driver={MySql ODBC 5.1 Driver};Server=phpmyadmin7.net4india.com;Option=3;Port=3306;Stmt=;" +
"DataBase=test;Uid=demo;Pwd=demo123;";
protected void Page_Load(object sender, EventArgs e)
{
string sql = "Select * from test.testing limit 5";
DataSet rsDSet = new DataSet();
try
{
if (sql == "") throw new Exception("SQL input required");
conn = new OdbcConnection(Address);
if (conn.State == ConnectionState.Closed) conn.Open();
OdbcDataAdapter adp = new OdbcDataAdapter(sql, conn);
adp.Fill(rsDSet);
}
catch (Exception ee)
{
Response.Write(ee.ToString());
}
GridView1.DataSource = rsDSet;
GridView1.DataBind();
}
}
Pl let me know how can i connect MYSQL DB.
Thank you in advance
Are you sure your MySQL instance is hosted on a machine named phpmyadmin7? It seems unlikely. Phpmyadmin is the name of a chunk of client software, as I am sure you know.
Are you sure your hosting provider allows access from outside their data center to MySQL server instances? (Presuming you are running the sample code you provided from your own machine, which might not be the case.) Most hosting providers protect their MySQL instances from internet-wide access because it is easy to mount DDOS attacks against them otherwise.
In some hosting provider environments the MySQL authentication scheme only allows access to your MySQL instance from the particular machine upon which your web apps run.
In any case you need to have a conversation with your support crew about how to do this.
i have a web server on internet i am using php/mysql. for my website, now i want to connect to my mysql database on my webserver using my c# windows form application so that i will be able to view,create, update, delete the data on mysql database on webserver, i want to user same database for website and my desktop application. i want any one having my desktop application would be able to update my database on my webserver(i mean not only form some specific ip).
Here is my code when i try to run it shows error "Unable to connect to any of specified MySQL hosts."
can any one Guide me how to do it. Thanks.
The following code is working fine for localhost but not working for web-server which is on internet.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient; // for mysql
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string con_str = "SERVER=fdb4.biz.nf;Port=3306;DATABASE=1558711_zee;UID=1558711_zee;PASSWORD=multan321;compress=true;";
MySqlConnection connection = new MySqlConnection(con_str);
try {
connection.Open();
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT * FROM zeetable";
MySqlDataAdapter mda = new MySqlDataAdapter(cmd);
DataSet mds = new DataSet();
mda.Fill(mds);
dataGridView1.DataSource = mds.Tables[0].DefaultView;
}
catch(MySqlException ex){
MessageBox.Show(ex.Message);
}
}
}
}
I want to write a MySql statement that will connect to the database, select a column from the table, then output that data to a text file to a specific location on my computer. I have searched the internet for a couple days now and don't seem to find the answer I am looking for. I am fairly new to c#, MySql, and Visual Studio. I am just trying to learn how to write the correct statements and get the desired result. Any help would be greatly appreciated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using MySql.Data;
using System.Windows.Forms;
using System.IO;
namespace NewPractice
{
public class Connect
{
static void Main()
{
string results = #"server=111.111.11.111; userid=anyone;
password=anypassword; database=anydatabase";
MySqlConnection conn = null;
try
{
conn = new MySqlConnection(results);
conn.Open();
//Console.WriteLine(
File.WriteAllLines(
#"C:\Documents and Settings\anyone\My Documents\Tests\testoutput.txt",
results.ToArray());
}
catch (MySqlException ex)
{
Console.WriteLine("Error: (0)", ex.ToString());
}
finally
{
if (conn != null)
{
conn.Close();
}
}
}
}
}
You're writing the contents of the result string to the file, not the data you're attempting to select. You need to run a sql command and get a SqlDataReader object to write your data to the file.
string results = #"server=111.111.11.111; userid=anyone;
password=anypassword; database=anydatabase";
MySqlConnection connection = new MySqlConnection(results);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader reader;
command.CommandText = "select * from mycustomers";
connection.Open();
reader = command.ExecuteReader();
using(var sw = new StreamWriter("C:\MyPath\MyFile.txt"))
{
while (reader.Read())
{
var row = (IDataRecord)reader;
sw.WriteLine(row["myColumn"]);
}
}
connection.Close();
If the database is on your local machine you can use 'select .. into outfile'. http://dev.mysql.com/doc/refman/5.1/en/select-into.html. This will write to a folder on the server so it's not v useful if it's a different machine and you can't copy from there.
There are plenty of tutorials out there for accessing MySQL from .NET.
This is one: http://zetcode.com/db/mysqlcsharptutorial/
In any language, there are a few simple steps to read from a database:
1. connect to the database.
2. execute a query
3. iterate through the results of the query
4. close the connection.
What you are doing in your code is connecting to the database and then trying to write the connection information to a file.