SQL Server 2005 SQL Authentication Connection String - c#

I'm building an application that connects to SQL Server 2005. It currently uses Windows authentication, but I'd like to switch to SQL Authentication (I believe it is also sometimes called Mixed Authentication). My current connection string is:
"Data Source=LOCALHOST;Initial Catalog={0};Integrated Security=SSPI"
That's for Windows authentication, but for SQL, I am thinking:
"Data Source=LOCALHOST;Initial Catalog={0};user id={1};password={2}"
Is this the correct way? The code assumes that:
{0} is the name of the database
{1} is the username
{2} is the password
I'm switching to SQL authentication because I'm thinking of connecting to a SQL Server instance on a remote server - is SQL authentication the right way to do this, and would I just have to enter the IP where "LOCALHOST" is currently?
Thanks!
UPDATE: Thank you for all the great answers, guys! All of them were wonderful and very helpful, I can't even decide which one to award "accepted answer" to, but I have voted up all of them because they rock. Thanks again!

You go in the right way, but I think that looking at Connection Strings may be much more helpfull to you than any answer in here.

You can also use uid instead of "User Id" and pwd instead of "password":
"Data Source=LOCALHOST;Initial Catalog={0};uid={1};pwd={2}"
In place of LOCALHOST, you would either use the IP of the remote machine, or the DNS Name. Note that if multiple instances of SQL Server exist on the remote machine, you need to specify the instance under Data Source - e.g. "Data Source=11.22.33.44\SQLEXPRESS".

There is an app for that: SqlConnectionStringBuilder:
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
scsb.DataSource = "LOCALHOST";
scsb.InitialCatalog = ...;
scsb.IntegratedSecurity = false;
scsb.UserID = ...;
scsb.Password = ...;
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
scsb.DataSource = "LOCALHOST";
scsb.InitialCatalog = ...;
scsb.IntegratedSecurity = true;
You can then extract the connection string from the builder's ConnectionString property. This way is error proof and you can later modify other properties like ConnectTimeout or AsynchronousProcessing, and you won't have to remember the string syntax.

Yes this will work exactly as you said.
"Data Source=11.22.33.44;Initial Catalog={0};user id={1};password={2}"

If you do not have a common Active Directory domain between the local and remote server, then I think you will need the SQL authentication. However, if you do have a common ADS domain, then I recommend using it – otherwise you have to either use a common SQL account for everyone (and then use an appropriate mechanism to encrypt the password) or create separate SQL accounts for each user, thereby duplicating data.
Be very careful with that Initial Catalog setting. If that value can be supplied by user input, then it might be used to try to attack another database, if you don't have good validations in place to protect against it. Sorry if I'm preaching to the choir :-).

Related

What is the format of a SQLConnection connection string?

What is the format of a SqlConnection connection string that is passed in the constructor method? I have run a search engine search online and all I could find so far is examples like:
"Data Source=(local);Initial Catalog=AdventureWorks; Integrated Security=SSPI;"
"User Id=sa;Server=localhost;Initial Catalog=Test;"
The examples raises questions. Since the SQL Server Management Studio (SSMS) program offers a different set of fields during start up in order to connect to a database, I have to ask how does "Server type, "Server name", "Authentication", "User name" and "Password". Also, is "Catalog" another name for a database table?
You should be more specific about what your goal is. This will provide you with better answers.
Catalog is a different name for database, you're connecting to a SQL server and use catalog to specify the database which you want to access.
Server type is either SQL or Windows Authentication
If you're trying to generate a ConnectionString in a string format but don't know how to format the string. The best way is to use the SqlConnectionStringBuilder . After you've set all the variables in the builder use the toString() method to convert it to a string. That way you don't have to worry about how to format your connectionstring.
If you have the string already, or don't need to generate it on the fly you can put it in your web/app.config and use it directly.
A very basic connectionstring that uses SQL authentication looks like this:
"data source=[sqlserver];initial catalog=[database];user id=[username];password=[password];"
I always look at this website for connection string patterns and examples:
https://www.connectionstrings.com/sql-server/

Connect c# to a SQL Server in different domain

I have an c# app that tries to connect to a SQL Server that is in the same network but out of domain. I'm trying to use SqlConnection (I would prefer not use ODBC or ole db).
My code is the follow:
con.ConnectionString =
"Server=PCX\\SQL;"+
"Initial Catalog=BBDD_SinGuid;"+
"User id=\\\\PCX\\user;"+
"Password=passwordofuser;";
And I'm sure that the user and the password are correct and are allow to connect to the SQL Server. The error that throws is a fail in the login with the user \PCX\user.
I'm missing something?
you need to add trusted connectioon here..
Provider=SQLNCLI10;Server=myServerName\theInstanceName;Database=myDataBase;
Trusted_Connection=yes;
for more information you check below link it's has lot's of suggestion may be you will get your answer.
http://www.connectionstrings.com/sql-server-2008/

How to connect to SQL server via TCP/IP which is server site using C#?

