connect to a sql server Db with c#

Posted on 2011-05-02
Last Modified: 2012-05-11
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");
            SqlCommand insertCommand = new SqlCommand(
                "INSERT INTO tblMainRecInfo (journaltitle) VALUES ('datalkjlkjk')",connection1);
Question by:sydneyguy
    LVL 39

    Assisted Solution

    by:Pratima Pharande
    connection string like this

      <add name="MyDbConn1"

    refr this

    Author Comment

    is this for c# ?? connection
    LVL 39

    Expert Comment

    by:Pratima Pharande
    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;");
    LVL 33

    Expert Comment

    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.

    Author Comment

    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
    LVL 33

    Accepted Solution

    try this

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

    Open in new window


    Author Closing Comment

    that got the local going but will need to lift this up to the sever eventually so thanks for the page link

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This story has been written with permission from the scammed victim, a valued client of mine – identity protected by request.
    If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now