Invalid value for key attachdbfilename C# - c#

I'm using this tutorial to create my first SQL Server database which is linked with my C# Windows form application. However when I compile my application in VS 2012 I get the following error:
"Invalid value for key attachdbfilename".
This is my connection string:
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\bin\Debug\Calculations.mdf;Integrated Security=True
I have searched many sites to resolve my problem but it's still persisting and I'm running out of ideas.
PS: In the connection string Calculations.mdf is my database name.

At AttachDbFilename should be the path to your mdf file.

Replace (LocalDB)\v11.0 by ., so it should be like this :
Data Source=.;AttachDbFilename=|DataDirectory|\bin\Debug\Calculations.mdf;Integrated Security=True

For your AttachDbFilename value, instead of using |DataDirectory|\bin\Debug\Calculations.mdf, try to give a full path like "C:\Project\Calculations.mdf". Because you can't verify that your path is correct with your current method.

Related

How to solve "Specified cast is not valid" when I tried to connect database

I am building an GUI and I used connection string to connect the sql server database, later I deleted the previous database and build a new one with different name, and change the connection string in app.config file. But after then it always showed that "Specified cast is not valid" and can't connect to DB anymore.
Could anyone can help how to connect to the Database again?
Use the AppSettings GUI or the project settings GUI to edit the app config. It's possible that you changed the name of the actual connection string and now the typed Settings class is no longer finding your setting.

SQL Server connection string specified without a computer name? How can that be?

