Cannot connect to mysql database in .NET MVC3 - asp.net

I've having some trouble in connecting mvc 3 application in .net. I know that mysql database instance has to be created before using web.config file to connect the it. My
connectionStrings as below:
<connectionStrings>
<add name="epmsDb"
connectionString="Server=localhost;Database=database1;
Uid=root;Pwd=mypassword"
providerName="System.Data.SqlClient" />
</connectionStrings>
and I've got a database name call epmsDb created in Server explorer. Then I tried to get the connectionString in the code using this :
string connectionString =
ConfigurationManager.ConnectionStrings["epmsDb"].ConnectionString;
context = new DataContext(connectionString);
usersTable = context.GetTable<UserObj>();
Every time I tried to access the database, I threw this exception:
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)
I've been trying to overcome this for a week now. Does anyone have ideas why this occurres?

Your example tries to connect to an MS SQL database. To connect to a MySQL DB you might want to use the MySQL connector
Then change the ProviderName property:
<connectionStrings>
<add name="epmsDb"
connectionString="Server=localhost;Database=database1;
Uid=root;Pwd=mypassword"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>

Related

A network-related or instance-specific error occurred while establishing a connection to sql server error 26 on stimulsoft Report

I use this connection string in my asp.net
<add name="DB_PersonnelEntities"
connectionString="metadata=res://*/Models.Model.csdl|res://*/Models.Model.ssdl|res://*/Models.Model.msl;provider=System.Data.SqlClient;provider connection string="data source=REGAPP\SQL2014;initial catalog=DB_Personnel;user id=personnel;password=qwe123!;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
and this code for StimulSoft
string connectionstring = ConfigurationManager.ConnectionStrings["DB_PersonnelEntities"].ConnectionString;
string serverlocation = HttpContext.Current.Server.MapPath(string.Empty);
StiReport mystireport = new StiReport();
mystireport.Dictionary.Databases.Add(new Stimulsoft.Report.Dictionary.StiSqlDatabase("DB_PersonnelEntities", connectionstring));
mystireport.Load(serverlocation + "\\Report\\RptPersonnel.mrt");
mystireport.Dictionary.Variables["pid"].Value = id.ToString();
StiWebViewer1.Report = mystireport;
mvList.SetActiveView(vwReport);
The connection string works properly in all SQL connections of the program, except for Stimulsoft Report with the code shown above.
Note: this code and connection work properly for StimulSoft SQL connection and other sql connections on my PC and run from Visual Studio, but when the program is published on the server, the StimulSoft SQL connection shows an error 26.
I changed the connection string like this
<add name="Conn"
connectionString="Data Source=REGAPP\SQL2014;Initial Catalog=DB_Personnel;User Id=personnel;Password=qwe123!"
providerName="System.Data.SqlClient" />
and changed instance name / disabled firewall / checked user permission for procedure.
THANKS FOR HELP
The connection string you showed is an Entity Framework connection string, which I'm pretty sure a third-party reporting tool won't be able to use.
But embedded in that EF connection string is a "raw" ADO.NET connection string - have you tried that??
<add name="Report_PersonnelEntities"
connectionString="Data Source=REGAPP\SQL2014;Initial Catalog=DB_Personnel;User id=sa;password=qwe123!;MultipleActiveResultSets=True;"
providerName="System.Data.SqlClient" />
Word of warning: it is generally recommended NOT to use the sa (system administrator) account which can do literally everything on your server for any programmatic database work - especially not for reporting! Create your own reporting user in SQL Server and use that instead of the all-powerful system admin account!
The StimulSoft Save connection string (in Report.mrt file) That created on my PC. When I Publish My Program, The StimulSoft report Use the connection string embedded in Report.mrt file NOT the web.config.

Error about ASP.NET security configuration(cannot connect to sql )

I've installed SQL Server 2012 and visual studio express for web 2012, I found that I couldn't connect to SQL via asp.net. I installed another SQL Server 2012 instance and changed datasource to (LocalDB)/v11.0, it then works.
But in the security configuration, though I've set the windows authentification,I get this error:
There was a problem with the selected datastore, which could be due to an invalid server name or credentials, or insufficient permissions. It may also be caused by the role manager feature not being enabled. Click the button below to redirect to a page where you can select a new data store.
The following messages may help diagnose the problem: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or cannot be reached. Verify that the instance name is correct and SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26-An error occurred while locating the specified server / instance)”
After testing the provider for storing site management data:Aspnetsqlprovide ’connection with sql,another error happens:
Failed to establish database connection.
If you haven't created a SQL Server database, exit the site administration tool, use the aspnet_regsql command-line utility to create and configure the database, then return to this tool to set up the provider.
Following the error message, I installed aspnetdb to this (LocalDB)/v11.0, and changed the codes in machine.config:
<connectionStrings>
<add name="LocalSqlServer"
connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=aspnetdb;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
I also set IIS connection strings as codes above and changed Application pool identity to“LocalSystem”,but these errors still occurs.
I really hope someone can help me to handle this, thanks

ASP.NET MVC network-related or instance-specific error

I am getting this error
System.Data.SqlClient.SqlException: '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)'
When I try to run my ASP.NET MVC project. Opening it for the first time on this PC.(I try to register a user and the database doesn't get created and I get the error above.) The database should get created the first time I hit the database PS: It's a code first project
I thought I know how to fix it until now.
The project runs on my other PC but on this not.
So I thought I'll go and change the connection string to
<connectionStrings>
<add name="foo"
providerName="System.Data.EntityClient"
connectionString="Data Source=localhost"/>
</connectionStrings>
(just change the Data Source= to localhost)
The constructor in my DbContext is
public EventManagerDbContext() : base("foo")
{
}
Still I can't run my project.
PS: I am able to connect to my database using the SQL Server Management Studio, but not from the project. Why is that?
I realised that the problem was that my connection string was placed in the Data Access Layer. But it has to be in the ASP.NET MVC application right? Would like some clarification.
Provide a complete details to your connectionstring like this:
<add name="foo" connectionString="Data Source=.\SQLEXPRESS2014;Initial Catalog=NameOfYourDB;
Integrated Security=True;"
providerName="System.Data.SqlClient" />
I suppose that your localhost is .\SQLEXPRESS2014 is your local machine server name.
To get the full server name on your part, connect to server, under Server Name, choose Browse for more option, then select Network Servers tab, and it will list your qualified server name:

MY ASPNETDB.MDF would not work in production server? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
A network-related or instance-specific error occurred while establishing a connection to SQL Server.
Today i have create two pages login.aspx using LOgin control and register.aspx using Create user wizard ...
when i use connection string of my production server in my local computer then site works only if my database ASPNETDB.MDF is present in APP_Data Folder in my local computer..
if i rename ASPNETDB.MDF or Delete ASPNETDB.MDF from my local compuet App_Data the the following error occurs ...
An attempt to attach an auto-named database for file C:\Users\Ashish Dobriyal\Documents\Visual Studio 2008\WebSites\VOLVOO\App_Data\ASPNETDB.MDF failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
but
After i publish my webpages ..... and database to my production server .... it produces an 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)
What was the problem actually ... ??
MY PRODUCTION SERVER CONNECTION STRING :
<connectionStrings>
<remove name="ConnectionString"/>
<add name="ConnectionString" connectionString="workstation id=volvobusesindia.mssql.somee.com;packet size=4096;user id=username;pwd=password;data source=dobriyal.mssql.somee.com;persist security info=False;initial catalog=dobriyal" providerName="System.Data.SqlClient"/>
</connectionStrings>
MY LOCAL SERVER CONNECTION STRING :
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
when i use connection string of my
production server in my local computer
then site works only if my database
ASPNETDB.MDF is present in APP_Data
Folder in my local computer..
This is because you need to add access permissions to the directory that you going to put the database. The permissions must be for the user that runs the asp.net pool that you using.
Alternative try this string, for both production and local server and just be sure that your database have the correct permissions for been access from the asp.net running pool account.
This is not depended from what you write on the string but from how you correctly setup the database. Its working 100% so if not play for you search the database permissions, both file and database settings.
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=localhost;Initial Catalog=dobriyal;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>

problem connecting to access database in asp.net 2.0

hi im trying to get a dataset from an access database
im using this connection string:
<connectionStrings>
<add name="SiteConnString" connectionString="Data Source=c:\inetpub\vhosts\db\mainDB.mdb"
providerName="Microsoft.Jet.OLEDB.4.0" />
</connectionStrings>
and this is my call to SqlHelper:
myDataSet = SqlHelper.ExecuteDataset(connString, CommandType.Text, strSQL);
and the error im getting is this:
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 providerName attribute expects an ADO.NET provider class name. Try changing your connection like so:
<connectionStrings>
<add name="SiteConnString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\inetpub\vhosts\db\mainDB.mdb"
providerName="System.Data.OleDb" />
</connectionStrings>
A very handy reference for connection string formats is http://connectionstrings.com

Resources