Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL Database connection in C#.net

What I have tried:

C#
string str;
   SqlConnection cnn;
   str = @"server=(local);database=SMS_Data;Integrated Security=SSPI";
   SqlConnection cnn = new SqlConnection(connetionString);
Posted
Updated 27-Sep-18 22:32pm
v2
Comments
Eric Lynch 28-Sep-18 8:00am    
If you've tried the above, something must have gone wrong. Otherwise, you wouldn't be here asking questions :)

Generally, in C#, if "something goes wrong" an exception is thrown and (unless you handle the exception) it is displayed somewhere. If you really want help, you'll need to share the details of that exception.

There are a bunch of different things that can "go wrong". Some examples include: you (as a user) are not authorized to access the database, you've provided an incorrect database name, SQL server is not running, the connection is blocked by a firewall or other network setting, some problem has caused a timeout before the connection is completed, or many, many other possible issues.

To help you, we need to know which of these many possible issues is the cause. The exception usually provides enough detail to (at a minimum) restrict the number of possibilities. Once we know what issues might be possible, we might be able to provide some advice about how to fix that specific issue (or issues).

Without that information, all we can do is guess, which is both a waste of our time and unlikely to help you.

Of course, this all assumes your code actually compiles. As others note, the variable "connetionString" is undefined, so either the compilation should fail or your question does not include the code you actually tried (perhaps a typo?).

As a secondary issue, OriginalGriff is correct, you shouldn't hard-code connection strings in the source code. Generally, I usually save these in either the application or web configuration file. If you also add the type of application you are writing (Web or WinForm/WPF/Console) into your question, you might get more specific information about how this is done.

Hopefully, you'll add enough information, to your question. help us to help you. Good luck.

Um. Look at your code:
string str;
SqlConnection cnn;
str = @"server=(local);database=SMS_Data;Integrated Security=SSPI";
SqlConnection cnn = new SqlConnection(connetionString);
What is in connetionString? Because the variable you put your actual string into is called str ...

You shouldn't "hard code" connection strings: it means you app has to be changed to switch between dev and production if nothing else, and that's dangerous. It also leads to loads of different places to change when you do release the product. Use a config file or similar instead. This is how I do it (but it's probably overkill for you): Instance Storage - A Simple Way to Share Configuration Data among Applications[^]
 
Share this answer
 
Comments
Ashutosh_jha 28-Sep-18 3:53am    
string str;
SqlConnection cnn;
str = @"server=(local);database=SMS_Data;Integrated Security=SSPI";
SqlConnection cnn = new SqlConnection(str);
OriginalGriff 28-Sep-18 4:06am    
Better - but seriously, don't use hard coded connection strings.
Ashutosh_jha 28-Sep-18 4:20am    
give me correct way and code
OriginalGriff 28-Sep-18 4:28am    
You are going to have to explain yourself rather more, I'm afraid: we have no idea what is wrong. Any error messages, what does it do that you didn't expect, or not do that you did - that sort of thing.
Ashutosh_jha 28-Sep-18 4:40am    
I am trying to make a simple windows application in visual studio 2005 with sql server 2008. i have no any idea how to connect with database via code.
You didn't provide enough information about your issue. Nevertheless...

First of all, i agree with OriginalGriff, because:
Quote:
Embedding connection strings in your application's code can lead to security vulnerabilities and maintenance problems. Unencrypted connection strings compiled into an application's source code can be viewed using the Ildasm.exe (IL Disassembler) tool. Moreover, if the connection string ever changes, your application must be recompiled. For these reasons, we recommend storing connection strings in an application configuration file.

See: Connection Strings and Configuration Files | Microsoft Docs[^]

For further details, please see:
Connection String Syntax | Microsoft Docs[^]
SQL Server Connection Strings for ASP.NET Web Applications | Microsoft Docs[^]
SQL Server 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