C# connect to MySQL and read data - c#

So I have this website that people can shop online, but I want to make a software that will read the MySQL database for new orders and when a new order is placed, the software should display the order in some sort of a list. I am using wordpress for this website and there is a button to mark the order as completed, so i also want to have that little button in my program. Any help from where I should be starting ? Thank you!

Kindly do some research work before ask Any question. This may help u.
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;
myConnectionString = "server=xxxx;uid=xxx;" +
"pwd=xxxx;database=xxx;";
try
{
conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = myConnectionString;
conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
}

Related

UWP connection string

I want to create a connection for my UWP and database. I want the uwp to send a value to the database.
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;
myConnectionString = "server=127.0.0.1; uid = root;" + "pwd=root;database=test";
try
{
conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);
conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
}
Is this the correct way to write the connection ? and where do I write this part of the coding at ?
enter image description here
Main page or any of my other page ? (scenario 1-3)
Your content looks right, I would try to use 'localhost' rather than ip.
var myConnection = new MySqlConnection();
myConnection.ConnectionString = "database=test;server=localhost;uid=root;pwd=root";
myConnection.Open();
More info see: https://dev.mysql.com/doc/dev/connector-net/6.10/html/P_MySql_Data_MySqlClient_MySqlConnection_ConnectionString.htm
I would also check here to see if you are providing enough info, which you are. https://www.connectionstrings.com/mysql/
In terms of connecting to the database, it depends. What type of application is this? Typically, database connections are made during the start of the application. If you are using Entity Framework, you'll want your Database Context to manage the connection (which is an entirely different topic).

Interacting with SQL Server database in Visual Studio 2015

