Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to connect SQL Database with C#

according to How to connect SQL Database to your C# program, beginner's tutorial[^]


there is 2 ways to connect to database :

1 is windows authentication
2 is database authentication

if i use database authentication :

should connectionString be like this ?


using(SqlConnection conn = new SqlConnection()) 
{
    conn.ConnectionString = "Server=[server_name];Database=[database_name]; password = = [password_name];user = [user_name];
    // using the code here...
}


What I have tried:

this is just a question about the syntax of the connectionstring. i would like to know if my syntax for the connectionstring is correct if i use database authentication.
Posted
Updated 31-Aug-17 21:04pm

It's not far off - though the password field appears to have too many "=" characetrs.
Here's one of mine:
This is the local access, with intgrated security.
Connection=Data Source=GRIFF-DESKTOP\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=True

And the remote access to the same database used SQL authantication
Connection=Data Source=GRIFF-DESKTOP\SQLEXPRESS;Initial Catalog=DatabaseName;User id=SQLUserName;Password=SQLUserPassword

But don't hard code them like that - always use a configuration file of some form to store them, so they can be changed if the server changes (or when you move to production).
 
Share this answer
 
Comments
forte74 31-Aug-17 6:41am    
i have read that C# does not have header files. so how do you import other files to program.cs or into main ?
OriginalGriff 31-Aug-17 7:05am    
Sorry?
You're going to have to ask that again - we only get exactly what you type to work with, and you are missing out a lot of context we don't have access to.
Foothill 1-Sep-17 11:48am    
With C#, application settings (you will find them in your project's properties) are saved to a file in your User\AppData\Local folder which is loaded when you run your program. Also, you could store your user name, password, or connection string there. You could also just save them to a text file in the same directory of the executable and load it with the System.IO.StreamReader class.
Thatone isn't too difficult, have a look at this site for some of your alternatives, the first one should serve you fine,

SQL Server Native Client 11.0 OLE DB Provider Connection Strings - ConnectionStrings.com[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900