Accessing sql 2008 database through visual studio 2010 C# - c#

Hey guys I have what should be a simple question, but I am new to C# so I am having just a little problem. I am using the following connection:
SqlConnection conn = new SqlConnection(#"Server=myservername\SQLEXPRESS;Database=Names;");
I have also tried to include user id and password but my login always fails.
I think this is because I am not really sure what username and password to use.
With my sql server I am using windows authentication... if that helps.
does anyone know which username and password I should be using? or another way to do this?
I have seen that I may need to turn remote access on in my sql settings, can anyone tell me how to do that?
Thanks!

Your connection string should specify that integrated authentication (Windows authentication) has to be used. Otherwise it tries to do SQL user/password authentication, which is disabled on the SQL Server by default, and if enabled it doesn't authenticate against Windows users.
Server=.\SQLEXPRESS;Database=YourDatabaseName;Integrated Security=SSPI;
Remote access is only required if your application and the SQL Server Express instance aren't on the same machine (which is why the sample connection string here has a . for the server name, which represents the local server).

Related

SqlServer2012-Error 40-Could not open a connection in SQL Server

I know this is an answered question , however I have tried all the steps mentioned
like:
In SQL Configuration manager have Enabled TCP/IP,Shared Memory and Named Pipes.
Restarted the SQLSERVER
In Services.msc I have restarted SQLSERVER and SQL Server Agent
Added port 1433 in firewall's Inbound rule
Restarted PC number of times
Even Registered Local Server in SQL Server Management tools
I can open SQL Server Management tools
It was working fine and now its not. However I am able to login , then I cannot do any work on it, it gives me an error as :
Tried all the steps mentioned in the accepted answer HERE
I have some other application accessing same SQL Server and they are working fine.
My Connection string :
public SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Database=RMS_EXPRESS;Integrated Security=True;User Id=sa;Password=xxxxxx");
I also tried with the following connection string
Data Source=.;Database=RMS_TS1;Integrated Security=False;User Id=sa;Password=xxxxxxx
It gave me an error. "Login Failed for User 'sa'"
My application and Database are on the same PC
So , please help.
Thanks
You can not setup in your connection string both Integrated Security=true and User Id=sa;Password=xxxxxx
Integrated Security = true means it is using windows credentials (of the user the application is running into). A database connection cannot use both windows AND SQL Server authentication.
You should either:
Use Integrated Security=true and do not specify User Id nor password. This would use windows authentication.
Use Integrated Security=false or remove it althogether and specify both User Id and Password. This would use SQL Server user/password authentication.
Besides that, there was an adittional problem, originally you put Data Source=.\SQLEXPRESS but had to access default instance via Data Source=. which caused that error.
Allow Remote Connections enabled under Connections in SQL Server Properties.

Connect with SQL Server 2008 Express using Winforms (C#)

I am creating the database from coding in Winforms (C#) and also run the script for creating the tables.
In short I create the database using the Winforms (C#) in SQL Server Express.
But when I run my application I get this error :
Cannot open database '' requested by the login. the login failed. login failed for user 'KETAN\admin'.
My connection string:
Server=.\SQLEXPRESS;Integrated Security=True;database=mydatabasename;User Id=myusername;Password=mypassword;
Any help would be appreciated.
If you have both
Integrated Security=True;
and
User Id=myusername;Password=mypassword;
in your connection string, then the Integrated Security wins and the specific user and password are ignored, and the connection is attempted with your current Windows credentials (KETAN\admin) and that doesn't seem to work
So just change your connection string to:
Server=.\SQLEXPRESS;database=mydatabasename;User Id=myusername;Password=mypassword;
Now, your specific user and its password will be used to connect to SQL Server, and if those are correct, you should be able to connect just fine

Using Windows Authentication with Connection string in IIS to connect to SQL 2008 R2

I'm am making a model first website using MVC4 on IIS7.
Currently my connection string looks like this:
<add name="VaultEntities"
connectionString="metadata=res://*/Models.VaultDB.csdl|res://*/Models.VaultDB.ssdl|res://*/Models.VaultDB.msl;provider=System.Data.SqlClient;
provider connection string="
data source=sqlserver\instance;
initial catalog=vault;
integrated security=True;
multipleactiveresultsets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
The connection was auto generated by Visual Studio 2011 when I added the database through the Model First approach. The query works perfectly locally on my development machine. But it dosen't work on the IIS server.
My guess is that this is because my user account on my development machine has access to the SQL server and when IIS tries to access the SQL server its user account gets denied.
In a perfect world I would add a user on the SQL server and use
User Id=myUsername;Password=myPassword;
in my connection string, unfortunately I don't have that option.
Without going into details the dba's are...difficult...to work with.
So is there any way to use my AD account in the connection string to login to the SQL server... Or can I make IIS identify itself as me?
Is there any way to use my AD account in the connection string to
login to the SQL server... Or can I make IIS identify itself as me?
You can get the application pool to use any AD user you want.
I would not recommend using your own username though. How many times have you locked your username in the last 12 months, and what happens when you leave?
Create/use a service AD account, map it in the application pool, and get the DBAs to give DB access to that user, using trusted connection. Sorry, you'll have to talk to the DBAs! :-)
Ok, there is two options.
1. Either you change you application pool identity to one which has AD access to database, this can be done also by using impersonation section in web.config file of your website(Check this link).
2. Either you add sql account to your connection string. As I can see you are using EF connection string, it's a little bit different from standard sql connection string. But still has the same part with sql credentials, just replace integrated security = true to used id =...;password=.... That's all.
Hope this helps.

SQL Server Database Connection String

I'm trying to write a program that will fetch data from a SQL Server database. The program is written on top of the .NET platform using C#. Furthermore, the program will be running on a Linux host (with mono). The problem that I am faced with is connecting to the database from the Linux machine, all of my code works fine from a windows host (since the server uses windows authentication), so my question is this: how can I connect to/authenticate with the SQL Server instance?
I've looked at http://www.mono-project.com/SQLClient, and it says to use a string similar to this: "Server=MyServer;Database=pubs;User ID=MyWindowsDomain\\MyWindowsUserid;Password=MyWindowsPassword;Integrated Security=SSPI" but that is not working. Is there maybe something I can do on the server (not likely that I'll be allowed to, but as a last resort)?
Thanks.
You cannot use sql server integrated authentication with linux, you have to use sql server authentication
As A. DIMO says, you cannot authenticate using integrated security on *NIX. It's not a windows box.
In your SQL Server Management studio, create a login that uses SQL Server authentication and not Windows Authentication. Ensure it has the necessary writes on the database in question.
Next, remove the Integrated Security=SSPI part of the connection string. That should do the trick.
While not a direct answer to your question, this site is useful for almost any connection string question: http://www.connectionstrings.com/.
At the risk of sounding like a commercial: It's your one-stop-shop for connection strings!

Problem accessing sql server database 2008 r2 over network

I've just finished my c# application with sql database built in 2008 r2 version
I wanted to share this database over LAN and my connection string is :
ConnectionString = #"Data Source=192.168.0.1,1433\SQLEXPRESS;user id=Rula; password=marojo;AttachDbFilename=\\192.168.0.1\Release\WEPA.mdf;Connection Timeout=30;Integrated Security=SSPI;user instance=true";
where 192.168.0.1 is the IP of the computer hosting the sqlexpress instance
when I connect to database locally from the hosting computer I get no errors, but when I try to connect to the database from the shared application I get the following error:
User doesn't have permission to perform this action.
while I mapped the GUEST user to my database on the hosting machine since I knew that if a computer try to connect to database remotly, it connect as guest.
notice: I have my application and database shared over network in the same folder, and again locally everything is ok
Please what can I do to solve this problem ????
thanks in advance =-)
If the 2 machines are not on the same domain this will fail. You might want to use sql logins
The guest account would not be used in an trusted connection. The integrated connection uses the user account attached to the process using the connection string.
http://forums.asp.net/t/822604.aspx/1
If you wanted to use integrated security, the web server would need to be configured to digest logins and then you would need to grant access to all (potential) authenticated users access to the database. This is typically not done due to the unnecessary complexity you are adding to the process. Just use sql links like Ali stated and be done with it.
Okay, I see a couple things that are wrong in your connection string.
Namely, you have defined a user id and password... then you go on to set the Integrated Security flag. These are mutually exclusive. Either it connects as the same user that the app is executing under (most likely the app pool user account) OR it connects as the user defined in your connection string.
The reason you see a difference between 7 and XP is simply that one is defaulting to using the user id / password, the other is defaulting to using integrated security.
I suggest that you visit http://www.connectionstrings.com/ in order to build a proper connection string.
Update:
Other things I see. First, AttachDbFilename can only work with integrated security. Obviously that isn't a possibility, so you need to get rid of that parameter and configure sqlexpress directly on the server to target that database file.
Second, User Instance is for desktop deployments.. not servers. Get rid of it as well because it's going to be a huge memory hog.
Third, read this: http://msdn.microsoft.com/en-us/library/ms247257(v=VS.100).aspx
There are quite a few reasons this could be happening:
SQL Browser service not turned on
Connection string using a trusted connection and you are not set up as that user (check what user your code is using by getting User from the current context)
You do not have the same protocols set up on the client (less likely)
Since it is a permissions issue, you will most likely have to set up your windows domain account in the database to use it. If that is not an option for the application users, then you may desire to move to a sql account rather than windows accounts.
problem solved !!
there was just attaching problem when the database is attached to sql server management studio it's attached as an old name or related to the computer it's copied from
Thank you everyone for your effort : )

Categories

Resources