Connecting to the Database on client side - sql

I am working on a standalone desktop application. I am Using an mdf file for the database and I have two connection strings
Server =.\SQLExpress;
AttachDbFilename = C:\MyFolder\MyDataFile.mdf;
Database = dbname;
Trusted_Connection = Yes;
I don't want to use this string because its not necessary that the client will install it on same path
Server =.\SQLExpress;
AttachDbFilename =|Directory\MyDataFile.mdf;
Database = dbname;
Trusted_Connection = Yes;
and this shows
error 40: Unable to connect sql server when installed on clients PC

You need to store in variable entire connection string, it'll work.
If it don't, please share your code that acquire connection.

Related

Connecting Azure SQL databse to shinyapps.io

I have been trying to connect SQL database (in Azure) to shinyapp deployed in shinyapps.io, but I could connect to the database from local R console. Please let me know what I am doing wrong?
This is the connection string in Azure SQL DB.
And this is what I am trying from local R console and the connection was successful.
con <- dbConnect(
odbc(),
Driver = "SQL Server Native Client 11.0",
Server = "xxxx.database.windows.net",
Database = "hist_data",
UID = "narendra",
PWD = "xxx",
Port = 1433
)
I had also connected the Database to the local SSMS.
When I deploy the app, getting error:
nanodbc/nanodbc.cpp:1021: 00000: [unixODBC][Driver Manager]Can't open lib 'SQL Server Native Client 11.0' : file not found
I tried to set Driver = "FreeTDS" along with TDS_Version, but it didn't work. However, when I removed the TDS_Version, the app started working on shinyapps.io.
I resolved it by removing "tcp:" from the server name prefix. You also need to adjust the driver name to "SQLServer".

Connecting to different servers in SQL Server

Scenario- We have more than 100 stores and each store has a server. I've created one application to install each users desktop. Their system needs to be connected with their store server, we have local database in place with the application which has procedures which will be interacting with the server. I can't hard code the server name in each procedures and in the SQL, Using DBLink didn't work.
What is the best way to achieve this?
At login page/form in addition to Username and Password, add Server field.
Your app can list any available sql servers on the network if there is only one then connect to it. If more than one put a dialog up to the users to confirm.
You can use:
sqlcmd -L
That will return any visible sql servers
You have to say some way to the Application installed ,which server to connect to..
there are many approaches...
1.Store connection details in a table from which the application can read to like below
storeid connectionstring
1 somestring
So application at startup event,figure out which connection string to connect to..This approach would not require recompiling app and installing it everytime whenever store or connection string changes
2.In WebConfig,store connection details as key value pair like below..
<add key="storeid1" value="server=localhost;database=myDb;uid=myUser;password=myPass;" />
and then connect based on storeid,but this requires change in config file and app installation ,whenever a connection string or Storeid changes

How to include .sdf database file when packaging my application

I was able to create an application in VB.net that talks to a SQL Server CE database.
Now i want to deploy this application using the publish feature with clickonce.
An issue im having tho is when I install the application on another computer it keeps looking for my sql server on my development pc.
Ive added the .sdf when i package my solution but its still an issue.
How do I change my connection string to connect to the .sdf file that I included in the package?
this is my current connection string which looks at the sql server:
connectionString = " Data Source=CHRIS-PC\SQLEXPRESS;Initial Catalog=ce_db;Integrated Security=True"
Thanks !!
connectionString = "Data Source=localhost\SQLEXPRESS;Initial Catalog=ce_db;Integrated Security=True"
or perhaps
connectionString = "Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\ce_db.sdf;Persist Security Info=False;";
this connection string would work if you had the database in the same directory as the executable using it.
essencially the data source is either the database server that you are connecting to, or the database file that you are connecting to.
If you are using a file then you have only one catalog. and if you are using a server then you will want to specify a specific catalog using the Initial catalog attribute of the connection string.

How can i test my TSQL syntax?

Quick question: How do I get some kind of database to use to test my SQL syntax and create basic data.
I have Sqlite Code which I'll soon put on a server. I have SQL Server 2008 installed with visual studio 2010. I tried connecting to the database and had no luck.
I also tried using an .mdf file instead thinking it's a file and I won't have connectivity issues. Wrong, I still couldn't connect and I used this site to help me (i'm aware its 2005)
In that case I used:
var conn = new SqlConnection(#"Server=.\SQLExpress;AttachDbFilename=C:\dev\src\test\SQL_DB_VS_Test\test.mdf;Database=dbo;Trusted_Connection=Yes;");
exception
Unable to open the physical file "C:\dev\src\test\SQL_DB_VS_Test\test.mdf".
Operating system error 5: "5(Access is denied.)".
Cannot attach the file 'C:\dev\src\test\SQL_DB_VS_Test\test.mdf' as database 'dbo'.
With trusted = no I get Login failed for user ''. (What user am I suppose to set?). I created the .mdf with Visual Studio somehow.
What if you try this connection string:
var conn = new SqlConnection(#"Server=.\SQLExpress;
AttachDbFilename=C:\dev\src\test\SQL_DB_VS_Test\test.mdf;
Database=test;Integrated Security=SSPI;");
I don't think it's a good idea to call your database "dbo" (that's a SQL Server keyword - I wouldn't use it for my own purposes!), and also I believe you need to use Integrated Security=SSPI; to define Windows authentication - Trusted_Connection is not used for SQL Server connection strings, AFAIK.
Have you tried using SSMS to access your local instance? It's helpful for getting connected and getting everything setup. Also, I think the default install of Sql Express with VS only support trusted connections.
After creating the mdf file with visual studios right click the mdf and select properties. In it you'll see a row called Connection String. Copy/paste it into your app and it should connect. The key part is User Instance=True

Mysql migration tool, source parameter string | connection string for sql server

I always got failed to "fetching of list failed error".
This is my connection string in ASP.NET
"Data Source=maywood\XSQLSERVER;Initial Catalog=maywood_test;Integrated Security=SSPI"
What exactly should I input at MySQL migration tool for source parameter string
FYI,'maywood' is my computer name and I am using SQL Server 2000.
You have integrated Integrated Security=SSPI set -- perhaps you should try setting the UID and Password manually to values that you know are correct.
Data Source=maywood\XSQLSERVER;Initial Catalog=maywood_test;
User ID=myUsername;Password=myPassword;
Integrated Security uses the credentials from the Windows system that you're using to log in -- these credentials might not be valid for the database you're trying to access.