I'm working from a code base I downloaded from a repository, and it is likely that I'm missing a system or local setting.
In Web.Config, I have this connection string:
<add name="Context"
connectionString="Data Source=InstanceName;
Initial Catalog=MyProduct;
Integrated Security=True;
Connect Timeout=15;
Encrypt=False;
TrustServerCertificate=False"
providerName="System.Data.SqlClient" />
(indentation mine)
Normally I would have expected the Data Source to be \\ComputerName\InstanceName or at least .\InstanceName if the SQL Server is on the same host. But here, nothing. The initially uploaded project had a local database, on the developer's machine. I can get the connection to work if I add .\, but I don't understand how only specifying the instance name can work. So, how can it?
The instance name is needed only if you want to connect to a named instance.
If your install of Sql Server hasn't created a named instance then the default for the instance is MSSQLSERVER and you DON'T need to specify that part on the connection string.
However, the computer name part is required but it could be expressed in various form
a point to mean the local computer
an IP address (local or not)
a server name recognized by the DNS system of your lan
the special string (LOCAL)
More info on the Data Source key could be found on MSDN docs for ConnectionString
Could it be that the InstanceName is the name of an ODBC Data Source that already has the target server configured, and the other employees have a corresponding ODBC data source set up?
The other option is that the connectionstring is modified before being passed to a data connector, so "MyMachineName" + connectionstring is happening somewhere (perhaps to separate production and development environments?
Also, double check the App_Data folder to make sure some sort of file-based database isn't being accessed.
In addition to what #Steve has already mentioned local or current machine can also be referred by a special string localhost. Please refer to below post:
What is the sql connection string I need to use to access localhost\SQLEXPRESS with Windows Authentication or SQL Authentication?

ORA-01005: null password given; logon denied

I have created a SSAS Database pro grammatically using SQL Server AMO namespaces. It has create successfully. But When I'm going to explore the data using BIDS of the Data source It is prompting an error like this,
ORA-01005: null password given; logon denied
My Connection String is
#"Provider=OraOLEDB.Oracle.1;Data Source=//cmbtrndb02/hector;Password=trnint16;User ID=trnint16;Intergrated Security=yes";
I have used the ConnectionStringSecurity Property as "unchanged" but it gives the same error.
ds.ConnectionStringSecurity = ConnectionStringSecurity.Unchanged;
What could be the problem for this.
Old post but maybe this helps someone out (like it helped me):
If you're using "System.Data.Entity.Infrastructure" on your project (maybe you are casting the DbContext to an ObjectContext for some reason?) you will be getting this error (ORA-01005).
As I finally found out, you can easily avoid it just by adding "Persist Security Info=true" on your "Oracle.ManagedDataAccess.Client"'s string connection.
If for some reason you really need to obtain the connection string from other connection, like user2306318 refers, you need to add the option "Persist Security Info=True" to your connection string.
You just need to include the password in the connectionstring as follows:
if your user name is carl and your password is bullet56 then:
connectionString="Data Source=LocalHost;User ID=carl;Unicode=True;Password=bullet56"
Hope it will be helpful.
The new connection objects are losing the passwords when you use objcon.connectstring. Maybe you are reading the connectstring from the connection object (=without password) somewhere in the code instead of providing the complete connectstring including the password.
For example
gacutil /u Policy.2.102.Oracle.DataAccess
set your version assembly
I faced the same error in spring boot with oracle 19c, I just changed the jdk version to latest i.e 13 and it solved my problem.

Connection string for remote SQL server 2008 without SQL Management Studio

I know its a classic problem, but I am too novice to even comprehend the basics (sorry). I have this ASP.NET "Website" with databases (SQL Server 2008), which I am trying to deploy on a Remote Host Server (HostGator) using WebSite Copy tool of VS 2010.
In VS 2010, I could grab the Connection string directly through the "property" of the database, which turned out to be:
Data
Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\myName\Documents\Visual
Studio 2010\WebSites\Experiment\App_Data\data.mdf";Integrated
Security=True;User Instance=True
Now, I know that a website called http://connectionstrings.com/ exists, but its too overwhelming for me at first glance.
My current connection string:
Data
Source=domainName.com\SQLEXPRESS;AttachDbFilename=C:\inetpub\vhosts\domainName\httpdocs\Experiment\App_Data\data.mdf;Integrated
Security=True;User Instance=True
I am pretty sure, that this is wrong as its giving me the classic error of:
A network-related or instance-specific error occurred while
establishing a connection to SQL Server..
But I don't know how to fix it. Where to start looking, how to initiate, how to get the parameters required to build the connection string. How to find the path of the AttachDbFilename ? Can you please help?
I believe, that connection string is the only parameter which needs to be modified. If there's something else, then please suggest? Also, I keep coming across the usage of SQL Management Studio to make things work, I don't have that, so is it hard to accomplish this without it?
Please help, thanks.
You can't specify a directory relative to your own machine.
Change AttachDbFilename to
AttachDbFilename=|DataDirectory|data.mdf
The specifics for Data Source should be given to you from your hosting company, as well the security model (probably you need to use a specific user)
You can download SqlServer Management Studio Express from this link
Man,
What do you think about HttpServerUtility.MapPath Method. It returns the physical file path that corresponds to the specified virtual path on the Web server.
[]'s
When you get your hosting. They will provide the connection string for you. like this
<add name="ConnectionStringName"
connectionString="Data Source=localhost;Database=YourDatabaseName;Uid=YourUserNameForDatabase;password=yourpassword;"
providerName="System.Data.SqlClient" />
Let me know, if you need more help

SQL Server 2008 connection with C#

No matter what I try I keep getting the following error when I try to run a simple select query to the database I created (I've run the exact query in the Management studio and it works fine and the login used is in the sysadmin group):
{"Cannot open database \"Test\" requested by the login. The login
failed.\r\nLogin failed for user
'JackLarson'."} System.SystemException
{System.Data.SqlClient.SqlException}
My connection string is:
Data Source=.\MPIT_TEST;Initial Catalog=Test;Integrated
Security=True;Connect Timeout=30;User Instance=True
you have to set your server name in your connection string
if your servername is SERVERSQLCOMPUTER your connection string will be:
Data Source=SERVERSQLCOMPUTER \MPIT_TEST;Initial Catalog=[Test_MPITRACKER];Integrated Security=True;Connect Timeout=30;User Instance=True
Moreover, always specify server name to be able to use your application on every computer in network entreprise (servername\serverinstance is an absolute path)
.\serverinstance means that your application is on the same computer than your Sql server
Take a look at the error message. It says it cannot find database named Test although you specified Test_MPITRACKER in the connection string. It looks like there is an issue when you have a database with undescore _ character in its name.
The easiest way would be to remove the undescore from the name of the database.
If you want to use database name with the underscore character, try to wrap it with square brackets, so it looks like this:
Data Source=.\MPIT_TEST;Initial Catalog=[Test_MPITRACKER];Integrated Security=True;Connect Timeout=30;User Instance=True
However, I haven't tested this solution, it's based on information found here: Database created in SQL Server Management Studios cannot be found by Visual Studio 2010.
EDIT
According to the conversation in the comments this is not the cause of the issue in this case.

Categories