I realise this question could be quite broad but ive been searching for the past 2 days with no luck.
I've created a project in Visual Studio 2015 and created a new data source in that project using a cloud database created with SQL Server Management Studio.
I'm now trying to code a login page in the project, which gets a username and password from 2 textboxes on the UI and executes an SQL query to check if the user exists in the database and if their password is right.
I'm familiar with Java code for database connections, such as the prepared statement and resultset functions.
Is there something equivalent for C#?
Thank you, reference to helpful articles or code samples will be greatly appreciated.
I think this can help you :-
http://csharp.net-informations.com/data-providers/csharp-sql-server-connection.htm
http://www.codeproject.com/Articles/823854/How-to-connect-SQL-Database-to-your-Csharp-program
here is probably the fastest but not the best way to check it.
string yourConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Users.accdb; Persist Security";
using (OleDbConnection conn = new OleDbConnection(yourConnectionString))
{
try
{
conn.Open();
using (OleDbCommand cmd = new OleDbCommand("Select * from UsersTable where UName = #Username and Pass = #Password"))
{
cmd.Parameters.AddWithValue("#Username", txtUserName.Text);
cmd.Parameters.AddWithValue("#Password", txtPass.Text);
using (OleDbDataReader r = cmd.ExecuteReader())
{
if (r.HasRows)
{
Console.WriteLine("User exists")
}
else
{
Console.WriteLine("User does not exist")
}
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}

VS C# connected to mySQL database

I am very new in C# and now try to find online resource to connect VS C# to mySQL database at server 'localhost', with userid 'root', and password '****', the databasename is 'dlht'.
1.I copied a line of code from youtube and it works:
this.stockTableAdapter.Fill(this.blhsDataSet.stock);
Can anyone explain to me what exactly this is doing? There is no place to put server, password, userid etc... How can it work?
I tried to use the online tutorials to connect to mySQL database
ZetCode C#Tutorial
string cs = #"server=localhost;userid=root;
password=****;database=dlht";
MySqlConnection conn = null;
try
{
conn = new MySqlConnection(cs);
conn.Open();
Console.WriteLine("MySQL version : {0}", conn.ServerVersion);
} catch (MySqlException ex)
{
Console.WriteLine("Error: {0}", ex.ToString());
} finally
{
if (conn != null)
{
conn.Close();
}
}
I run this code at Form1.cs at VS C#. It is always stuck at :
conn = new MySqlConnection(cs);
Why? Thank you so much!
It seems that the proper way to connect to MySql database is this
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

How can commit data update in Microsoft SQL sever?

In a web app, I am using SQL server. However, when I try to store some bulk amount of data, it misses some of the records and does not insert them into the database. I want to know whether there is any commit statement or synchronization for the database? Data is being sent object by object using an ajax call.
Here is my code:
try
{
int surah = Convert.ToInt32(Request["surah"]);
string verse = Request["data"];
string connectionString = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\PROGRAM FILES (X86)\MICROSOFT SQL SERVER\MSSQL.1\MSSQL\DATA\PEACE_QURAN.MDF;Integrated Security=True";
System.Data.SqlClient.SqlConnection connection = new SqlConnection(connectionString);
string query = "insert into Ayyat_Translation_Language_old_20131209 values(null,null,"+surah+",'"+verse+"')";
SqlCommand cmd = new SqlCommand(query, connection);
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
}
catch(Exception e){
System.IO.StreamWriter file = new System.IO.StreamWriter(#"E:\Office_Work\Peace_Quran\Peace_Quran\Files\ExceptionFile.txt", true);
file.WriteLine("exception details : "+e.ToString());
file.Close();
}
As you understand, the records cannot get lost in the way. Either the INSERT statement would execute, or you would get an exception. Since neither is happening, I believe that you loose something in the request generating mechanism.
I would strongly suggest to put some logging message on each request. You will probably find out that your requests are less than you thought. This could be for a number of reasons, but since I don't know the exact mechanism calling the server side code, I cannot have an opinion.
Hope I helped!

ODBC Connection Issues: Function Sequence error when executing anything

I've been tasked with a one time migration from a 4D database to our MSSQL structure. I've got a datasource set up in the ODBC administrator and I was able to connect to it without issues.
I 'reverse engineered' the schema in Visio so I can get a good visual of the relationships between the tables and plan out how I'm going to re-structure the 4D data to fit into our schema. I created a simple console application as this will be a one time run, and I'm able to make the connection to the data source, but as soon as I execute anything, the connection drops or is disabled.
System.Data.Odbc.OdbcConnection conn =
new System.Data.Odbc.OdbcConnection("Dsn=4D v12 datasource");
try
{
conn.Open();
Console.WriteLine("Status of Connection:" + conn.State.ToString());
//Here it says the connection to the DB is open and ready for action
Console.ReadLine();
//pause to visually confirm the connection is open
OdbcCommand com = new OdbcCommand("SELECT * FROM ATable", conn);
com.CommandType = CommandType.Text;
Console.Write(com.ExecuteNonQuery());
//Right here it blows up and closes the connection
}
I also tried to do something with data set and data adapter, but to no avail.
System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection("Dsn=4D v12 datasource");
try
{
conn.Open();
Console.WriteLine("Status of Connection:" + conn.State.ToString());
Console.ReadLine();
OdbcCommand com = new OdbcCommand("SELECT * FROM ATable", conn);
com.CommandType = CommandType.Text;
//Also tried using data sets and data adapters
DataSet dsTest = new DataSet();
OdbcDataAdapter dataAdapter = new OdbcDataAdapter(com);
//but right at this line the connection suddenly disconnects
dataAdapter.Fill(dsTest);
}
catch (Exception e)
{
Console.WriteLine("Failure:" + e.Message.ToString());
// the exception message reads simply connection has been disabled.
Console.WriteLine("Status of Connection: " + conn.State.ToString());
Console.ReadLine();
}
finally
{
Console.Write("Closing connection.");
conn.Close();
Console.Write(".");
conn.Dispose();
Console.WriteLine(".");
Console.WriteLine("Connection Closed and Disposed");
Console.ReadLine();
}
I've tried to look for anyone experiencing this same difficulty, but the documentation I've found has been scarce and not very helpful in this specific regard. There is a good amount of information about executing queries on the 4D product, but not from across the digital divide. Anyone with experience please advise. Thanks for your time and for any help you may be able to give, avid reader.
Apparently the root cause of the issue was in the driver. Upon replacing the driver I got from the website with one sent from the proprietor, the connections ceased being disabled and all is working as intended.

Categories