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
Related
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.
I have a database on my local machine on SQL Server Management studio.
The database connection information is as follows.
In my c# application, I have a connection string to connect to this database. I get an error though saying my connection string is incorrect. I have tried a number of different ones.
This is my function to connect to a database.
public virtual void openConnection()
{
con = new SqlConnection();
con.ConnectionString = "Server=DESKTOP-8UDMQUI\\WILLIAMSQL;Initial Catalog=team3db;TrustServerCertificate=true;";
try
{
con.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
I have two \ in the server name because when I only have one, it has a red squiggle line under it throwing me an error on visual studio.
What should my connection string be? Note there is no password on this database.
Replace TrustServerCertificate = true; with Integrated Security = true
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
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
I am creating a Windows forms application and my SQL Server database is on a remote server. How can I connect to it using Visual C# and ADO.NET?
You need to investigate the SqlConnection, SqlCommand and possible SqlDataReader and SqlDataAdapter components in .NET (see the MSDN online docs).
Once you have that, you need to define your connection string - check that site link for a huge selection and explanation of connection strings.
Then you basically connect using:
using(SqlConnection conn = new SqlConnection('your connection string here'))
{
conn.Open();
// do stuff
conn.Close();
}
and you can do stuff in various ways, e.g. by filling data sets, reading values etc.
Read the MSDN ADO.NET Overview to get started! Or Google for "ADO.NET tutorial" - you'll find tons of links.
The answer can be found here -
Connect to remote MySQL database with Visual C#
Also, read up and download from here -
http://dev.mysql.com/downloads/connector/net/5.2.html
In the eye of MS SQL Server it is no difference where your SQL Server is located. All you need is to make sure you have access that server in terms of IP and Port number.
Use the below code to create the connection objects necessary.
public bool BeginTransaction(string strServerName) {
try
{
bool bRet = OpenConnection(strServerName);
if (bRet)
{
m_objTransaction = m_conn.BeginTransaction();
m_dtAdapter.SelectCommand.Connection = m_conn;
return true;
}
}
catch (Exception ex)
{
return false;
}
return false;
}
public bool OpenConnection(string strServerName) {
try
{
m_connStr = string.Empty;
m_connStr = string.Format("Data Source=;Initial Catalog=;User Id=sa;Password=;"); //write your credentials here with DB name and server
m_conn = new SqlConnection(m_connStr);
m_conn.Open();
m_dtAdapter = new SqlDataAdapter();
if (m_conn != null)
{
m_dtAdapter.SelectCommand = new SqlCommand();
}
}
catch (SqlException ex)
{
return false;
}
catch (Exception ex)
{
return false;
}
return true;
}