CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2007
    Posts
    144

    Visual Basic to C# Script

    Can anyone help me convert the following VB script to C#

    Code:
    Public Sub Main()
        Dim conn As New SqlConnection("server=(local);Integrated Security=SSPI;database=Test")
        
         AddHandler conn.InfoMessage, New SqlInfoMessageEventHandler(AddressOf OnInfoMessage)
         
         conn.Open()
         
         Dim cmd As New SqlCommand()
         cmd.Connection = conn
         cmd.CommandType = CommandType.StoredProcedure
         cmd.CommandText = "SPWithPrint"
         cmd.ExecuteNonQuery()
         conn.Close()
         Dts.TaskResult.Results.Success
         
    End Sub
    
    Private Sub OnInfoMessage(ByVal sender As Object, ByVal args As System.Data.SqlClient.SqlInfoMessageEventArgs)
        Dim sqlEvent As System.Data.SqlClient.SqlError
         For Each sqlEvent In args.Errors
                Dts.Events.FireInformation(sqlEvent.Number, sqlEvent.Procedure, sqlEvent.Message, "", 0, False)
            Next
    End Sub
    Last edited by versacestl; April 30th, 2010 at 10:00 AM.

  2. #2
    Join Date
    Mar 2007
    Posts
    144

    Re: Visual Basic to C# Script

    This is what i have so far

    Code:
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using Microsoft.SqlServer.Dts.Runtime;
    using System.Windows.Forms;
    
    namespace ST_0cfea7721c684e35b7b1a384d3ade7da.csproj
    {
        [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
        public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
        {
    
            #region VSTA generated code
            enum ScriptResults
            {
                Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
                Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
            };
            #endregion
    
            public void Main()
            {
    
                string DBConn = System.Configuration.ConfigurationSettings.AppSettings["DBConn"];
                SqlConnection cn = new SqlConnection(DBConn);
                cn.Open();
    
                string strSQL = "Schema.StoredProcedure";
    
                SqlCommand cmd = new SqlCommand(strSQL, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = cn;
    
                cn.Close();
    
                Dts.TaskResult = (int)ScriptResults.Success;
            }
    
            private void OnInfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
            {
                
                 foreach(sqlEvent s in e.Errors)
                {
                    
                }
    
            }
        }
    }

  3. #3
    Join Date
    Oct 2004
    Posts
    206

    Re: Visual Basic to C# Script

    how about

    Code:
    public void Main()
    {
        SqlConnection conn = new SqlConnection("server=(local);Integrated Security=SSPI;database=Test");
        conn.InfoMessage += new SqlInfoMessageEventHandler(OnInfoMessage);
        conn.Open();
    
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "SPWithPrint";
        cmd.ExecuteNonQuery();
        conn.Close();
        Dts.TaskResult.Results.Success;
    }
    
    private void OnInfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs args)
    {
        foreach (System.Data.SqlClient.SqlError sqlEvent in args.Errors)
        {
            Dts.Events.FireInformation(sqlEvent.Number, sqlEvent.Procedure, sqlEvent.Message, "", 0, false);
        }
    }

  4. #4
    Join Date
    Aug 2008
    Posts
    902

    Re: Visual Basic to C# Script

    see if this works:

    Code:
    public void Main()
    {
        SqlConnection conn = new SqlConnection("server=(local);Integrated Security=SSPI;database=Test");
        
        conn.InfoMessage += new SqlInfoMessageEventHandler(OnInfoMessage);
        
        conn.Open();
        
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "SPWithPrint";
        cmd.ExecuteNonQuery();
        conn.Close();
            
        Dts.TaskResult.Results.Success();
    }
    
    private void OnInfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs args)
    {
        System.Data.SqlClient.SqlError sqlEvent = default(System.Data.SqlClient.SqlError);
        foreach (var sqlEvent in args.Errors) {
            Dts.Events.FireInformation(sqlEvent.Number, sqlEvent.Procedure, sqlEvent.Message, "", 0, false);
        }
    }
    if it does, just use http://www.developerfusion.com/tools.../vb-to-csharp/ in the future because that only took 3 seconds.

  5. #5
    Join Date
    Aug 2005
    Posts
    198

    Re: Visual Basic to C# Script

    Fast is good, but sometimes it has to be fast + correct.
    This is what you want for the second method:
    Code:
    private void OnInfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs args)
    {
    //INSTANT C# NOTE: Commented this declaration since looping variables in 'foreach' loops are declared in the 'foreach' header in C#:
    //	System.Data.SqlClient.SqlError sqlEvent = null;
    	foreach (System.Data.SqlClient.SqlError sqlEvent in args.Errors)
    	{
    		Dts.Events.FireInformation(sqlEvent.Number, sqlEvent.Procedure, sqlEvent.Message, "", 0, false);
    	}
    }
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Visual Basic to C# Script

    You may want to use a 'using' block to help ensure that resources are freed up properly.

    Code:
     
    public void Main()
    {
      try
      {
        using( SqlConnection conn = new SqlConnection("server=(local);Integrated Security=SSPI;database=Test") )
        {
          conn.InfoMessage += new SqlInfoMessageEventHandler(OnInfoMessage);
        
          conn.Open();
        
          using( SqlCommand cmd = new SqlCommand( conn ) )
          {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "SPWithPrint";
            cmd.ExecuteNonQuery();
                
            Dts.TaskResult.Results.Success();
          }
        }
      }
      catch( Exception ex )
      {
        // Report error
      }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured