Specify schema/instance for DB2 query in connection string - c#

I am trying to specify the default schema/instance as part of a connection string (or specific command) so it does not need to be part of the query.
OdbcConnection conn = new OdbcConnection("Driver={IBM DB2 ODBC DRIVER}; Database=myDB; Hostname=myHostName; Port=myPort; Protocol=TCPIP; Uid=myID; Pwd=myPW;");
OdbcCommand comm = new OdbcCommand("select count(*) from customers", conn);
conn.Open();
var value = comm.ExecuteScalar();
conn.Close();
Unfortunately this fails with the error:
ERROR [42S02] [IBM][CLI Driver][DB2] SQL0204N myID.customers is an undefined name. SQLSTATE=42704.
Notice it's using myID where the Schema/Instance should be. If I specify the schema/instance explicity:
OdbcCommand comm = new OdbcCommand("select count(*) from mySCHEMA.customers", conn);
it works as expected. I'd like to specify mySCHEMA as part of the Connection String, similar to "Initial Catalog" when using MS SQL Server.
After a bunch of experimentation and googling, I can't seem to figure it out. Any ideas?

Wow, this one was obvious. I just need CurrentSchema=mySCHEMA in the connection string.
For some reason I didn't connect that dot right away after working through http://www.connectionstrings.com/ibm-db2 (tried all sorts of variations like Schema, Default Schema, etc). Hopefully this helps someone in the future...

Related

execute SQL script with USE DATABASE in C#

My query file looks like this:
USE DB_A
GO
SELECT * FROM sch.table;
but my connection string, because of some reason, should be forced to set as #"Data Source=SERVER;Initial Catalog=DB_B; ......"
As ExecuteNonQuery() doesn't support GO, this query will be separated into two parts, i.e, actual code looks like this:
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = ConnString;
OleDbCommand cmd = new OleDbCommand();
conn.open();
cmd.ComandText = "USE DB_A";
cmd.ExecuteNonQuery();
cmd.ComandText = "SELECT * FROM sch.table";
cmd.ExecuteNonQuery();
conn.close();
it seemsUSE DATABASE doesn't work, it is still reporting errors cannot find object sch.table when executing.
is there any neat solution other than changing my query file or my connection string?
As you've noticed USE doesn't work here. What you can do is fully reference your objects. So instead of
SELECT * FROM sch.table
write
SELECT * FROM DB_A.sch.table
Note that this will only work if you can access DB_A from DB_B with the same credentials you've used to connect to DB_B. If this is not the case you'll have to use a seperate connection to DB_A.
A Solution available is just remove the initial catalog Initial Catalog=DB_B from connection string. Not a perfect solution to this question, but worked in my case.

Error "ORA-00933: SQL command not properly ended" on Select with ODBC Command

I'm trying to execute a query against an Oracle DB using ODBC in .NET and am getting the following error:
ORA-00933: SQL command not properly ended
However, the SQL statement is definitely correct, and I can execute it successfully from Oracle SQL Developer. The query itself looks like this:
SELECT * FROM TABLE(SCHEMA.PKG.SPNAME('PARAMS'));
Another simple query works fine:
SELECT COUNT(*) FROM SCHEMA.MYTABLE
It looks like something with the former, where it's using a package in the query and causing something to break. The error indicates that it's not properly ended, but it has a semi-colon and correct braces, so it seems something else is going on.
If I remove the trailing semi-colon, I get an error with no message.
My C# code is basic and looks like this:
using (var connection = new OdbcConnection(connectionString))
{
using (var command = connection.CreateCommand())
{
command.CommandText = commandText;
connection.Open();
var result = command.ExecuteScalar();
connection.Close();
Console.WriteLine(result);
}
}
When using the Oracle library for .NET, it works when I remove the trailing semi-colon. If I keep that in place, the same error about the SQL command not being properly ended comes up.
It seems like this query should work with ODBC. Is there anything I need to differently to get it working, or is using the Oracle Managed Data Provider the only way?
From my experience, I have noticed several instances where a semicolon will break the query, such as JasperSoft Studio and the cx_Oracle Python module. I know very little about ODBC vs OracleDataClient, but I would imagine this is a similar situation.
I would not use ODBC I would actually do something like this using the OracleDataClient
var strSQL = "SELECT * FROM TABLE(SCHEMA.PKG.SPNAME('PARAMS'));";
using (OracleConnection connStr = new OracleConnection(connectionString))
{
using (OracleCommand cmd = new OracleCommand(strSQL, connStr))
{
cmd.CommandType = CommandType.Text;
cmd.Connection.Open();
cmd.ExecuteScalar(); //change the ExecuteScalar to fit the proper call
}
}

