Error loading data from a C# database application - c#

I have created a C# database application, the database file is of type .mdf . The app works fine on my PC and in other PCs that have MS SQL Server already installed. In other machines that do not have SQL server installed, the installation process shows me an unhandled exception, after that, the app opens but no data are loaded .
The exception says:
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: 52 - unable to locate a Local Database
Runtime installation. Verify that SQL Server Express is properly
installed and that the Local Database Runtime feature is enabled.).
This is the full exception in detail:
*System.Data.SqlClient.SqlException (0x80131904): System.ComponentModel.Win32Exception (0x80004005): The system cannot
find the file specified at
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection, Action`1 wrapCloseInAction) at
...
... at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)
ClientConnectionId:00000000-0000-0000-0000-000000000000 Error
Number:2,State:0,Class:20*
The app can run successfully but no data from database are loaded.
Do I have to install SQL server express in all machines,so the app can load its required data, or just some components of SQL server are enough. Other PCs have Windows update disabled.
For more information here are application files:
Data177k.mdf.deploy
Data177k_log.ldf.deploy
deatach.application
deatach.exe.config.deploy
deatach.exe.deploy
deatach.exe.manifest
setup.exe
What can I do in order that the app to load its database even in PCs that dont have SQL server express installed on.

I recommend you read up on SQL Server LocalDB, which allows you to publish the necessary plumbing for using an .MDF file on a client computer, without installing SQL Server/Express on it.
MSDN SQL 2014 Download and information page

Related

C# app with SqlClient does not work on another computer

I have written an application that uses SqlClient and a local mdf database. However, it does not work on another machine (throwing an exception that the server cannot be run) and I assume this is because there is no SQL server installed? But I thought that using local database and SqlClient elimites the need for the server, something like the old SQL CE. Is that the problem?
EDIT Exception: System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while extablishing 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
As for connection string, I keep the one generated by Visual Studio Express, just modify the path to use the app folder.
SqlConnection s = new SqlConnection(#"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename ="+ System.IO.Path.GetDirectoryName(Application.ExecutablePath)+ #"\Database1.mdf; Integrated Security = True");
Is there a simple way how to rework this so that my app does not require SQL server installed? All I need is to have it use the local database file in its folder.

EF 6 not using connection string in Web.config

I am receiving the error below which for the life of me I cannot resolve. I am using EF 6 with Identity 2 and it was working fine a few days ago.
I fired up VS 2015 yesterday and started receiving the error both when publishing to the test site and locally. It is almost as if EF is looking for a local instance of SQL Express even though Web.config is configured to use a SQL 2014 Standard server.
Here is the error:
Server Error in '/' Application.
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)
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.
SQLExpress database file auto-creation error:
The connection string specifies a local Sql Server Express instance using a database location within the application's App_Data directory. The provider attempted to automatically create the application services database because the provider determined that the database does not exist. The following configuration requirements are necessary to successfully check for existence of the application services database and automatically create the application services database:
If the application is running on either Windows 7 or Windows Server 2008R2, special configuration steps are necessary to enable automatic creation of the provider database. Additional information is available at: http://go.microsoft.com/fwlink/?LinkId=160102. If the application's App_Data directory does not already exist, the web server account must have read and write access to the application's directory. This is necessary because the web server account will automatically create the App_Data directory if it does not already exist.
If the application's App_Data directory already exists, the web server account only requires read and write access to the application's App_Data directory. This is necessary because the web server account will attempt to verify that the Sql Server Express database already exists within the application's App_Data directory. Revoking read access on the App_Data directory from the web server account will prevent the provider from correctly determining if the Sql Server Express database already exists. This will cause an error when the provider attempts to create a duplicate of an already existing database. Write access is required because the web server account's credentials are used when creating the new database.
Sql Server Express must be installed on the machine.
The process identity for the web server account must have a local user profile. See the readme document for details on how to create a local user profile for both machine and domain accounts.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[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: 26 - Error Locating Server/Instance Specified)]
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +92
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +285
This is the connection string and EF config in Web.config:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=SQL5017.Smarterasp.net;Initial Catalog=DB_9CF975_moverlive;User Id=xxxxxxxxx;Password=xxxxxxx;" providerName="System.Data.SqlClient" />
</connectionStrings>
I can connect fine to the DB using SQL Server Management Studio with the above connection string.
What should I look for?
The first place to check is your DB context to ensure you have specified the name of the connection string:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", false)
{
}
// ...
Your connection string looks fine. The error indicates that there is still something in your web.config or code that is looking for App_Data directory which is strange. Have your tried doing a find in your solution for App_Data or |DataDirectory| .
Looks like,There is a reference to the App_data folder somewhere in your code or (LocalDb) or SQlExpress
You don't happen to have a Config Transform file, like Web.Debug.config, or Web.Release.config underneath your Web.config file do you? It might be rewriting your connection string.
Usually Config Transform files do their transformations when you publish a site, but I'm not sure how they work in some of the debugging modes, since there are a few modes for ASP projects like IIS Express and Local IIS.
I had it happen to me today with an App.config file, an App.Debug.config was rewriting my connection string, I didn't even notice that there was a App.Debug.config until I expanded the node. It had a totally different connection string.
After much debugging I found a typo in the namespace in Startup.Auth.cs
This meant that no connection string was passed through to DefaultConnectionFactory which intern meant that The connection factory then uses the context name as the database name in a default connection string. (This default connection string points to .\SQLEXPRESS on the local machine unless a different DefaultConnectionFactory is registered.)
Once the namespace was corrected, everything functioned as planned :-)

Hosting ASP.net app on somee - database update from C# app

I have ASP.NET app with database which is updated by C# application, so database is placed on my local disc. .NET app can only read entries (changes are not allowed).
I can run my .NET app from local host (from Visual Studio or/and IIS) or other computer in network by ipaddress:port_number/page_name.aspx. Everything is working fine.
But now I would like to publish my app on somee. :) I made account and in the same project I added one simple page (just plain text) and that page can be accessed my other computers fron Internet by typing myusername.somee.com/that_simple_page_name.aspx
I found a lot of similar topics on the Internet forums so I made changes:
- TCP/IP enabled in SQL Server Configuration Manager
- exceptions ports 1433 and 1434 (UDP),
- SQL Server Browser enabled...
I made .zip of folder created during IIS publishing (c:\inetpub\wwwroot\My_App_name) and uploaded it to the some by File Manager Upload. But when I tried to access to the page with readings from database I got error
Server Error in '/' Application. 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) 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.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)
I believe that problem is with my connection string. My current connection string (web config file uploaded to the somee and on my Visual Studio 2013 Ultimate) is:
<connectionStrings>
<add name="_web_server_test.Properties.Settings.Base1ConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Database Somee web\Base1.mdf";Integrated Security=True;Connect Timeout=30"
providerName="System.Data.SqlClient" />
</connectionStrings>
Can you recognize what is the problem and how to change connection string?
Thanks.
You first have to create the database manually in your dashboard.After creating a database the page will show you the connection string you can use in your webconfig file.
make sure that you pick the right SQL version on the somee dropdown, the latest version is what worked best for me.

Can't connect to SQL Server in visual studio 2012

I am trying to connect to an SQL server database in visual studio 2012 but having no luck. I have the following code.
var db = Database.Open("anagram_database");
var shows_data = db.Query("SELECT * FROM sorted_words");
the database name is correct without any typo's, I am receiving 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)
I am completely out of my depth with this and don't even know where to start. I cant seem to find anything with googling.
From the documentation:
name
Type: System.String
The name associated with the database to open. name can specify an .sdf or .mdf database file that is in the App_Data folder. (Do not include the file-name extension.) Alternatively, name can specify the name of a connection string in the Web.config file.
Here, App_Data means the logged in user's Application Data folder (or actually, the user running the application, which is probably the same). Type %APPDATA% in the address field of a Windows Explorer and press enter. The folder that opens is where you should place the anagram_database.mdf database file.

error while Connecting .mdf database

I developed a small project in visual studio 2010. In my project I attach a service-based database named database1.mdf.
My connectionString is :
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True
It works fine on my developer pc but it throws an exception on a client's pc.
Exception is :
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)
I don't understand what is happening.
Two things need to occur before you can connect to a SQL Server Express database.
SQL Server Express must be installed on the target server. Having the file present isn't sufficient. (This is what the error you're experiencing likely means).
The path to your database file should be an absolute path to rule out the possibility of a file location error.
this connection string is for your developer machine. you share your database on the network. you also must update your connection string by adding ip address of your server.

Categories

Resources