connect to a sql server Db with c#

Am using the below code to connect to the sqlsever db have not set any passwords or usernames in the dB and am not sure what is supposed to be set or not set to access the db via the C# code any help woudl be appreciated am using visual studio 2005 and sqlserver 2005 when you run the code connection1.Open();
 it just comes back cannot connect
  private void connectsqlserver_Click(object sender, EventArgs e)
        {

            SqlConnection connection1 = new SqlConnection("user id='';" +
                                       "password='';server=serverurl;" +
                                       "Trusted_Connection=yes;" +
                                       "database=Extract; " +
                                       "connection timeout=30");
           
            connection1.Open();
            SqlCommand insertCommand = new SqlCommand(
                "INSERT INTO tblMainRecInfo (journaltitle) VALUES ('datalkjlkjk')",connection1);
            insertCommand.ExecuteNonQuery();
            connection1.Close();
           
           }
sydneyguyAsked:
Who is Participating?
 
hongjunConnect With a Mentor Commented:
try this

 
SqlConnection connection1 = new SqlConnection(@"Data Source=DICKSMITHMIR-PC\SQLEXPRESS;Initial Catalog=PubMedExtract;Integrated Security=SSPI;");

Open in new window

0
 
Pratima PharandeConnect With a Mentor Commented:
connection string like this

  <add name="MyDbConn1"
       connectionString="Server=MyServer;Database=MyDb;Trusted_Connection=Yes;"/>


refr this
http://msdn.microsoft.com/en-us/library/ff647396.aspx
0
 
sydneyguyAuthor Commented:
is this for c# ?? connection
0
Increase Security & Decrease Risk with NSPM Tools

Analyst firm, Enterprise Management Associates (EMA) reveals significant benefits to enterprises when using Network Security Policy Management (NSPM) solutions, while organizations without, experienced issues including non standard security policies and failed cloud migrations

 
Pratima PharandeCommented:
its in web.config of C# project get more details for that link

in code you can use like this

 SqlConnection connection1 = new SqlConnection("Server=MyServer;Database=MyDb;Trusted_Connection=Yes;");
0
 
hongjunCommented:
Try this

SqlConnection connection1 = new SqlConnection("Data Source=serverurl;Initial Catalog=Extract;Integrated Security=SSPI;");

Open in new window


Please replace the serverurl with the server name.
0
 
sydneyguyAuthor Commented:
SqlConnection connection1 = new SqlConnection("Data Source=DICKSMITHMIR-PC\SQLEXPRESS;Initial Catalog=PubMedExtract;Integrated Security=SSPI;");

this is the connection string but it does not like the DICKSMITHMIR-PC\SQLEXPRESS expecially the \ mark in the equation
0
 
sydneyguyAuthor Commented:
that got the local going but will need to lift this up to the sever eventually so thanks for the page link
garry
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.