WPF app SQL connection string error - c#

This question has probably been asked a thousand times but all the answers that I have seen are not relevant to my question.
I have an asp.net web site setup locally to connect to sql and I used the exact same connection strings in the WPF App.config file
<connectionStrings>
<add name="TrackingConnection" providerName="System.Data.SqlClient" connectionString="Data Source=RAIDSHIPPING\FRTOOLS; Initial Catalog=SHIPPING; User Id=sa; Password=password" />
<add name="RMAConnection" providerName="System.Data.SqlClient" connectionString="Data Source=RAIDSHIPPING\FRTOOLS; Initial Catalog=FRTOOLSRMA; User Id=sa; Password=password" />
<add name="PickListConnection" providerName="System.Data.SqlClient" connectionString="Data Source=RAIDSHIPPING\FRTOOLS; Initial Catalog=Picklist; User Id=sa; Password=password" />
<add name="ShipWorksConnection" providerName="System.Data.SqlClient" connectionString="Data Source=RAIDSHIPPING\FRTOOLS; Initial Catalog=ShipWorks; User Id=sa; Password=password" />
<add name="SupplyConnection" providerName="System.Data.SqlClient" connectionString="Data Source=RAIDSHIPPING\FRTOOLS; Initial Catalog=SupplyManager; User Id=sa; Password=password" />
</connectionStrings>
As usual this works on the local machine but on the network machine I get a "A network-related or instance-specific error occurred while establishing a connection to SQL Server." error.
Like I said this connection string is copied exactly from the asp.net web application, web.config file connection string which the network machine consumes fine. I have the App.config properties set to Copy always also the Sql server configuration is set up to enable Named Pipes and TCP/IP. Windows Firewall has port 1433 open. I thought the Copy always property being set would have solved the problem since the program wouldn't work because images were causing it to break until I set their properties to copy always. I've restarted the SQL Server and machine.

Related

Login failed while connecting to database

I have created WPF application which has MDF file. When I added MDF file to my solutions folder It automatically created a connection string for me
<add name="Connection"
connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=C:\Users\Public\Desktop\MyDatabase.mdf;Integrated Security=True;"
providerName="System.Data.SqlClient" />
I Deployed my application using SQL Server Compact (as described here). But When I run my Application I am getting exception
Cannot open database "C:\Users\Public\Desktop\MyDatabase.mdf" requested by the login. The login failed.
Login failed for user 'MY-PC\NAME'.
I am using Windows Authentication for my SQL Server
change your connection string as follows:
<add name="Connection"
connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=C:\Users\Public\Desktop\MyDatabase.mdf;persist security info=True;Integrated Security=SSPI;"providerName="System.Data.SqlClient" />

Underlying provider failed to open when I convert database property to do not copy

This is the connection string and when I change the property of the database file (database1.mdf) to "do not copy", it causes an error.
<connectionStrings>
<add name="ClassLibrary1.Properties.Settings.Database1ConnectionString"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
That's probably because of AttachDbFilename=|DataDirectory|\Database1.mdf that you can't not copy the database to the DataDirectory.
The connection string tries to find and attach the database from the given address and it can't find it. you can attach the db first and then use this connection strings:
<add provider connection string="data source=.;
initial catalog=Database1;
integrated security=True;/>

Entity Framework connection string is being rewritten when website is published

When publish a MVC website via Visual Studio it rewrites the connection string for Entity Frameworks with an invalid one.
It appears to be the same invalid one it created when I first imported the database model (not code first) The correct connection string is in my local web.config, and there are no changes being made by the web.release.config file.
Correct connection string:
<add name="Entities" connectionString="metadata=res://*/Models.MODModels.csdl|res://*/Models.MODModels.ssdl|res://*/Models.MODModels.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost;initial catalog=R4S-MOD;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Generated connection String:
<add name="Entities" connectionString="Data Source=localhost;Initial Catalog=R4S-MOD;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework" providerName="System.Data.EntityClient" />
When you do a release publish they should show you a popup where you choose if you want to do filesystem or iis deployment. there is a section in the wizard which has connection string. check it there, if there is already a preselected value for your connection string – qamar

appharbor [Win32Exception (0x80004005): The system cannot find the file specified] with model first EF

I have a small project where I used continuous integration with bitbucket and appharbor When I published the project the login is working normally and so on but other database connections gives the following error:
The system cannot find the file specified
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ComponentModel.Win32Exception: The system cannot find the file specified
here are my local connection strings
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-ZeinyCenter-20130614160141;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
<add name="Model1Container" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=(LocalDb)\v11.0;initial catalog=aspnet-ZeinyCenter-20130614160141;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
and here is the appharbor connection string
Server=576666e4-329e-4511-95b9-a1dd00ff4766.sqlserver.sequelizer.com;Database=db576666e4329e451195b9a1dd00ff4766;User ID=yusmzpttdopksbua;Password=H3Ex6jp3wihobPu3W2KZHBpW7tDk8SWtxYWTxabXGXhVBGTAnRJZz6AgLRzKNe4L;
Can someone please help me
I solved my problem typing the connectionString that appharbor gave me on the Web.config.
Steps:
1) Go to appharbor and select your application.
2) Click on add-on, and select SQL Server (install the free one).
3) Then click on Go to SQL Server.
4) Scroll to the bottom and you will see Connection string.
5) Copy all (Click it and press ctrl+A), the part that says Server=bla bla bla.
6) Now go to Visual Studio --> Web.config and paste it in:
add name="DefaultConnection"
connectionString="Your appharbor connectionString"
providerName="System.Data.SqlClient" />
Example, mine was like this:
add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\v11.0;
Initial Catalog=aspnet-DeliveriandoWebApp-20131019135542;
Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|\aspnet-DeliveriandoWebApp-20131019135542.mdf"
providerName="System.Data.SqlClient" />
Now is like this:
add name="DefaultConnection"
connectionString="Server=8a6b279b-0441-49c7-a97e-a2860049ea89.sqlserver.sequelizer.com;
Database=db8edfdf6c279c044149e7a17ea2860049ea89;
User ID=lolyctkwkaebnkj3;
Password=nYhP4hZKDNMLftEaSyeeeS8eVpbs3H3seUZQUQ8PZzMRXA5hPVfDirfPwZzHfvaFDsz;"
providerName="System.Data.SqlClient" />

How do I build the database connection string if the website is on the same server as the db?

I have a problem with my connection string and I couldn't find anything on the web that could help me. So I have a test website on which I connect to a database which is not on the same server. There it all works fine, the data is being read correctly and the connection is successful.
Now when I try the same on a website which is on the same server than the database it doesn't work. So I figured I can't just connect to the database server as I would from a different server if I am already on that server like this:
<connectionStrings>
<add name="nameOfConnString" connectionString="Data Source=serverName;Initial Catalog=databaseName;User ID=userName;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
Could anyone help me with that?
If you need more information just ask please.
Thanks in advance!
[edit]
Oh I forgot...this is the error message I get:
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: Named
Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Try localhost instead of server name
<connectionStrings>
<add name="nameOfConnString" connectionString="Data Source=localhost;Initial Catalog=databaseName;User ID=userName;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
<connectionStrings>
<add name="nameOfConnString" connectionString="Data Source=.;Initial Catalog=databaseName;User ID=userName;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
Both answers above are correct, however, if you have SQL Express installed (I assume we're talking about MS SQL) then your connection string must be something like this:
<connectionStrings>
<add name="nameOfConnString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=databaseName;User ID=userName;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>

Categories

Resources