views:

62

answers:

2

Hi guys,

I have been programming in vb6 for few time ago and i used open SQL Server connection and command objects to make database traansactions. I have been searching for similar approaches in vb.net too but not finding any starting point.

How can we work similarly in vb.net application?

+1  A: 

I think you're looking for SqlConnection and SqlCommand.
The MSDN page for SqlCommand shows a sample for how they can be used:

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx

ho1
is it better than sql data adaptar
KoolKabin
No, just different parts of the same process, if you look at the sample at the MSDN doc for `SqlDataAdapter` you'll see that they use a `SqlCommand` and `SqlConnection` to connect etc. If you want to bind your controls to datasets you probably want to do it that way, if you want to retrieve data "manually" from the database you might want to look at `SqlDataReader` instead. But all these are part of the `System.DataSqlClient` namesapce
ho1
+1  A: 

I would recommend using the SqlDataReader when possible for retrieving data. It is a faster option, and it sounds like Microsoft is not investing in the future of DataSets.

using (SqlConnection conn = new SqlConnection(connString))
                    {

                        conn.Open();

                        if (conn.State == ConnectionState.Open)
                        {


                            string sql =   "Select FirstName, LastName from Customers";
                            SqlCommand cmd = new SqlCommand(sql, conn);

                            SqlDataReader reader = cmd.ExecuteReader();

                            if (reader != null)
                            {


                                while (reader.Read())
                                {

                                    Customer cust = new Customer();
                                    cust.FirstName = reader["FirstName"].ToString();
                                    cust.LastName= reader["LastName"].ToString();
                                    collection.Add(cust);

                                }

                                reader.Close();

                            }

                            conn.Close();

                        }
dretzlaff17
Can I save the Conn Connection to a global variable? how can i use it globally
KoolKabin
You could use the connection object globally by creating it as static public property inside a class in your application.
dretzlaff17