I'm trying to open an OracleConnection on Visual Studio (c#) to join a Database with this command:
OracleConnection conn = new OracleConnection("Data Source=((DESCRIPTION=(ADDRESS_LIST =(ADDRESS=(PROTOCOL=tcp)(HOST=XX.XXX.X.XXX)(PORT=XXXX)))(CONNECT_DATA=(SERVICE_NAME=DEPL_D)(SERVER=xxxxx0xx)));User Id=X_XXX_XXX; Password=XXXXXXXXXX");
conn.Open();
my tnsnames.Ora contains the connection string like this:
DEPL_D.WORLD=
(DESCRIPTION=
(ADDRESS_LIST =
(ADDRESS=
(PROTOCOL = tcp)
(HOST = XX.XXX.X.XXX)
(PORT = XXXX)
)
)
(CONNECT_DATA =
(SERVICE_NAME = DEPL_D)
(SERVER = XXXXXXXXXX)
)
)
On the conn.Open(); I have the message ORA-12533: TNS: illegual ADDRESS parameter
After having searched on the net, It seems it's a problem on my tnsnames.oRA
I have done a test SQL+ with the administrator of the database (on audio) and it works. But It doesn't with visual studio.
If anybody has any idea, everything can helps.
Thanks in advance,
Greetings,
Flo
EDIT1:
I have tried to edit the (CONNECT_DATA =
(SERVICE_NAME = DEPL_D)
(SERVER = XXXXXXXXXX)
) with (CONNECT_DATA =
(SID=DEPL_D) but it doesn't works too.
Guessing from what you've provided: The SERVICE_NAME in the connect string is not the same SERVICE_NAME from your tnsnames.ora file.
Also, why did you censor the SERVER parameter ? Only possible values are SHARED, DEDICATED or POOLED. No ip address or hostname here.
Related
I am working on a Winforms project, and I am just setting up the setup file for the installation of the program.
Scenario
The connection string I have set for my use is like this:
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\Application\MyAppv1.0\MyApp\DB.mdf;Integrated Security=True");
return con;
Here, I use local DB from my personal location.
Question
On installation on the client's PC, the database is also attached, so the installed files will be on C:\Program Files\Author\MyAppv1.0. When I use the same connection string, the app shows an exception. So, should I update my connection string for that location during the setup creation? (I am using MSI).
Is there any commonly followed approach for this scenario?
My aim is to make the installed app use the DB that is also on the same location of installation named DB.mdf and is provided with setup.
Thanks guys in advance.
Do not use AttachDbFilename, it has many issues. Instead, attach your database normally using CREATE DATABASE...FOR ATTACH.
At startup of your app, you can connect to the server using master as the current DB, and check for existence of your DB. If it's not there you can create it.
private static void CheckDbExistence(string connectionString)
{
const string query = #"
IF NOT EXISTS (SELECT 1
FROM sys.databases
WHERE name = 'MyDb')
BEGIN
DECLARE #sql nvarchar(max) = N'
CREATE DATABASE MyDb ON
(FILENAME = ' + QUOTENAME(#mdf, '''') + '),
(FILENAME = ' + QUOTENAME(#ldf, '''') + ')
FOR ATTACH;
';
EXEC (#sql);
END;
";
var csb = new SqlConnectionStringBuilder(connectionString);
csb.Initial Catalog = "master";
using (var conn = new SqlConnection(csb.ConnectionString))
using (var comm = new SqlCommand(query, conn))
{
comm.Parameters.Add("#mdf", SqlDbType.NVarChar, 255).Value = Path.Combine(Environment.CurrentDirectory, "MyDb.mdf");
comm.Parameters.Add("#ldf", SqlDbType.NVarChar, 255).Value = Path.Combine(Environment.CurrentDirectory, "MyDb.ldf");
conn.Open();
comm.ExecuteNonQuery();
}
}
Im expirementing with databases and most recently specifically with the graph-database Neo4J. Im trying to connect Neo4j with C#, just as what I did with postgreSQL (see code)
class Sql_connection : DatabaseConnection
{
public string Server, Port, User_Id, Password, Database, Connstring;
public NpgsqlConnection SQLconnection;
public Sql_connection(string Server, string Port, string User_Id, string Password, string Database)
{
this.Server = Server;
this.Port = Port;
this.User_Id = User_Id;
this.Password = Password;
this.Database = Database;
this.Connstring = "Server="+this.Server+";Port="+this.Port+";User Id="+this.User_Id+";Password="+this.Password+";Database="+this.Database /* +";" */;
this.SQLconnection = new NpgsqlConnection(this.Connstring);
this.SQLconnection.Open();
}
public string InsertQuery(string INSERT_INTO, string VALUES)
{
NpgsqlCommand InsertCommand = new NpgsqlCommand();
InsertCommand.Connection = this.SQLconnection;
InsertCommand.CommandText = "insert into "+INSERT_INTO+" values "+VALUES;
InsertCommand.ExecuteNonQuery();
return "succes";
}
I already typed "Install-Package Neo4j.Driver-1.0.2" in the NuGetPackagemanager.
Besides that, I of course did some research myself, but I found multiple websites and github-repositories all saying something different, and I dont know what to believe/do anymore.
My two concrete questions are:
1: "How do you make a Neo4J-C# connection?"
2: "How do you run a query with this library/API"
I am aware of how graph-databases work and the syntax of Neo4j, so I understand the insertquery will not have insert into and values as key words.
Thanks in advance to everyone trying to help :D
Neo4j has good official documentation. Out of all sources the developers should be the most trustworthy. This is straight from their website and seems to work fine.
using Neo4j.Driver.V1;
using (var driver = GraphDatabase.Driver("bolt://localhost", AuthTokens.Basic("Username", "Password")))
using (var session = driver.Session()) {
sesion.Run("CREATE (a:Person {name:'Arthur', title:'King'})");
var result = session.Run("MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title");
foreach (var record in result)
Console.WriteLine($"{record["title"].As<string>()} {record["name"].As<string>()}");
}
Taken from here : https://neo4j.com/developer/dotnet/
Simply replace the localhost with your server ip (or localhost if you run it locally) and the username and password with your own username and password.
The developers have given multiple links to examples both documentation and Github source code. Hope this helps.
I'm beginner in oracle,want to connect oracle database with c# windows appliation
but why i try connect database i get this error:
my listener file is this:
MYLISTNER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = DESKTOP-A5CFJSH)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
SID_LIST_MYLISTNER =
(SID_LIST =
(SID_DESC =
(SID_NAME = CLRExtProc)
(ORACLE_HOME = C:\app\BEHZAD-HUSH\product\11.2.0\dbhome_2)
(PROGRAM = extproc)
(ENVS = "EXTPROC_DLLS=ONLY:C:\app\BEHZAD-HUSH\product\11.2.0\dbhome_2\bin\oraclr11.dll")
)
)
ADR_BASE_MYLISTNER = C:\app\BEHZAD-HUSH
and my tnsnames file is:
ORACLR_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
(CONNECT_DATA =
(SID = CLRExtProc)
(PRESENTATION = RO)
)
)
What happen?How can i solve that?thanks.
my tns ping is:
TNS Ping Utility for 64-bit Windows: Version 11.2.0.1.0 - Production on 02-NOV-2015 15:42:22
Copyright (c) 1997, 2010, Oracle. All rights reserved.
Used parameter files:
C:\app\BEHZAD-HUSH\product\11.2.0\dbhome_2\network\admin\sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))) (CONNECT_DATA = (SID = CLRExtProc) (PRESENTATION = RO)))
TNS-12541: TNS:no listener
Server name should be from TNS (ORACLR_CONNECTION_DATA), not localhost.
EDIT:
TNS-12541: TNS:no listener
Check that tns listener is running.
CMD> lsnrctl
LSNRCTL> start
Before you start to develop application, you must first check database connection over tnsping/sqlplus. Only after you have successfully connected you can move forward.
If listener is curently runnig, you cant try to connect over tcp/ip, not over IPC (after changing tns connection string in tnsnames.ora) on this -
ORACLR_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP) (HOST = DESKTOP-A5CFJSH) (PORT = 1521))
(CONNECT_DATA = (SID = CLRExtProc))
)
TNS is a real double-edged sword. When it works, it works great, but if you don't have any control over the client machine where you are deploying an app, you may want to just skip it and connect directly.
Fortunately, Oracle came up with ezConnect, which dramatically simplifies connection strings. It's essentially:
server/port:service name (or SID)
By explicitly specifying the elements normally encapsulated in TNSNAMEs, you eliminate any chances that the target machine has a different definition for that TNS name or worse, no definition at all.
Or, if you use the Direct=true, you can still bypass TNSnames and explicitly spell out the server, port and service name:
String conString = String.Format("Direct=true;Server={0};" +
"Port={1};Service Name={2};User Id={3};Password={4};",
"myhost.foo.bar", 1521, "oraprod", "scott", "tiger");
or using the SID (I can never remember which works, so I try both):
String conString = String.Format("Direct=true;Server={0};" +
"Port={1};SID={2};User Id={3};Password={4};",
"myhost.foo.bar", 1521, "oraprod", "scott", "tiger");
and then:
OracleConnection conn = new OracleConnection(constring);
conn.Open();
(with appropriate error trapping, of course).
That was more info than you asked for, but the bottom line is when you are establishing the connection, I would use this as the "Server Name" in your dialog:
DESKTOP-A5CFJSH:1521/CLRExtProc
I'm looking to change localhost in my code to an IP that would work for users using the application i'm creating. Could someone help me out on what to type? I tried just putting my public IP but it returned Unable to connect to any of the specified MySQL hosts.
Any help on what to do would be fantastic. Also, if there's anything I need to do port-wise to make this happen, please include that, preferably with instructions.
Code:
else
{
var constring = "datasource=localhost;port=3306;username=*****;password=*********";
using (var con = new MySqlConnection(constring))
{
con.Open();
var cmd0 = new MySqlCommand("select username from userinfo.users where username=#username");
cmd0.CommandType = CommandType.Text;
cmd0.Connection = con; // assign to connection
cmd0.Parameters.AddWithValue("#username", RUsernameTextBox.Text);
bool userNameIsFree;
using (var reader = cmd0.ExecuteReader())
{
userNameIsFree = !reader.HasRows;
}
if (!userNameIsFree)
CMessageBox("Error", "Username is already taken.");
else
}
}
try to get your IP.
open a command line (Start button > search... > cmd)
and run
ipconfig /all
Look for IPv4 Address. thats your local ip.
Try using that as your host. If its working, you are almost done. only have to configure yourself as an fixed ip in your network.
If its not working, you will have to chek a lot of things as i commented in your question, and that will be hard to answer here.
good luck!
I knew, there are thousands of topics similar to mine. I read them all. Nothing works for me. And there I will describe my problem: I created a Oracle database using Oracle SQL developer setting values:
connection name: test,
username: bob,
password: qwerty
Connection type: basic,
role: default,
Hostname: localhost,
port: 1521,
SID : orcl
Reason I'm writing all of this is that I really did everything possible, and I hope that by providing each kind of data someone will be able to help me.
My next step was downloading and installing ODAC 11.2 Release 4 (11.2.0.3.0) with Oracle Developer Tools for Visual Studio.
When I want make a connection to database from my application using server explorer I'm entering following data:
data source name: //localhost:1521/test
user name: ADMIN
password : qwerty
connection name is being set automatically.
When I'm trying to test connection I'm getting ORA 12514... and here it is how I changed my tnsname.ora:
LISTENER_ORCL =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
orcl =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
Make sure of the following:
Oracle service is running on windows.
Your pfile or spfile has local_listener='LISTENER_ORCL'
change the orcl config in tnsname.ora as :
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)