Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

Need to convert from OleDb to SQL Express format

Hi,

Is there an easy way to reformat this code which I use with Microsoft Access to use SQL Express instead?
I do remember there being calls which are in common.

Could someone take a shot at it?

thanks,
newbiewb

            public void SynchronizeID()
            {
                  OleDbConnection conn = null;
                  OleDbCommand dbSelectCommand = null;
                  OleDbDataReader reader = null;
                  string sql = null;
                  try
                  {
                        conn = new OleDbConnection();
                        conn.ConnectionString = Handles.Preferences.DMSConnectionString;
                        conn.Open();
                        dbSelectCommand = new OleDbCommand();
                        dbSelectCommand.Connection = conn;

                        sql = "SELECT ID FROM BadgeIDs WHERE BadgeID='" + badgeID + "'";

                        dbSelectCommand.CommandText = sql;
                        reader = dbSelectCommand.ExecuteReader();
                        if ( reader.Read() )
                        {
                              this.id = reader.GetInt32(0);
                        }
                  }
                  catch (Exception ex)
                  {
                        Handles.LogException( "TLLocation.SynchronizeID()", ex, sql );
                  }
                  finally
                  {
                        if ( reader != null )
                              reader.Close();
                        if ( dbSelectCommand != null )
                              dbSelectCommand.Dispose();
                        if ( conn != null )
                        {
                              conn.Close();
                              conn.Dispose();
                        }
                  }
            }
Avatar of hongjun
hongjun
Flag of Singapore image

try this

            public void SynchronizeID()
            {
                  SqlDbConnection conn = null;
                  SqlDbCommand dbSelectCommand = null;
                  SqlDbDataReader reader = null;
                  string sql = null;
                  try
                  {
                        conn = new SqlDbConnection();
                        conn.ConnectionString = Handles.Preferences.DMSConnectionString;
                        conn.Open();
                        dbSelectCommand = new SqlDbCommand();
                        dbSelectCommand.Connection = conn;

                        sql = "SELECT ID FROM BadgeIDs WHERE BadgeID='" + badgeID + "'";

                        dbSelectCommand.CommandText = sql;
                        reader = dbSelectCommand.ExecuteReader();
                        if ( reader.Read() )
                        {
                              this.id = reader.GetInt32(0);
                        }
                  }
                  catch (Exception ex)
                  {
                        Handles.LogException( "TLLocation.SynchronizeID()", ex, sql );
                  }
                  finally
                  {
                        if ( reader != null )
                              reader.Close();
                        if ( dbSelectCommand != null )
                              dbSelectCommand.Dispose();
                        if ( conn != null )
                        {
                              conn.Close();
                              conn.Dispose();
                        }
                  }
            }
and of course you need to add

using System.Data.SqlClient;
Avatar of curiouswebster

ASKER

something's not right.

The compiler can not find anything starting with Sql.

SQLDbConnection or SqlDbConnection or any of the others.

and the help has nothing on any of them.

Are you sure System.Data.SqlClient is the only thing I need?
ASKER CERTIFIED SOLUTION
Avatar of hongjun
hongjun
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial