3
Answers

converting vb.net code into c#

Hussain Afridi

Hussain Afridi

12y
1.3k
1
salam sir i want to convert the following vb.net code into csharp...

Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class clsDML

  Public conn As SqlConnection


  Public Function DML(ByVal Query As String)
  Try


  Dim connectionstring As String

  connectionstring = CreateConnection()

  conn = New SqlConnection(connectionstring)
  conn.Open()

  Dim cmd As New SqlCommand

  cmd.Connection = conn
  cmd.CommandType = CommandType.Text
  cmd.CommandText = Query
  cmd.ExecuteNonQuery()
  conn.Close()
  Catch ex As Exception
  MessageBox.Show(ex.ToString())
  End Try
  End Function
  Public Function GetArray(ByVal Query As String, Optional ByVal LocalServer As String = "")


  Try
  Dim connectionstring As String

  connectionstring = CreateConnection()

  conn = New SqlConnection(connectionstring)
  conn.Open()
  Dim cmd As New SqlCommand
  cmd.Connection = conn

  cmd.CommandText = Query
  Dim data As SqlDataReader = cmd.ExecuteReader()  'reader = command.ExecuteReader();

  Dim i, j, x As Int32
  While (data.Read())
  i = i + 1
  End While
  Dim count As Int32
  Dim A(i - 1, data.FieldCount - 1)

  count = data.FieldCount
  data.Close()
  Dim data1 As SqlDataReader = cmd.ExecuteReader()
  j = 0
  While (data1.Read())

  For x = 0 To count - 1

  A(j, x) = data1.GetValue(x).ToString()

  Next

  j = j + 1
  End While
  data1.Close()
 
  Return A
  Catch ex As Exception


  End Try

  End Function
  Public Function CreateConnection() As String
  Dim source As String

  source = "data source=(local);database=mdin;user id=sa; pwd=;Persist Security Info=False"


  Return source
  End Function
End Class

Attachment: clsdml.zip

Answers (3)
0
Vulpes
NA 98.3k 1.5m 12y
Santhosh's translation looks OK to me except for this line:

   Dim A(i - 1, data.FieldCount - 1) 

where he has:

  [,] A = new[];

I think that needs to be:

  object[,] A = new object[i, data.FieldCount];

Notice also that you need to be using C# 4.0 (VS 2012) or later for the GetArray method to support the use of the optional parameter, LocalServer.




0
Satyapriya Nayak
NA 53k 8m 12y
Hi Hussain,


You can convert it by using some online tools as below


http://www.developerfusion.com/tools/convert/vb-to-csharp/



Thanks
0
Santhosh Kumar Jayaraman
NA 9.9k 2.3m 12y

using System;
using System.Data;
using System.Data.SqlClient;

public class clsDML
{


    public SqlConnection conn;

    public object DML(string Query)
    {

        try {

            string connectionstring = null;

            connectionstring = CreateConnection();

            conn = new SqlConnection(connectionstring);
            conn.Open();

            SqlCommand cmd = new SqlCommand();

            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = Query;
            cmd.ExecuteNonQuery();
            conn.Close();
        } catch (Exception ex) {
            MessageBox.Show(ex.ToString());
        }
    }
    public object GetArray(string Query, string LocalServer = "")
    {


        try {
            string connectionstring = null;

            connectionstring = CreateConnection();

            conn = new SqlConnection(connectionstring);
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;

            cmd.CommandText = Query;
            SqlDataReader data = cmd.ExecuteReader();
            //reader = command.ExecuteReader();

            Int32 i = default(Int32);
            Int32 j = default(Int32);
            Int32 x = default(Int32);
            while ((data.Read())) {
                i = i + 1;
            }
            Int32 count = default(Int32);
            [,] A = new[];

            count = data.FieldCount;
            data.Close();
            SqlDataReader data1 = cmd.ExecuteReader();
            j = 0;

            while ((data1.Read())) {

                for (x = 0; x <= count - 1; x++) {
                    A(j, x) = data1.GetValue(x).ToString();

                }

                j = j + 1;
            }
            data1.Close();

            return A;

        } catch (Exception ex) {

        }

    }
    public string CreateConnection()
    {
        string source = null;

        source = "data source=(local);database=mdin;user id=sa; pwd=;Persist Security Info=False";


        return source;
    }
}

//