Connection to MySql database in ASP.NET Core - mysql

I am creating website in ASP.NET Core 2.0 and now I need to get data from my mysql database.
Database is located on hosting which use cpanel
In past I have created simple REST API which get some data from that database and it used this config:
server: db5.cpanelhosting.rs
Port: 3306
user: termodom_guest
pw: a1s2d3f4g5h6j7k8
Now problem is when I use code like this:
using (MySqlConnection con = new MySqlConnection("server=217.26.215.19;port=3306;database=termodom_TDMajstor;user=termodom_guest;password=a1s2d3f4g5h6j7k8"))
{
con.Open();
using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM KORISNIK", con))
{
MySqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
return true;
}
}
}
It drops me error
MySql.Data.MySqlClient.MySqlException (0x80004005): Timeout expired.
The timeout period elapsed prior to completion of the operation or the
server is not responding. at
MySql.Data.Common.StreamCreator.GetTcpStream(MySqlConnectionStringBuilder
settings) at MySql.Data.MySqlClient.NativeDriver.Open().
I have also tried online testing connection but it also cannot connect. What to do?

Related

Backup MariaDB database from Android client (Xamarin)

MySQLBackup.net works fine on Windows applications, but not in Xamarin (tested with exact same code).
Is there any workaround or alternatives that I can use ? I'm really lost...
I want to backup my database from a Linux (Debian9) MariaDB server, so I found MySqlBackup.NET, but nothing can make it work with Xamarin.
There's my code to backup (works in windows console app):
// Use MySqlBackup.Net to backup database
using (var conn = new MySqlConnection("myconnstring"))
{
conn.Open();
using (MySqlCommand cmd = new MySqlCommand())
{
conn.Open();
using (MySqlBackup mb = new MySqlBackup(cmd))
{
cmd.Connection = conn;
mb.ExportToFile(Constants.EXPORT_PATH + "/dolibarr_" + identifier + "dataBackup.sql");
conn.Close();
}
}
}
Getting exception: The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception.

aws mysql connection issue for azure site

I cant seem to find an answer to my issue.
I made a quick little website with .net and published it on azure.
when I test it I can query my aws mysql db just fine however after publishing I am getting.
System.InvalidOperationException: Connection must be valid and open. at
MySql.Data.MySqlClient.Interceptors.ExceptionInterceptor.Throw(Exception
exception) at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex) at
MySql.Data.MySqlClient.MySqlCommand.Throw(Exception ex) at
MySql.Data.MySqlClient.MySqlCommand.CheckState() at
MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() at
TaskiiLink._Default.Page_Load(Object sender, EventArgs e) in
C:\............\Default.aspx.cs:line 39
line 39: MySqlDataReader reader = command.ExecuteReader();
my connection string is :
<add name="dbconn" connectionString="user id=NAME;Password=PASS;persistsecurityinfo=true;server=AWSENDPOINT;Port=3306;database=DB;" providerName="MySql.Data.MySqlClient"/>
page load code
if(Request["type"] == "1")
{
string sql = "checkDB";
conn.Open();
using (MySqlCommand command = new MySqlCommand(sql))
{
command.Connection = conn;
try
{
MySqlDataReader reader = command.ExecuteReader();
if (!reader.Read())
{
Response.Write("false");
} else
{
Response.Write("true");
}
You need to pass the connection object.
MySql.Data.MySqlClient.MySqlCommand myCommand = new MySql.Data.MySqlClient.MySqlCommand(insertQuery, connection);
Figured it out myself,
AWS security group was only allowing my local host ip as an inbound.
found my web apps outbound ip in the property settings in azure portal.
helpful links if anyone has the same issue.
AWS issue
https://forums.aws.amazon.com/thread.jspa?threadID=176030
azure info
https://social.msdn.microsoft.com/forums/azure/en-US/fd53afb7-14b8-41ca-bfcb-305bdeea413e/maintenance-notice-upcoming-changes-to-increase-capacity-for-outbound-network-calls
issue I had when adding the ip solution.
https://forums.aws.amazon.com/thread.jspa?threadID=158994

ServiceStack OrmLite: MySQL connection pool

I understand the topic is not new, I read a few posts but did not come to the answer ...
Each time the connection is opened for a very long time, but the idea was to use a connection pool, is not it?
As I understand it in MySQL you cannot specify a connection pool in the connection string.
How to do the right thing not to spend a lot of time opening the database connection?
Thanks!
IDbConnectionFactory = connection = new OrmLiteConnectionFactory(TeleportParams.DbConnectionStr, MySqlDialectProvider.Instance)
...
void function1(){
var db = connection.Open();
db.Select("some request");
}
void function2(){
var db = connection.Open();
db.Select("some request");
}
...
function1();
function2();
As I understand it in MySQL you cannot specify a connection pool in the connection string.
You can add pooling=false to the connection string to disable Connection Pooling in MySql.

how to prevent tomcat from caching mysql data

I'm not sure if this is a tomcat issue, or a mysql issue. When reading an entire table (select * from Employees), and returning the data through jsp, the same data is returned even after updating the table.
I am using my own homespun connection pool. That cant change at the moment as both a desktop app and the servlets use the same core libraries.
I have READ-COMMITTED set in mysql config
transaction-isolation=READ-COMMITTED
I even set it when creating the connection
conn = DriverManager.getConnection(url, userName, password);
if (conn != null)
conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
Then in the servlet I get the data and send it out like this (GetAll() is effectively SELECT * from Employees):
Vector<DBO_Employees>vc = DBO_Employees.GetAll();
Vector<JSONObject>vr = new Vector<JSONObject>();
for (DBO_Employees h : vc)
vr.add(h.json());
try {
json.put("employees", vr);
} catch (JSONException e) {
e.printStackTrace();
}
response.setContentType("application/json;charset=utf-8;");
PrintWriter pw = response.getWriter();
pw.write(json.toString());
To test I call the servlet once, make a quick change in the database (using mysql cli), then refresh the page however the change in data is not there. I'm not sure if mysql is caching the data or if tomcat is caching the data.

How can I make my DB connection works?

I set up a MySQL database in my AZURE account, but I cannot connect it in any way. I tried:
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
SqlConnection sqlconnection = new SqlConnection("Database=Banco1; Data Source=br-cdbr-azure-south-a.cloudapp.net;User Id=USERID Password=PASSWORDid");
Console.WriteLine("1");
sqlconnection.Open();
Console.WriteLine("2");
SqlCommand cmd = new SqlCommand("CREATE TABLE Customer(First_Name char(50), Last_Name char(50), Address char(50), City char(50), Country char(25), Birth_Date datetime); ", sqlconnection);
Console.WriteLine("3");
cmd.ExecuteNonQuery();
Console.WriteLine("4");
Console.ReadLine();
}
}
}
I tried putting prints through the commands, and the program only prints "1" wich means I can't get the connection opened... how can I fix that?
The output error:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Error in the network or specifics to the instance when stablishing conexion to the SQL Server. The server was not found or was not avaible. Check if the instance name is correct and if the SQL Server is configured to allow remote conexions. (provider: Named Pipes Provider, error: 40 - Could not stablish connection to SQL Server)
SqlConnection is used for connecting SQL Database, for MySQL Database, you need to use MySqlConnection. Please first install MySql Connect/NET and then add reference
Now you can connect to your Mysql database successfully with the code below:
MySqlConnection con = new MySqlConnection("your_mysql-connect_string");