Error when trying to out out data from a database

I am getting the error shown here when trying to output things:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') FROM colours.prompt_reply' at line 1
Please help
MySqlCommand cmdDatabase = new MySqlCommand("select (user_Id, project, project_feedback, when) FROM colours.prompt_reply ;",conDataBase);
i think you have an error in your command
MySqlCommand cmdDatabase = new MySqlCommand("select user_Id, project, project_feedback, when FROM your_table_name",conDataBase);
is the 'when' part of your table?
i haven't really used MySql in C# but i had used Sql Sever before i think it would work
I think your query is not formatted properly,
Is "when" is a column name of your table ? If yes then
use below as
MySqlCommand cmdDatabase = new MySqlCommand("select user_Id, project, project_feedback, `when` FROM colours.prompt_reply ;",conDataBase);
As when is a keyword in MySql so if you are using that as column name then use backticks.

Can't create a sql connection due to the fact that it won't rcognize the data source keyword

Hello I'm trying to run a simple sql command on a DB from MS VS C# 2010 and I have encountered a error I have never seen before the relevant code is:
SqlConnection comCon = new SqlConnection(#"Data Source=C:\\Users\\George\\Desktop\\programming\\C#workspace\\Projects\\Examen\\Examen\\Companie.mdf;Initial Catalog=Proiect;Integrated Security=True"); 
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "UPDATE Proiect SET Buget = Buget + 500 WHERE (Buget > 0)";
cmd.Connection = comCon;                                                      
comCon.Open();
Console.WriteLine(cmd.ExecuteNonQuery().ToString());
comCon.Close();
And the error is Keyword not supported: 'data source'
The main problem is that I'm not used to creating these sqlconnections by hand so please tell me if I'm missing something.
You are using the wrong structure. To attach a database file, you need to use the following structure:
SqlConnection sqlConnection =
"Server=DatabaseServerName;AttachDbFilename=d:\Database\Database.mdf;
Database=DatabaseName; Trusted_Connection=Yes";
You need to have the right permissions on both the target file and database server to attach the databse and establish the connection.
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
If it's not an ASP.NET application don't use the DataDirectory syntax and just use the full c:... path.

running a query from C# on sql server

when i run a query like this:
SqlDataAdapter dap = new SqlDataAdapter("select * from some table", myConnection);
before doing the select, should i be doing "use somedatabase; go" ??
No, your database and schema should be set in the connection string for myConnection.
No you should specify the database name in myConnection
InitialCatalog = [databaseName]
Your connection string should something look like this
data source=[ServerName];Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=[DatabaseName];
I imagine myConnection is already specifying a default catalog (i.e. database) in its connection string, so you don't need to use the use line.
See here for details.
No; the myConnection object's connection string should define which database needs to be used, along with the server and login information.
That should all be in the myConnection variable, since I presume that contains the connection string.
Although you might want to call using on the DataAdapter
using(SqlDataAdapter dap = new SqlDataAdapter("select * from some table", myConnection)
{
//do stuff with dap here
}//dispose of dap
Since it does inherit from something that implements IDisposable.
Your connection string tells it what database to connect to.
connectionString = "Data Source=SERVERNAME; Initial Catalog=DATABASENAME; Integrated Security=SSPI;";
That would create a connection to a server and database using windows authentication.

Categories

Resources