Connecting to Sql Server2008 : General Questions

Connecting to Sql Server2008

Ask general questions here.

Connecting to Sql Server2008

Postby kumarchowg » Wed Feb 01, 2012 7:17 am

HI,

i just want to know whether we can connect to Database using Ranorex, if yes could you help us in doing it,

Thanks
Kumar
kumarchowg
 
Posts: 14
Joined: Mon Jan 23, 2012 8:53 am

Re: Connecting to Sql Server2008

Postby Stian » Wed Feb 01, 2012 9:11 am

kumarchowg wrote:HI,

i just want to know whether we can connect to Database using Ranorex, if yes could you help us in doing it,

Thanks
Kumar


You can do anything that C#/VB and the .NET framework can do - if you are able to write code for it.

Whether or not Ranorex Studio provides some means to hook up to a database without coding, I don't know.
Stian
 
Posts: 16
Joined: Mon Jan 30, 2012 10:38 am

Re: Connecting to Sql Server2008

Postby sdaly » Wed Feb 01, 2012 10:09 am

Since I'm such a lovely chap, here is a MSSQLConnector class that I use in our automation framework - if using RxStudio then maybe you could make a code module :wink:

Code: Select all
using System;
using System.Data;
using System.Data.SqlClient;

namespace AutoFramework
{
   public class MSSQLConnector
   {
      
      private string server, database, uid, password, connectionString;
      
      public MSSQLConnector(string _server, string _database, string _username, string _password)
      {
         this.server = _server;
         this.database = _database;
         this.uid = _username;
         this.password = _password;
         this.connectionString = "user id="+ this.uid +";" +
            "password="+ this.password +";" +
            @"server="+ this.server + ";" +
            "database="+ this.database + "; " +
            "connection timeout=30";
      }
      
      /// <summary>
      /// Returns a datatable containing the data the query retrieved.  Use this when you need data returned.
      /// </summary>
      /// <param name="SQL"></param>
      /// <returns></returns>
      public DataTable RunQuery(string SQL)
      {
         
         SqlConnection connection = new SqlConnection(connectionString);
         SqlDataReader reader = null;
         DataTable dt = new DataTable();
         try{
            SqlCommand command = connection.CreateCommand();
            command.CommandText = SQL;
            connection.Open();
            reader = command.ExecuteReader();
            //load the mysql reader into a databable - this allows the connection to the db to be closed while still
            //having access to the data that the query returned
            dt.Load(reader);
         }catch(Exception e){
            Console.WriteLine("Exception is MSSQLConnector::RunQuery - " + e.ToString());
         }finally{
            reader.Close();
            connection.Close();
         }
         return dt;
      }
      
      /// <summary>
      /// Returns the count of records found for a count query.  Example SELECT COUNT(*) FROM tablename.
      /// Use this when you just was a count and don't need any data returned.
      /// </summary>
      /// <param name="SQL"></param>
      /// <returns></returns>
      public int CountQuery(string SQL)
      {
         SqlConnection connection = new SqlConnection(connectionString);
         int count=0;
         try{
            SqlCommand command = connection.CreateCommand();
            command.CommandText = SQL;
            connection.Open();
            count = Convert.ToInt32(command.ExecuteScalar());
         }
         catch (Exception e){
            Console.WriteLine("Exception is MSSQLConnector::CountQuery - " + e.ToString());
         }
         finally{
            connection.Close();
         }
         return count;
      }
      
      /// <summary>
      /// Returns the number of rows affected by the supplied query.  Use this if you need to run an UPDATE or INSERT query.
      /// </summary>
      /// <param name="SQL"></param>
      /// <returns></returns>
      public int UpdateQuery(string SQL)
      {
         SqlConnection connection = new SqlConnection(connectionString);
         int rowsAffected=0;
         try{
            SqlCommand command = connection.CreateCommand();
            command.CommandText = SQL;
            connection.Open();
            rowsAffected = Convert.ToInt32(command.ExecuteNonQuery());
         }
         catch (Exception e){
            Console.WriteLine("Exception is MSSQLConnector::UpdateQuery - " + e.ToString());
         }
         finally{
            connection.Close();
         }
         return rowsAffected;
      }
   
   }
}
User avatar
sdaly
 
Posts: 213
Joined: Mon May 10, 2010 12:04 pm
Location: Dundee, Scotland


Return to General Questions

Who is online

Users browsing this forum: No registered users and 0 guests