C# windows Application attachmentFilename - c#

I have a working SQL connection here:
SqlConnection conn =
new SqlConnection(#"server=localhost\SQLEXPRESS;
AttachDbFilename=C:\Seach ENGINE (June 22, 2015)\SE\SE\MainDatabase.mdf;
Integrated Security=True;User Instance=True");
but the problem is I need to provide the complete path. Unlike asp.net, all I have to do is to add |DataDirectory| to it and include the database name ..
Then I tried to use the |DataDirectory| in this connection string and I get an error:
invalid value for key 'attachDbFilename'
I published my application and I can't locate the database now because I installed it in a different computer.. so the attachDbFilename is not true ..
If the DataDirectory is not working is there any other code that I can use similar to data directory?

AttachDbFilename is used with local database. If you have remote database then the connection string will be different . Have a look at article - How to configure SQL Server 2005 to allow remote connections.
Access mdf file
As you see here
System.Data.SqlClient resolves the substitution strings into full
paths against the local computer file system. Therefore, remote
server, HTTP, and UNC path names are not supported. An exception is
thrown when the connection is opened if the server is not located on
the local computer.
If you want to connect to an already-running SQL Server Express instance on another computer over TCP, then you need to set-up TCP connections on that server first. Note that you cannot use AttachDbFilename either because that option only applies to "user instances" of SQL Server Express.
Hope this will help you.

SqlConnection require you to specify the fully-qualified name of the
db that is being attached.
No way around that.

Related

connect mysql with c#

I'm building a c# windows application which will connect with the mysql database in a remote server.
I'm using the following connect script
string connectionString;
connectionString = "SERVER = eu5.org;UID = myuserid; PASSWORD = mypassword; DATABASE = mydatabasename;";
connection = new MySqlConnection(connectionString);
It shows the error couldn't connect to the database.
P.S: Mysql database is at eu5.org server
Personally I prefer to use the MySql Workbench IDE for testing connections and working (Querying) the database directly where possible. Most hosted databases that I have worked with normally define the Server as Instance.[DomainName] so I would have expected your server URL to be something like MySql1.eu5.org
Below is a connection string that I tested using the MySql Connector, change the parameters.
<connectionStrings>
<add name="MySqlConnection" connectionString="server=INSTANCENAME.DOMAINNAME.COM;UID=USERNAME;password=PASSWORD;database=DATABASENAME;Persist Security Info=True;" providerName="MySql.Data.MySqlClient"/>
MAke sure that your server should be started...
Just read the FAQ:
Many people want to only use database, but their site is hosted elsewhere. We provide free database for websites hosted with us. For that reason, external access is blocked without exception.
--> http://www.freewebhostingarea.com/faq.html
so it's not possible to access your MySQL-DB from external sources.
only localhost will be allowed
Your code is correct.
Use a browser for MySQL, like http://mysql-query-browser-for-windows.apponic.com/ to check if your database is available.

What kind of database connecting string is needed to run application in different computers

I am learning c# and sql database. I have a database management system developed in c#.net.
My database connection string is:
string _ConnectionString = #"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Promming Codes\OOP2 (C#)Programs\C# Database\MMS\MMS\MMSdatabase.mdf;Integrated Security=True;User Instance=True";
It works on my PC very fine, but when I try to install the setup file to my friends PC, the connection string cannot be found. How can i overcome this kind of problems?
If you are trying to access the database from your friend's computer and both of you are on the same network then you need to modify your connection string. Your current connection string is pointing to local machine, you need it to point to your machine (Which is acting like a server). Your connection string in that case should be :
string _ConnectionString = #"DataSource=yourmachineaddress\SQLEXPRESS;
AttachDbFilename=E:\Promming Codes\OOP2 (C#)Programs\C#
database\MMS\MMS\MMSdatabase.mdf; Integrated Security=True;User
Instance=True";
where your machine address could be your machine name. You may also enable remote access on your sql server. How to enable Remote Connection in SQL Server
If you and your friend are not on the same network and you need to install a separate instance of the application then you have to install SQL Express on your friend's computer. Restore the database there and create connection string according to that particular machine setting. For building connection string check Connection strings for SQL Server 2008

Use of MS SQL SERVER 2008 R2 EXPRESS with C# Windows Application?

I've created a c# windows application(2.0 framework) which uses MS SQL database.
During developing I've used MS VISUAL STUDIO 2010 and SQL 2008 MANAGEMENT STUDIO.
My connection string during development is :
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=SL;Integrated Security=True");
Everything works fine....
Now I want to run this application on client system.
So installed MS SQL SERVER 2008 EXPRESS successfully on client system.
Stopped sql services of my system and copied the .mdf and .ldf files from my machine and pasted in "c:\Database\" of the client.
But while running comes the problem.
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 connection strings which I've tried many times are :
SqlConnection con = new SqlConnection("Data Source=.\\MSSQLEXPRESS;Initial Catalog=SL;Integrated Security=True");
SqlConnection con = new SqlConnection("Data Source=.\\MSSQLEXPRESS;Initial Catalog=SL;Persist Security Info=True;User ID=sa;Password=pass");
SqlConnection con = new SqlConnection("Data Source=.\\MSSQLEXPRESS; AttachDbFilename =C:\\Database\\SL.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
When I use User ID=sa;Password=pass in connection string I get:
authentication failed for 'sa'
Am I missing some steps or doing wrong? Please tell me what should I do after copying the database from my system. What should be my connection string in C#?
Thanks !
You have only copied the files you need to attach the database into SQLExpress, take a look at using OSQL, or alternatively install the client tools onto the PC where you have SQLExpress and attach the databases.
First of all, be sure to check out Sres' answer.
If you don't tell SQL Server in the connection string to attach your database (with AttachDbFilename, like in your third example), you have to do the attaching yourself as he said.
Concerning your three connection string examples: all of them only work under certain circumstances. You might want to check out connectionstrings.com.
Here are your three connection strings, plus short explanations of their issues:
Data Source=.\MSSQLEXPRESS;Initial Catalog=SL;Integrated Security=True
--> This uses the current Windows user that you app is running under. So the current windows user must have permissions on the database on the client's machine.
Data Source=.\MSSQLEXPRESS;Initial Catalog=SL;Persist Security Info=True;User ID=sa;Password=pass
--> This uses the special 'sa' user account. In order for this to work, you have to make sure that the following prerequisites are met:
Mixed mode authentication must be set up (if you don't do this, you only have Windows authentification, and 'sa' is a SQL Server authentification user name)
the password of 'sa' needs to be specified, and of course it must be the same as on the development machine.
But this is not the best solution anyway. 'sa' is an admin account with full permissions, and you shouldn't use an admin account to access SQL Server with your app.
If you really want to use SQL Server authentification (instead of Windows authentification), it's better to create a new account with the minimal necessary permissions that your app needs.
Data Source=.\MSSQLEXPRESS; AttachDbFilename =C:\Database\SL.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
--> same issue as with the first connection string (the current Windows user must have permissions).
Plus, User Instance=True needs to be enabled in SQL Server.
Quote from connectionstrings.com:
To use the User Instance functionality you need to enable it on the
SQL Server. This is done by executing the following command:
sp_configure 'user instances enabled', '1'. To disable the
functionality execute sp_configure 'user instances enabled', '0'.
Are you sure the new instance name is 'MSSQLEXPRESS'?
My R2 Express instance is called 'SQLEXPRESS' (this is actually due to a known bug in the installer).
You might want to check that the instance is actually called what you think it is.

SqlConnection problem

My sqlconnection is
SqlConnection(#"Data Source = John\Administrator; Initial Catalog = TicketingSystem; Integrated Security = true;");
I try to connect to the server but i cant this error pops up
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 think that the error is in the Data Source but I cant find it. When I open Microsoft SQL Server Management Studio it says that my server is "John" and the Connection is "John\Administrator" so please help me.
if the server is actually called 'John', then that is your data source. When you're running locally, you could probably just set Data Source=(local), tho.
Other than that, you'd need to specify a user to connect with
http://connectionstrings.com/
An easy way to get your connection string is
create a file called x.udl
double click on it
Follow wizard
open x.udl file in notepad
and inside you will find your connection string.
When I open Microsoft SQL Server
Management Studio it says that my
server is "John" and the Connection is
"John\Administrator" so please help
me.
That means you're logged on to your server's default instance (John) as Administrator. Remove the \Administrator part, and you should be good to go!
var cn = new SqlConnection(#"Data Source = John; Initial Catalog = TicketingSystem; Integrated Security = true;");
You can get your connectionstring from SQL Server Management Studio from the properties window.
Just click on the John\Administrator node, then press F4 to open the properties window, and search for the connection string there...
The Integrated Security=True part of your connection string means "connect as the current user". When you run an asp.net page, the current user by default is a special ASPNET account. You can "impersonate" a different user, but I'm guessing you haven't done that and the ASPNET user doesn't have access to your database.
It's possible that your server isn't configured to allow remote connections. You can check this in the SQL Server Configuration Manager tool.
Have a look here too.
Your Data Source is the server. So you only need "John", not "John\Administrator", that's not a valid data source. You're using integrated security which means it will use the same username as the program is running under and it's credentials, if it's on the domain.
Data Source = John\Administrator
Specifying John\Administrator as your Data Source, means ADO should connect to the SQL instance named "Administrator" on the server "John". Although instances are quite useful, I don't think that's what you want. (SQL Instances allow you to run multiple copies of SQL Server on one server)
As "d." mentioned, you will need to change Data Source to the correct server name or (local) ((local)\SQLEXPRESS if it's SQL Express).
Also, Integrated Security=true means that the user running ASP.NET, or at least the AppPool, will need access to the database. (The default is NETWORK SERVICE on pre-Windows 7, IUSR / IIS_USRS on 7).
If, however, you use ASP.NET windows authentication with <identity impersonate="true" /> then the user accessing the site will need access to the database.
Create a file called x.udl
Double-click on it
Follow the Wizard
Open x.udl file in notepad
and then you will find your connection string inside.

SQL Server connection issue

I am using VSTS 2008 + .Net 3.5 + C# + SQL Server 2008 Enterprise on Windows Server 2003. I am using the following connection string, and labtest1 is the local machine name and I connect from local machine using ADO.Net. Then it always fail with connection error. But when I change in the connection string from "labtest1" to ".", connection has no issue with the same ADO.Net client code. Any ideas what is wrong?
Data Source=labtest1;Initial Catalog=CustomerDB;Trusted_Connection=true;Asynchronous Processing=true
Here is the detailed error message I got,
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Looks like a Sql Server configuration issue to me : have you tried to tune protocols in Sql Server Network configuration ? Named Pipes or TCP/IP should be enabled.
When you use "." or "(local)" it connects to the default instance on your PC, perhaps the SQL Server was installed with instances, in which case you have to specify the instance name in the connection string in the format "SERVER\INSTANCE_NAME".
In SQL Management studio execute this query to see your full server\instance name
select ##servername
I have seen this issue previously with ZoneAlarms blocking the connection (on the machine trying to connect to the SQL server). I would spend some time investigating this area around firewalls etc.
Hope this helps
Do you have the Named Pipes network protocol enabled in the network config? (In the SQL Server Configuration Manager - sql server 2005, that's what I have, might be different in 2008 - you should be able to verify this setting)
Try to ping by the computer name: ping labtest1. if it does not find the server, then try ping labtest1.mydomainname.com with the domain name. If this works, then you just need to add/fix the DSN aliases in the domain controller or just re-login to the machine.
One other thins - and may not be relevant - but you've not specified the security model, at least not in the string provided as a sample.
I would expect to see: Integrated Security=True (given that its the local machine) in the connection string.
The other thing that may be relevant is - as has already been mentioned - the protocols, I'd look to make sure that TCP/IP is enabled.

Categories