Connection strings LocalDB in ASP.NET WebForms Visual Studio - asp.net

I use ASP.NET sample application using by Microsoft: WingtipToys.
It uses LocalDB.
Which is the difference about connection strings?
<add name="DefaultConnection"
connectionString="Data Source=(LocalDB)\V11.0;Initial Catalog=aspnet-WingtipToys;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WingtipToys-20131223105750.mdf;Initial Catalog=aspnet-WingtipToys-20131223105750;Integrated Security=True"
providerName="System.Data.SqlClient" />
I get error connection using Data Source=(LocalDB)\V11.0;Initial Catalog=aspnet-WingtipToys;Integrated Security=True
sqlocaldb.exe command:
C:\Users\Espinete>sqllocaldb.exe v
Microsoft SQL Server 2012 (11.0.3156.0)
Microsoft SQL Server 2014 (12.0.2000.8)
Microsoft SQL Server 2016 Release Candidate 0 (RC0) (13.0.1100.286)
C:\Users\Espinete>sqllocaldb.exe i
MSSQLLocalDB
Projects
ProjectsV13
v11.0

First, LocalDB connection strings usually has database file naming convention using unique number suffix with this format:
aspnet-[project name]-yyyyMMddHHmmss.mdf
The unique number suffix represents database creation date & time stamp at nearly same time as project creation time.
By default, when creating a LocalDB database file which not defined in any SQL Server instance, the connection string in web.config uses AttachDBFileName setting. The difference between AttachDBFileName & Initial Catalog setting as described in Common Connection String Settings is there:
AttachDbFileName
This setting specifies the path and name of the database file for SQL
Server Express or LocalDB databases that are not defined in the local
SQL Server Express instance. This setting is typically used for database files that you keep in the App_Data folder.
Initial Catalog
This setting specifies the name of the database in the SQL Server
instance catalog. In LocalDB connection strings, the Visual Studio web project templates add a unique number as a suffix to both the file name and the Initial Catalog setting. The reason for this is to avoid database name collisions in the SQL Server Express LocalDB instance.
From descriptions above, the first connection string certainly doesn't work because LocalDB doesn't know which database file should be attached into its instance. To use Initial Catalog without AttachDBFileName setting you need to attach created LocalDB database in an SQL Server instance (either using SSMS or sqlcmd command line utility to create DB catalog).
Difference summary between first and second connection string:
Initial Catalog without AttachDBFileName => trying to connect using already attached database name defined in SQL Server instance catalog.
Initial Catalog with AttachDBFileName => trying to attach manually & connect using database MDF file inside App_Data directory with Initial Catalog set to attached database file name in LocalDB instance.
NB: sqllocaldb.exe v command lists all installed LocalDB versions in your machine, where sqllocaldb.exe i lists all shared existing LocalDB instances (more info in SqlLocalDB.exe docs).
Additional reference:
SqlConnection.ConnectionString Property (MSDN)

Related

Amazon RDS connection string setup

I have successfully created an Amazon RDS database, now I have also connected to this database in my ASP.Net application in Visual Studio. However, previously this database was stored locally, so now I am trying to migrate to the cloud (Amazon RDS).
How do I setup my connection string? In my app.config file, as you can see, previously it is set to the local database.
I am using the latest version of Entity Framework
<add name="BookStoreEntityModelContainer" connectionString="metadata=res://*/BookStoreEntityModel.csdl|res://*/BookStoreEntityModel.ssdl|res://*/BookStoreEntityModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=(localdb)\MSSQLLocalDb;Initial Catalog=BookStore;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
Disclaimer, this is what worked for me on .netCore 3.1 with EFCore, but the connection string should be interpreted the same for your case.
This is a truncated example of what we use for our RDS MSSQL server for connectionString value:
Server=tcp:example-endpoint-db.rds.amazonaws.com,1234; Initial Catalog=MyApp.DatabaseName; User=myappusername; Password=myapppassword; MultipleActiveResultSets=True; Application Name=MyApp.Api
The example-endpoint-db.rds.amazonaws.com and port can be derived from your RDS web console after your selected your DB as referenced:

Create database on Azure using Entiitty Framework

I want to create ASP.NET MVC app using database on Azure. I have already written model classes for database. But how can attach this database or tables on azure?? It's connections string for my already created DB, which i want to use.
<add name="AccomodationContext" connectionString="Server=tcp:student-docs.database.windows.net,1433;Database=docDb;User ID={username};Password={password};Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient" />
It's empty, I would like to use it to has a tables created from my model classes, or create a new database. But I can't find, how should look correct connection string. I know that for LocalDB there is 'AttachDbFilename' in connection string.
You should have a connection called DefaultConnection in your web.config that you can place your connections string in.
You can get the actual connection string directly from your Azure portal.

Using named instance in connection string

I want to upload my DNN site in customer's host, they use SQL Server 2005 and they use named instance, how can I use named space in connection string ?!
<add key="SiteSqlServer"
value="Data Source=79.175.164.226,2005; Initial Catalog=jdmedu; User ID=; Password=;" />
DNN shows database connection error.
Can you check that you can construct ODBC Connections to the DB (outside of your application).
I Suspected that, the problem is in the Configuration of the DB Instance and it can be solved from the SQL Server Configuration Manager.
Also, can you check
http://www.connectionstrings.com/sqlconnection/connect-via-an-ip-address/
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;
Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;

MVC 3 ASPNETDB.MDF SqlExpress Database Connection String on Web Hosting

I think this is more of a connection string issue that MVC 3, so sorry in advance.
I've created a website in MVC 3 with the default database that is created when you register a new user. It works fine local but not on my web hosting where I receive the following error:
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
The default connection strings are:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
<add name="ASPNETDBConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
Should I specify a connection string with an explicit IP address of the server or retain the .\SQLEXPRESS command? If I need to specify a username and password then what should I use? I've never used a local SQLExpress database before so I'm clueless (which I'm sure is apparent).
If you cannot use SQL Express, then you could consider an embedded SQL Engine:
SQL Compact Edition or SQLLite
I've never used SQL CE, but SQLLite is just a single DLL.
Obviously you don't have all the features of a fully fledged database engine, but they are still pretty powerful.
Generally you will need use a connection string with a specific IP Address, Username, Password, etc.
Who are you hosting with? Generally a .net host provider will give you the connection string to use to connect to your database. You can copy and paste in to you web.config. Also, when they create the database for you, they generally specify the username, password, and database name so you will have to change all of that.
This is visual studio has Web.config.debug and Web.config.Release with the xml transformations. This way when you publish to the site, the connection string is automatically changed from your local connection string to the connection string used in your production environment.
You probably need to follow this article?
http://www.asp.net/mvc/tutorials/authenticating-users-with-forms-authentication-cs
Deafault ".net" installs Application Services to "AttachDBFilename=|DataDirectory|aspnetdb.mdf"
Then you create your own DB and now you're using two DB's to store data that could just be in one.
If you read the above article it shows you how to combine everything into one DB.
When you deploy to the hosting server (and it has a limit on how many DB's you can use or isn't even running sqlexpress) neither DB attach. So you need to make a "transform file" for your web.config file (usually Debug and Release) which is basically another web.config but only with the lines of config in that you need to change when the site is deployed to the hosting server.
So your hosting provider will give you the details needed for the connection string on the server. You then put that in the "web.config.release" file and when you compile and publish the "release" version of your site you will get a web.config file that has the correct connection string data in.

asp.net mvc unable to connect to SqlServer when creating the users from Register page

I have Visual studio 2008 and Installed MVC2 RTM.
For data base I have Sql Server 2008.
When I first create a project of type "asp.net mvc web application"
I get the default structure of the project and app_data.
After I run the project and trying to Register a new user It is giving the following error.
The user instance login flag is not supported on this version of SQL Server. The connection will be closed.
The above message is displayed, while the connection string value is
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
I'm getting the below error when data source=.\SQLEXPRESS
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Any Idea Why it is not connecting to the data base?
Thanks in advance.
It looks like you're using a full SQL Server.
Based on the link in Anand Kumar M's comment, you can't use AttachDBFilename when you're using a full SQL server, so try removing that from your connection string and attaching the database to your SQL server
For attaching the database to your SQL Server instance.
http://msdn.microsoft.com/en-us/library/ms190209.aspx
Once you've done that, remove the AttachDbFilename clause from your connectionstring and replace it with the following:
Initial Catalog=Mydatabase;
Where MyDatabase is the name of the database as it shows in SSMS.
Hope this helps

Resources