2017-11-30 2 views
1

Also ich versuche, C# zu lernen, weil ich meine alte Programmierfähigkeit ändern will, die vb.net ist im Öffnen einiger Konverter Seite, aber es funktioniert nicht. Ich möchte nur den Code von USING und WITH in vb.net zu C# wissen.vb.net code VERWENDUNG und MIT was ist der code für C#

hier ist mein Code in vb.net:

Dim rand As New Random 
    abuildnumber.Text = rand.Next 
    Dim exist As String = String.Empty 
    exist &= "select * from stocks " 
    exist &= "where [email protected]" 
    Using conn As New SqlConnection("server=WIN10;user=admin;password=12345;database=pc_parts") 
     Using cmd As New SqlCommand 
      With cmd 
       .Connection = conn 
       .CommandType = CommandType.Text 
       .CommandText = exist 
       .Parameters.AddWithValue("@build", abuildnumber.Text) 
      End With 
      Try 
       conn.Open() 
       Dim reader As SqlDataReader = cmd.ExecuteReader 
       If reader.HasRows Then 
        reader.Close() 
        abuildnumber.Text = rand.Next 
       End If 
       abrand.Enabled = True 
       apart.Enabled = True 
       aquantity.Enabled = True 
       aday.Enabled = True 
       amonth.Enabled = True 
       ayear.Enabled = True 
       add.Enabled = True 
       conn.Close() 
      Catch ex As Exception 
       MsgBox(ex.Message) 
      End Try 
     End Using 
    End Using 
End Sub 
+2

Mögliche Duplikat (https://stackoverflow.com/questions/1063429/ Äquivalenz-mit-Ende-mit-in-c) – Magnetron

Antwort

2

Die Verwendung der Syntax ist sehr ähnlich:

using (conn As new SqlConnection("server=WIN10;user=admin;password=12345;database=pc_parts")) 
{ 
    // some code here 
} 

Ich bin mir nicht sicher, ob es eine direkte Möglichkeit gibt, das zu tun, was Sie mit "With" machen. Obwohl, können Sie Zuweisungen in Ihre Objektdeklaration wie folgt Inline: [? Äquivalenz von "With ... End With" in C#]

cmd = new SqlCommand { 
    Connection = conn, 
    CommandType = CommandType.Text, 
    CommandText = exist 
}; 
cmd.Parameters.AddWithValue("@build", abuildnumber.Text); 
+0

danke das ist eine große Hilfe für im bekommen wirklich schwindlig finden die gleiche Syntax für C# – Ndrangheta

3

In C# die Verwendung ist so ziemlich das gleiche Syntax:

// Notice single lined, or multi lined using statements { } 
using (var conn = new SqlConnection("server=WIN10;user=admin;password=12345;database=pc_parts")) 
    using (var cmd = new SqlCommand()) 
    { 

    } 

Zum Glück gibt es keine With Äquivalent in C#

+2

"Zum Glück" hast du eine Stimme :) C# kann über viele Zeilen erstrecken, was nützlich ist – Grantly