I realize there's similarly titled posts here, I've searched through all of them without finding my solution.
I'm uploading an Excel file to SQL Server in ASP.net MVC. Here's my connection string:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebApplication12-20180107114522.mdf;Initial Catalog=aspnet-WebApplication12-20180107114522;Integrated Security=True;" providerName="System.Data.SqlClient" />
And here's my Excel file connection string:
string constr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", filepath);
The rest of my code uses SqlConnection / SqlCommand etc, rather than oleDB commands.
You use SqlConnection to connect to SQL Server databases. That connection type doesn't understand "Provider" in the connection string.
You need to use the appropriate connection type for Jet databases, e.g. OleDbConnection.
Related
Ive been working on a web app for the past month and now its time to deploy it on real windows server.The problem is once i deployed it my default localdb code first SQL database no longer works with this code:
[SqlException (0x80131904): 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: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.
)]
Source Error:
Line 11: var manager = new UserManager();
Line 12: var user = new ApplicationUser() { UserName = UserName.Text };
Line 13: IdentityResult result = manager.Create(user, Password.Text);
Line 14: if (result.Succeeded)
Line 15: {
my web.config string :
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-projecttwo-80a6f83e-fca9-45c3-869f-c5c625497346;AttachDbFilename=|DataDirectory|\aspnet-projecttwo-80a6f83e-fca9-45c3-869f-c5c625497346.mdf;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>
Its so frustrating im kinda lost , i dont want to build my login and registration from scratch i just want to use the provided ones from Visual Studio 2017 and there is no tutorial up to date how to do it.
I have SQL Express on my computer installed and its currently running.
There are possible solutions to solve LocalDB instance issue:
1) If the database is already created (as given by AttachDbFilename=|DataDirectory|\aspnet-projecttwo-80a6f83e-fca9-45c3-869f-c5c625497346.mdf), remove the section so that it only contains Initial Catalog shown below.
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-projecttwo-80a6f83e-fca9-45c3-869f-c5c625497346;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>
2) Set IIS application pool for the MVC application to either LocalSystem or NetworkService from ApplicationPoolIdentity - if you suspect a permission-related issue for the application.
3) Find out \%WinDir%\System32\inetsrv\config\applicationHost.config file and enable setProfileEnvironment="true" (restart the application pool afterwards):
<!-- use application pool name depending which pool that your MVC site used -->
<add name="DefaultAppPool" ...>
<processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
</add>
Further references:
SQL Server Connection Strings
Using LocalDB with Full IIS Series: (Part 1) (Part 2)
Similar issue:
Deployment of SQL Express LocalDB on IIS
What is the correct format of the Oracle connection string in MVC 6?
My dependencies:
"EntityFramework": "5.0.0",
"Oracle.ManagedDataAccess": "12.1.2400",
"Oracle.ManagedDataAccess.EntityFramework": "12.1.2400",
My connection string :
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Data Source=myHost:myPort/mydb;User Id=myUser;Password=myPassword"
}
}
}
Reported error:
{"An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct."}
In MVC 5 I use this:
<add name="DefaultConnection" connectionString="metadata=res://*/App.csdl|res://*/App.ssdl|res://*/App.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="DATA SOURCE=myHost:myPort/myDb;PASSWORD=myPassword;PERSIST SECURITY INFO=True;USER ID=myUser;Connection Timeout=900"" providerName="System.Data.EntityClient" />
In your Example your are using Managed Driver as Data Provider to use oracle DB.
But in your EF connection string your are mentioning different Provider (providerName="System.Data.EntityClient") while you should use (providerName="Oracle.ManagedDataAccess.Client")
See correct connection string below :
<add name="TestContext" providerName="Oracle.ManagedDataAccess.Client" connectionString="User Id=test;Password=testpassword;Data Source=eftest" />
For more details Please refer to the following link :
https://docs.oracle.com/cd/E56485_01/win.121/e55744/InstallEntityConfig.htm#ODPNT8266
I have a connection string in my web.config file like this
<add name="ModelDbContext"
connectionString="metadata=res://*/Models.DizlyModel.csdl|res://*/Models.DizlyModel.ssdl|res://*/Models.DizlyModel.msl;provider=System.Data.SqlClient;provider connection string="data source={Server},1433;initial catalog=Dizlybeta;user id={id};Password={Password};MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
but then I go to my hosted azure website and place this as connection
And then I replace the hidden security with
metadata=res://*/Models.DizlyModel.csdl|res://*/Models.DizlyModel.ssdl|res://*/Models.DizlyModel.msl;provider=System.Data.SqlClient;provider connection string="data source={Server},1433;initial catalog=Dizlybeta;user id={id};Password={Password};MultipleActiveResultSets=True;App=EntityFramework"
but it still doesn't work, can anyone help me with how to place the right connection string in the website configuration?
Something to try:
Use a SqlClient connection string instead of an EntityClient connection string.
In that case, you'll probably need to pass the name of the connection string to the DbConext constructor, which you can do in one place like:
public MyDbContext : base("sqlClientConnectionStringName")
{
}
You might also need to ensure a SqlClient provider exists. Check the docs for more.
I am getting a "keyword not supported error" when I try to connect to a LocalDB database using ObjectContext.
This is my connection string:
<add name="connStr" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=proj1db;Integrated Security=True" />
and this is the code that tries to create an instance of the ObjectContext:
var connectionString = ConfigurationManager
.ConnectionStrings["connStr"]
.ConnectionString;
ObjectContext _context = new ObjectContext(connectionString);
The last line throws System.ArgumentException: Keyword not supported: 'data source'.
I am using Visual Studio 2012 for Web and targeting .NET Framework 4.5. I have LocalDB installed on my machine.
If I use DbContext instead it works:
public class proj1dbContext: DbContext
{
public proj1dbContext() : base("name=connStr")
...
It seems that this is a similar question
Help with EF Code first connection string
but unfortunately it does not give a definitive answer to why instantiating ObjectContext throws that error.
Any help is appreaciated. Thanks!
ObjectContext takes an EF connection string (with Metadata and Provider Connection String keywords), not a provider-specific connection string.
You can't use ObjectContext with Code-First; ObjectContext requires the metadata XML files.
updated question
How do I specify the data source to my local MSSQL server?
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(localhost)\SQLEXPRESS;Initial Catalog=bladb;Integrated Security=true" providerName="System.Data.SqlClient" />
</connectionStrings>
I keep getting a "data source not found" exception no matter what I try.
Yep. Your application needs to know the location of the database, the username, and password to authenticate with.
You call this a magic string but I don't know what other magic method you are thinking of where your application could automatically know the IP Address of your database, the Username, and the Password.
Most of the time when you setup your database they create this connection string for you so you can just copy and paste it in. It is just a one time thing so it shouldn't be too big of a deal.
You don't put parenthesis around localhost; it's just for (local) or (LocalDb)\v11.0. For localhost, it would be: DataSource=localhost for the default, unnamed local database (if one exists, which is not necessarily) or DataSource=localhost\SQLExpress for the default named installation of SQL-Server Express.
See http://www.connectionstrings.com/ for more info.
if you define your connection string section in the web.config as
<connectionStrings configSource="connections.config"/>
It will point to a separate config file, just containing the connection strings.
You can then add connection strings in code via the code below.
Note the advantage of this is that is does not have to reboot the website. Happy days...
using (StreamWriter writer = File.CreateText(#"c:\code\AVIVA_site\connections.config"))
{
writer.WriteLine("<connectionStrings>");
for (int i = 0; name.Length > i; i++)
{
writer.WriteLine("<add name=\"{0}\" connectionString=\"{1}\" providerName=\"{2}\" />", name[i], conString[i], provider[i]);
}
writer.WriteLine("</connectionStrings>");
writer.Close();
}
For the love of god...
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection(
"DefaultConnection",
"Users",
"UserId",
"UserName",
true);
}
Instead of adding "DefaultConnection" I added the Configuration.ConnectionStrings["DefaultConnection"] instead.
The problem wasn't in the connection string in the web.config, but in the initializedatabase call...