sql server 2005 connection string to .net application

Allgemeine Diskussion sql server 2005 connection string to .net application

  • Montag, 28. November 2011 09:12
     
      Enthält Code

    hi

    i am using sqlserver 2005 express edition and visual studio 2010 .i am not able to  connect  sql server database to the .net application.Can anyone plz tell me how to do it.i wrote the below code.but its not working.the problem is with establishing the connection.pls let me know the correct connection string..

     

     

        protected void Button1_Click(object sender, EventArgs e)

        {

    SqlConnection con=new SqlConnection("server=JKC-PC/SQLEXPRESS;database=exam;Trusted_Connection=True;");

            if (con.State == ConnectionState.Closed)

            {

                con.Open();

            }

            SqlCommand cmd = new SqlCommand("select admin_id from scheduleforexam_app_devp_tb");

            Label2.Text = cmd.ExecuteScalar().ToString();

     

    }


    • Bearbeitet sql learner12 Montag, 28. November 2011 09:20
    • Verschoben Bob BeaucheminMVP Montag, 28. November 2011 16:51 Moved to the appropriate forum for best response (From:.NET Framework inside SQL Server)
    • Typ geändert sql learner12 Donnerstag, 9. Februar 2012 04:09
    •  

Alle Antworten

  • Montag, 28. November 2011 09:22
     
     

    boss try sending the connection obj (con) to sqlcommandobj as

     SqlCommand cmd = new SqlCommand("select admin_id from scheduleforexam_app_devp_tb",con);

    if u get any exception do post it
    • Bearbeitet Prahalnathan Montag, 28. November 2011 09:22
    •  
  • Montag, 28. November 2011 09:32
     
      Enthält Code

    hi 

    thank you.. but i cud see its not getting connected to sql server 

            SqlConnection con = new SqlConnection("server=JAIPALMUSKU-PC/SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=schedularforexam");

     

            if (con.State == ConnectionState.Closed)

            {

                con.Open();

            }

            SqlCommand cmd = new SqlCommand("select admin_id from scheduleforexam_app_devp_tb",con);

            Label2.Text = cmd.ExecuteScalar().ToString();

     

    error:

    Line 26:         if (con.State == ConnectionState.Closed)
    Line 27:         {
    Line 28:             con.Open();
    Line 29:         }
    Line 30:         SqlCommand cmd = new SqlCommand("select admin_id from scheduleforexam_app_devp_tb",con);

     



  • Montag, 28. November 2011 09:35
     
     

    in the connction name  DB name is missing..

    it should be something like this

     

    SqlConnection connectionString = new SqlConnection();

    connectionString.ConnectionString = "Data Source =server; Database=DB1; Integrated Security=SSPI;Pooling=no";

     

    post about how it goes and also the exception message
    • Bearbeitet Prahalnathan Montag, 28. November 2011 09:36
    •  
  • Montag, 28. November 2011 09:45
     
     

    same error again..unable to open connection

     

       protected void Button1_Click(object sender, EventArgs e)

        {

            SqlConnection connectionString = new SqlConnection();

            connectionString.ConnectionString = "Data Source =JAIPALMUSKU-PC/SQLEXPRESS; Database=schedularforexam; Integrated Security=SSPI;Pooling=no";

            SqlCommand cmd = new SqlCommand("select admin_id from scheduleforexam_app_devp_tb",connectionString);

            if (connectionString.State == ConnectionState.Closed)

            {

                connectionString.Open();

            }

     

            Label2.Text = cmd.ExecuteScalar().ToString();

     

        }

    }

     



  • Montag, 28. November 2011 09:53
     
     
    is the server name and DB correct?. 
    • Bearbeitet Prahalnathan Montag, 28. November 2011 09:54
    •  
  • Montag, 28. November 2011 09:58
     
     
    yes i am able to connect to the database and retrieve the data  by using the smart tag above the dropbox..i cud retrieve all the feilds from the database to the form..but not able connect it by writing the code.
  • Montag, 28. November 2011 12:13
     
     
  • Mittwoch, 30. November 2011 02:03
    Moderator
     
     

    Hi sql learner12,

    The connection can’t be opened caused by the incorrect Connection String. You can use Server Explorer to get the correct Connection String. Just do like this:

    1.       Find out the database which you want to connect in the Server Explorer;

    2.       Double click the database, then you will find the red cross attached to the database icon has disappeared, it means you have already connected to the database;

    3.       Look at Properties Window, there’s a property named Connection String, please copy the value of the property and paste it to your code.

    Best Regards,


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
  • Donnerstag, 9. Februar 2012 03:37
     
     
    thank you Allen..

    akhila