Everyone.
I've got some questions here..
I'm using C# and trying to connect the other computer which is server site.
The server site has been built a large database and it can be accessed by SQL server manager 2012.
All I'm trying is to read the data in that database then convert to the other file.
The other project test in my computer works(self-connection), but
Here's my sqlconnection string:
string sqlstring = #"Data Source=XXX.XXX.XXX.XXX\SQLEXPRESS,1433;Network Library=DBMSSOCN;Initial Catalog=Database; Integrated Security=False; Connect Timeout=30;User ID=sa;Password=XXXXXXX";
Do I need to add "AttachDbFilename" into there?
or even database path?
AttachDbFilename=""C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Databasename.mdf"
Because the results are "sa log in failed" or "it couldn't find the database file"..
Is there any good advice? thank you guys!
If you don't really know what arguments the connection string takes you may want to look into the SqlConnectionStringBuilder.
Here is a short code snippet:
SqlConnectionStringBuilder connString = new SqlConnectionStringBuilder();
connString.UserID="sa";
connString.Password = "XXXX";
connString.DataSource = "XXXX"; //if you need to specify a port write
// the portnumber with a ',' behind the address
connString.InitialCatalouge = "MyDatabaseName";
connString.IntegratedSecurity = false; //set this one true if you want to use windows
//authentification instead of sql authentification
string myconnectionstring = connString.ConnectionString;
It is a bad idea to use the super user, consider making a user that has only the privileges you need for your application to minimize security risks.

SQL connection string on domain C#

I moved a webapp from my personal machine to my work machine, along with the database I've been using to test. When clicking the submit button that inserts the data, I get the error, can't find server/instance. I'm thinking that being on a domain here is part of the problem. Here is my connection string, and I've tried several forms of it already.
Data Source=\\bkirkland_lpt.domain.com\brandon;Initial Catalog=STATUS;integrated security=SSPI;persist security info=False;Trusted_Connection=Yes;");
I see a couple problems here.
First, it looks like you are using the wrong data source. The data source should be in the format [ServerName]\[InstanceName]. Based on your code, I would suggest
Data Source=bkirkland_lpt.domain.com\brandon;Initial Catalog=STATUS;integrated security=SSPI;persist security info=False;Trusted_Connection=Yes;
Additionally, I notice you are using Integrated Security. This means that the user you are running under must be granted permissions on the database. My guess is that your domain user does not have access to the database that you moved.
\bkirkland_lpt.servicesource.com\brandon is a shared folder path, not a host name. Try bkirkland_lpt.servicesource.com
Try to enclose the server name in []. Try to replace the servername with localhost and see if it makes any difference (if it is all on your local machine). I also would use proper casing and True vs Yes as parameter value.
Data Source=[bkirkland_lpt.domain.com]\brandon;Initial Catalog=STATUS;Integrated Security=SSPI;Persist Security Info=False;Trusted_Connection=True;
Data Source=localhost\brandon;Initial Catalog=STATUS;Integrated Security=SSPI;Persist Security Info=False;Trusted_Connection=True;

Sharing SQL Server Between Multiple System

I have three computer in a office and I have installed my C#-2005 Project on all three
computers. But Problem is that Boss wants Sql-server-2000 on One PC out of three and other
would share the same.
I don’t know how to share Sql-server-2000 between three PC?. How to do?.
Confusion:-
Thanks for your co-operation but here I have a confusion on majority people said to check
TCP/IP address and consider the Connection string as per main server from client PC.
Suppose I have financial project and there would be thousand of connection string in a
project. As per above I have to change thousand of connection string as per main pc.
Now think it is a one customer's need If I have ten cutomer having same offer than think How much time I have to waste on it?. I have to modify thousand of connection string ten time more?.
If it is true than it will take lots of time on installation to each customer.
I don’t know if it is only way?.
The Connection string I have utilized on my each winform is as below:
string connstr = "server=.;initial catalog=maa;uid=mah;pwd=mah";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
Here suggested about Config File and same I don't know if some body give me idea about how to consider it with my C#2005 project than it will save my lots time.
When you connect to the database in your code, you'll a database connection string of some sort somewhere in there. Figure out the connection string for the Database server and set your code to point to that database server's connection info; I'd bet you currently you have it pointed at localhost
If you're using SQL Server you may need to enable remote connections on the database server.
added: you may need to modify firewall settings as well to allow SQL Server traffic (thanks Jared)
.
Edit: For putting the configuration string into a central location.
Your posted code
string connstr = "server=.;initial catalog=maa;uid=mah;pwd=mah";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
Change to
Assuming your application has a App.Config file then you'd add an entry in there like
<add key="DBConnectionString" value="server=.;initial catalog=maa;uid=mah;pwd=mah"/>
And change your C# code to be like
string connstr = ConfigurationManager.AppSettings["DBConnectionString"];
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
Putting the ConfigManager call into a class might be a good idea if you have a lot of settings to retrieve and/or believe the configuration storage methodology might change down the road. Going with the above example is WAY better than having the string literal scattered throughout your code.
Enable theTCP/IP connection in SQL Server. So that you can connect remotely from any pc within the network
check here
If your problem is that you embedded your connection string in the code, then you are going to have to do some refactoring. These would be the general steps, you will have to tailor them a bit to your situation.
Add your connection string to the app.config file.
Create a static/shared method that will read the connection string from the
config file.
Do a find and replace in your solution to replace all
of the hard coded connection strings in your code with the (class
and) name of the method that gets the connection string.
This is probably going to be easier than rewriting all of your data calls to use something like enterprise library or EF.
You will need to do as the others suggested, such as changing the connection string and enabling TCP/IP within SQL Server, but you will also likely need to configure your firewall to allow requests to SQL Server (default port of 1433) through.

Categories

Resources