Use of database name in connection string - sql

What is the use of mentioning the database name in connection string while opening a connection from dot net application to SQL server? Because even though we mention a database name in connection string we have to explicitly write the fully qualified name (DBName.schemaName.ProcName) while calling a stored procedure if the default DB is different for that particular user.

Connecting to database from a .NET application is different from accessing a table of different database.
use of mentioning the database name in connection string
so for instance you can use connection string below to connect to myDB at MyServer
Data Source=MyServer;Initial Catalog=myDB;Integrated Security=True
if you will not specify at least these information how your .NET application can connect to a stored procedure (MyProcInMyDB) located in myDB.
Now for part you asked
though we mention a database name while calling a stored procedure if
the default DB is different for that particular user
this is not a normal case to access stored procedure of another database using same connection string if it is a very special case (not likely) then you will do it for calling one or two stored procedures. But if it is required quite often within your application then you should create a separate connection string. Using same connection string and calling like
command.CommandText = "myDB2.dbo.getList"
can result is difficult maintenance and flexibility

Related

SSIS Oracle connection string as project parameter is not being replaced at runtime

We move data from Oracle 11 to SQL 2014 using SSIS project deployment model. We use Attunity 3.0 connector.
Connection string to oracle data source is a project parameter and is also stored in a table in SQL.
We use custom stored procedure that
Gets this connection string stored in the sql table
sets project parameters (via [SSISDB].[catalog].[set_execution_parameter_value] )
executes packages (via [SSISDB].[catalog].[start_execution] )
We use [SSISDB].[internal].[execution_parameter_values] to check that parameter values are being replaced during run time with the connection string we stored in the backend.
What's interesting is that, even though Oracle connection string is being replaced during runtime, the package still tries to use the connection string it has been complied with(Project Params). We do not have the same issue when connecting to a SQL Source in a similar fashion.
Do you have any suggestions? Is it a known issue?
Found the solution. Turns out that the oracle connection string that we stored in the table did not prefix the server name with "SERVER = ". The connection string would straight away start with For Eg - 'x1abc01.something.com:1234/x1abc01;ORACLEHOME=;ORACLEHOME64=;WINAUTH=0;'. Changed the connection string to 'SERVER = x1abc01.something.com:1234/x1abc01;ORACLEHOME=;ORACLEHOME64=;WINAUTH=0;' and it started working now. We tested it by deploying the ssis solution with one connection string and changing it with a different connection string from the database and the overwritten value persists.
However, its still bizarre where the disconnect happens when the run time connection string has an invalid value and its not reported out as an error and ssis quietly switches to design time value in Project Param.

Password of connection string in Pass Through Query properties

I am calling oracle stored procedures via Pass Through Query in MS Access.
In pass through query properties we are saving connection string with password. I don't want to allow the user to see the password.
Is there any way to encrypt the password in pass through query.
Connection string contains
ODBC;DSN=NEW_ODBC;UID=XXXXX;PWD=XXXXXXX;DBQ=XXXXXX;DBA=W;APA=T;EXC=F;FEN=T;QTO=T;FRC=10;FDL=10;LOB=T;RST=T;BTD=F;BNF=F;BAM=IfAllSuccessful;NUM=NLS;DPM=F;MTS=T;MDI=F;CSR=F;FWC=F;FBS=64000;TLO=O;MLD=0;ODA=F;
I am using docmd to call the pass through query
DoCmd.OpenQuery "qry_into_table", acViewNormal, acEdit
If i remove the PWD from the above string, every time it ask for the password to enter. I heard about the concept to use DSN less connection but i am not sure how to create dsn less connection for odbc. I am using user dsn.
Is there any way to encrypt the password or user can not see the password??
Thanks
If you link all your tables and also that of pass-though queries and LEAVE OUT the uid/password, then your quires and linked tables etc. will run just fine. You need to execute JUST ONE logon command at application start.
So in your DSNless re-link code, include the pass-though queries.
How to eliminate the need to include the user id and password in linked tables it outlined here:
Power Tip: Improve the security of database connections
https://www.microsoft.com/en-us/microsoft-365/blog/2011/04/08/power-tip-improve-the-security-of-database-connections/
The above also works for pass-through queries.
The result is then you can execute the pass though query like this in code:
Currentdb.Execute "qry_into_table"
And you can even pass values to a stored procedure with this code:
With Currentdb.QueryDefs("MyPass")
.SQL = "exec sp_MyUpdate " & strParm1
.execute
End With
Note how clean, and how we don't have to deal with connection strings in code with the above.
So the above power tip really eliminates huge amounts of code, eliminates the need to introduce ADO for calling stored procedures, and removes the need of having to deal with messy connection strings in code.

Multiple readers access database

I've read about an option in the connection string called MARS (MultipleActiveResultSets), but from what I've gathered it is only a valid argument for Sql Server 2005. Is there an equivalent setting for OleDB to open an access database with multiple readers?
I am aware that I can create multiple connections, one for each reader. I am currently doing this, but it would preferable to execute both readers on a single connection. Here's my current connection string:
"Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\MyDb.mdb;Jet OLEDB:Database Password=MyPassword"

Connect VB with SQL (Local + unknown database name)

Basically I want to know if it is possible to connect my VB app with SQL server on the same Computer (Local) and interrogate it for a list of database names.
I just want to know if that is possible.
sqlConn.ConnectionString = Data Source=local;Initial Catalog=unknownDatabase;User Id=knownUsername;Password=knownPassword;"
Fill DataBase Names to Drop Down
User Selects DB Name
Change Connection to now Known DatabaseName
Of course it is possible.. You can create a connection to a local SQL instance the same way you'd create a connection to a remote one.
Then to get a list of the databases on the server, execute "SELECT Name FROM master.sys.databases"

SQL server - execute scalar function without specifing db name

I have a user defined SQL function that I am able to call from management studio using syntax dbo.Function(arg)
Now, when I have to call this function from C# if I don't specify **dbname**.dbo.Function(arg) I get an error that SQL server does not find this user defined function. How can I solve this without specifing dbname ? I already connect to the server using a connection string that specifies the "initial catalog = dbname"
It seems that I cannot reproduce mentioned behavior at this point :-) (either using SQL server 2005 or 2008) I have to put this question on hold
Your connection string needs to specify the database to use initially. It might look something like this:
var cn = new SqlConnection(
"SERVER=SomeServer;DATABASE=SomeDb;Integrated Security=SSPI;"
);
Without that, you're probably being dumped into the master database, which is why you need to fully qualify the function name.