WinFormApp won't connect to the database - c#

No clue why this won't work. It has worked before.
I do have the connection string in the app.config as well. I get the MySqlException error saying unable to connect to any database.
I made sure the firewall wasn't stopping it and I opened the ports on my router. All the references are in place too. This should work.
string connString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
try
{
using (MySqlConnection Conn = new MySqlConnection(connString))
Conn.Open();
MessageBox.Show("DB Connected");
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
}

try this code
string connetionString = null;
MySqlConnection cnn ;
connetionString = "server=localhost;database=testDB;uid=root;pwd=abc123;";
cnn = new MySqlConnection(connetionString);
try
{
cnn.Open();
MessageBox.Show ("Connection Open ! ");
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}

MySQL Databases cannot be used in WinFormApp due to security risks on a remote server. MS SQL DB's are supported. I didn't know that and I see a lot of questions about this so if anyone can't connect this may be why.

Related

unable to connect to any of the specified MySQL host cpanel

i get the error
Unable to connect to any of the specified MySQL hosts
string conn_string = "Server=jelletaal.nl;Port=3306;Database=jelletaa_thepuckcup;Uid=jelletaa_jelle;Pwd = nP_9+hC_!_2E;";
MySqlConnectionStringBuilder connstring = new MySqlConnectionStringBuilder();
try
{
MySqlConnection conn = new MySqlConnection(conn_string);
conn.Open();
MessageBox.Show("succesfully created connection to database");
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
in my cpannel I have created a database called jelletaa_thepuckcup
I have created a user called jelletaa_jelle
with the password nP_9+hC_!_2E
the domain is jelletaal.nl
i have also granted my ip adress acces to the database
if anyone knows what i have to do tho get this working, please tell me.

Issue connecting to gearhost database

I'm trying to connect with C# code to a SQL Server database hosted on gear host using the System.Data.SqlClient library, but it doesn't work, I get an error "Server does not exist or connection refused."
Maybe I need do install some driver? Or is there anything wrong with my code?
string connetionString = "Data Source=mssql.gear.host:1433;Initial Catalog=databasename;User ID=userid;Password=mypassword";
SqlConnection cnn = new SqlConnection(connetionString);
try
{
cnn.Open();
Console.WriteLine("Connection Open!");
cnn.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
when removing the port it works ,obviously the database in gear host doesn't use a port

How to disable connection pool?

Connection string that my app is using to connect to DB is the following:
private const string oradb = "Data Source=(DESCRIPTION=(ADDRESS_LIST="
+ "(ADDRESS=(PROTOCOL=TCP)(HOST=host.name)(PORT=1521)))"
+ "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=service.name)));"
+ "User Id=myusername;Password=mypass;";
In all DB access points of my app I am using the following pattern:
OracleConnection conn = new OracleConnection(oradb);
try
{
Console.WriteLine("Opening DB Connection...");
conn.Open();
string queryString = string.Format(#"SELECT ...");
using (OracleCommand command = new OracleCommand(queryString, conn))
{
using (OracleDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
...
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Exception occured during DB access: {0}", e.Message);
dbr.Error = e.Message;
}
finally
{
Console.WriteLine("Closing DB connection");
conn.Close();
conn.Dispose();
}
For sure I am properly handling exceptions and in try/catch/finally closing AND disposing connection object. However, often I am receiving oracle service message that I am holding oracle sessions. Moreover, if I just leave my app open and next day try to make operation, I am getting ora-12537 network session end of file exception first time, then second attempt is going through. After some reading it looks like I have to disable connection pool. If this is the right way to solve, how to disable pool? If not, then what other thing can be wrong?
You could add Pooling=False in the connection string, but this means a new connection is created each time.
+ "User Id=myusername;Password=mypass;Pooling=False;";
Take a look at this article, it might help with your issue. Also, take a look at this website page, specifically the Using Connection Pooling section

Connecting to a database in C#

I feel like an absolute idiot. I've been trying to connect to a database I created for a few hours now and I can't seem to get it to connect. Here is my code.
string chooseMoodCmbBx = moodCmbBx.SelectedIndex.ToString();
string source = "Data Source='E:\\Documents\\Database\\MyDatabase.sdf';" +
"Password='password';" +
"Persist Security Info=False;";
SqlConnection conn = new SqlConnection(source);
try
{
conn.Open();
MessageBox.Show("Succesfully Connected");
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
You have an SDF file, meaning that you connect to a Sql Compact not to Sql Server. You need to use the classes in the namespace System.Data.SqlServerCe
SqlCeConnection conn = new SqlCeConnection(source);
Also, I am not sure of this, but I think you don't need to have single quotes around values in the connection string
string source = "Data Source=E:\\Documents\\Database\\MyDatabase.sdf;" +
"Password=password;" +
"Persist Security Info=False;";

problem with oracle connection in c#

I have a Windows XP machine used to create .Net applications with VS 2008.
I want to connect to a remote network server (using Windows xp) that is running an Oracle 10g database.
I am using the code below (with the first connection string) to connect directly to a version of 10g that is running on the same machine with no problems, however when I try to connect to the network machine, it actually crashes my application.
I have tried several variations of connection strings as I feel that I must be making a syntax error somewhere.
What concerns me is that I have dual try/catch statements in the application and I do not understand why it simply does not refuse the connection and report the error.
I suppose the real question is 'what is the correct syntax for the connection string'....or WHATEVER the hell I am doing wrong.
Any help or suggestions are greatly appreciated. Thank you in advance.
//Class Variables
string CONNSTR = "Server=192.168.0.1:1521;User ID=zahid;Password=abc123;";
public Oracle()
{
InitializeComponent();
}
//Methods
private void TestMyOracleConnection()
{
OracleConnection Conn = new OracleConnection(CONNSTR);
try
{
Conn.Open();
MessageBox.Show("Oracle Connection Established", "Success");
}
catch (OracleException ex)
{
MessageBox.Show(ex.Message, "Oracle Connection Failed!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Oracle Connection Failed!");
}
finally
{
Conn.Close();
MessageBox.Show("Oracle Connection Closed", "Success");
}
}
private void buttonTestConnection_Click(object sender, EventArgs e)
{
TestMyOracleConnection();
}
You might want to try something like:
connection string:
Data Source=DBNAME;User ID=zahid;Password= abc123;Persist Security Info=True;Unicode=True;
Now look in the tnsname.ora file on your PC. It will be somewhere under your local Oracle Client installation. You're looking for something like:
DBNAME =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = OracleServerName)(PORT = 1510))
(ADDRESS = (PROTOCOL = TCP)(HOST = OracleServerName)(PORT = 1514))
)
(CONNECT_DATA =
(SERVICE_NAME = DBNAME)
)
)
You're looking to replace DBNAME and OracleServerName with your own. Don't forget, DBNAME is the name of the main database, not one of the schemas (the entity with all the tables and procedures, etc) under it.
Assuming you are using ODP.Net, you can try a connection string such as the following (taken from a web.config):
<add name="OdpConnection" connectionString="Data Source=xe;User Id=some_user;Password=some_password; Promotable Transaction=local"
providerName="Oracle.DataAccess.Client" />
You can read this connection string from the web.config in your code using something like this:
string connectionString = ConfigurationManager.ConnectionStrings["OdpConnection"].ConnectionString;
It's a good practice to avoid putting connection strings directly in code, since to change them requires a re-compile. By putting them in a config file, such as web.config or app.config, you can change them without having to re-compile your library or exe.
try
{
Conn.Open();
MessageBox.Show("Oracle Connection Established", "Success");
}
catch (OracleException ex)
{
MessageBox.Show(ex.Message, "Oracle Connection Failed!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Oracle Connection Failed!");
}
finally
{
Conn.Close();
MessageBox.Show("Oracle Connection Closed", "Success");
}
You have pottential problem here: if conn.Open() fails (so connection is not opened) in finally you call conn.close(), but connection isn't opened - so it also fails, but it is now outside try catch. You need check for
if (Conn!=null && Conn.IsOpen())
Conn.Close();
Connection string for oracle:
Data Source=TORCL;User Id=myUsername;Password=myPassword;
Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;
More here

Categories

Resources