SQL Connection C#

WarAngelos
WarAngelos used Ask the Experts™
on
Hi ya... am a bit confused with SQL Server and its connection with C#

I search online and got this code....
SqlConnection myConnection = new SqlConnection("user id=username;" +
                                       "password=password;server=serverurl;" +
                                       "Trusted_Connection=yes;" +
                                       "database=database; " +
                                       "connection timeout=30");

But I have no idea what do I put in there am guessing User id = My Username as in the windows authentication one as thats what I chose when installing SQL Server 2008 Enterprise.

so here is what i think so far
User Id = WindowsUsername(Rudu)
Password = WindowsPassword(Secret) :P
Server = ????????????????????????????? NO IDEA LOL
right ill give you my configuration for SQL SERVER... RUDU-LAPTOP (Rudu-Laptop\Rudu) and the database I created is called TestingSQLc... in that there is a table called testA
Trusted_Connection=yes
database=?????????????????????????? Err huh? LOL
connection timeout=30

Right mm if i were to guess what to put in it i would put
SqlConnection myConnection = new SqlConnection("user id=Rudu;" +
                                       "password=Secret;"+
Hi ya... am a bit confused with SQL Server and its connection with C#

I search online and got this code....
SqlConnection myConnection = new SqlConnection("user id=username;" +
                                       "password=password;server=serverurl;" +
                                       "Trusted_Connection=yes;" +
                                       "database=database; " +
                                       "connection timeout=30");

But I have no idea what do I put in there am guessing User id = My Username as in the windows authentication one as thats what I chose when installing SQL Server 2008 Enterprise.

so here is what i think so far
User Id = WindowsUsername(Rudu)
Password = WindowsPassword(Secret) :P
Server = ????????????????????????????? NO IDEA LOL
right ill give you my configuration for SQL SERVER... RUDU-LAPTOP (Rudu-Laptop\Rudu) and the database I created is called TestingSQLc... in that there is a table called testA
Trusted_Connection=yes
database=?????????????????????????? Err huh? LOL
connection timeout=30

Right mm if i were to guess here... shud it look like this?? or possibly server should be my IP Address? in my case it would be LOCALHOST

SqlConnection myConnection = new SqlConnection("user id=Rudu;" +
                                       "server=RUDU-LAPTOP (Rudu-Laptop\Rudu);" +
                                       "Trusted_Connection=yes;" +
                                       "database=TestingSQLc; " +
                                       "connection timeout=30");

but yeah please tell me how its supposed to be, and also tell me if it is LOCALHOST for SERVER what do I do if the server is on Another Computer on Another SITE just write there ip e.g server=141.84.25.25.. Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Reza RadConsultant, Trainer

Commented:
1-do you get error when connecting through application?
2- do you connect from local or remote machine?
3- can you connect to sql server via ssms with sql server authentication?
Top Expert 2013

Commented:
e.g. connectionString when using default Windows Authentication
connectionString="Data Source=Rudu-Laptop\Rudu;
Initial Catalog=TestingSQLc;
Integrated Security=True"

If you want to use Sql Server Authentication then you have to create a new user e.b. myuser
Then you can use that account in userid and password.
connectionString="Data Source=Rudu-Laptop\Rudu;
Initial Catalog=TestingSQLc;
userid=myuser;password=myuserpassword"

The difference is with 1st ie.Windows Integrated security, the identity passed by IIS will be use to connect to Sql server and
in second case all the time same userid/password identity will be used to connect to the Sqlserver.
Check this:http://www.connectionstrings.com/sql-server-2008

Author

Commented:
right so it should be like this??

                 SqlConnection myConnection = new SqlConnection("Data Source=Rudu-Laptop\Rudu;Initial Catalog=TestingSQLc;Integrated Security=True;");

mmm right so err.. if that's how it should be I have one more question...
am confused about how this should be
SqlConnection myConnection = new SqlConnection("Data Source=Rudu-Laptop\Rudu;Initial Catalog=TestingSQLc;Integrated Security=True;");
                try
                {
                    myConnection.Open();
                    SqlCommand myCommand = new SqlCommand("SELECT * FROM TestTable " +
                                                         "WHERE PASSWORD='"+ passwordTb.Text +"'", myConnection);
                    myCommand.Connection = myConnection;

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }

Open in new window

Top Expert 2013
Commented:
did not get what you confused about?
You mean connectionstring or how to execute command?
Here is basic tutorial how to use SqlConnection and SqlCommand objects:
  http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson03.aspx
You don't need the following statement as you are already passing myConnection while creating myCommand object in the constructor.
               myCommand.Connection = myConnection;  //not needed

Author

Commented:
Thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial