How To connect to SQL database in windows form applications


  • Hi

    How To connect to SQL database in windows form applications?
    I used "System::Data::SqlClient::SqlConnection ", but I did not know What is ConnectionString?
    I use VC++ 2008 with MSSQL 2005.

    Tuesday, December 08, 2009 10:55 PM


All replies

  • Hi

    I'm not VC++ developer^^ but I can show you the code as C#
    I'm sure you'll found out how to convert to C++, right? ^^

    Please refer following my code:

                string cnnstringforWindowsAuthenticate = "server=localhost; database=adventureworks;integrated security=true;";
                string cnnstringforSQLAuthenticate = "server=localhost; database=adventureworks;user id=SQLUser;Password=P@ssw0rd;";

                using (SqlConnection cnn = new SqlConnection(cnnstringforWindowsAuth))

                    SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM Sales.SalesOrderHeader WHERE SalesOrderID <= 45659", cnn);

                    using (SqlDataReader rdr = cmd.ExecuteReader())
                        while (rdr.Read())

    I hope that helps

    Best Regards,
    Jungsun Kim
    Wednesday, December 09, 2009 4:18 AM
  • Hi Mahdi,

    you can get sample code from here
    Thanks Mike --------Please mark as answer if it is useful----------
    Wednesday, December 09, 2009 5:57 AM
  • Hello,

    you can look up the several connections string / parameters at:

    e.g. for Sql Server 2005:

    Olaf Helper ----------- * cogito ergo sum * errare humanum est * quote erat demonstrandum * Wenn ich denke, ist das ein Fehler und das beweise ich täglich
    Wednesday, December 09, 2009 6:18 AM