none
How To connect to SQL database in windows form applications

    Question

  • 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

Answers

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);

                    cnn.Open();
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            listBox1.Items.Add(rdr.GetInt32(0));
                        }
                    }
                }
    ------------------------------------------------------------------------------------------------------------------------------------------

    I hope that helps
    Thanks


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

    you can get sample code from here http://functionx.com/vcnet/databases/procdataentry.htm
    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:
    http://www.connectionstrings.com/

    e.g. for Sql Server 2005:
    http://www.connectionstrings.com/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 http://olafhelper.over-blog.de
    Wednesday, December 09, 2009 6:18 AM