Donate

Insert Parameter Array In VB.NET

Here's a snippet to insert a parameter array in .NET:
' add a class ex. Patient  
        Public Class Patient  
           Public name As String  
          Public code As Integer  
         End Class  
         ' create a delegate   
         Delegate Function myConn() As String  
         'function to return connstring  
         Private Function connectionstring() As String  
          Dim conn As String = "your_connection_string here"  
          Return conn  
        End Function  
        '--Main Sub  
        ' execute insert array    
        Dim objCon As myConn  
          objCon = New myConn(AddressOf connectionstring)  
          Dim str As String = objCon.Invoke()  
          Dim connection As New SqlConnection(str)  
          Try  
            Dim patients As New List(Of Patient)  
            For i As Integer = 0 To max_count_variable  
              Dim patient As New Patient()  
              patient.name = "greg nelson" + i.ToString  
              patient.code = i * 2  
              patients.Add(patient)  
            Next  
            connection.Open()  
            Dim query As New StringBuilder  
            Dim cmd As New SqlCommand  
            For j As Integer = 0 To patients.Count - 1  
              query.Append(String.Format("Insert into patients  
                 (PatientName,PatientSexCode)   
                 values(@names{0},@code{1});", j, j))  
              'declare the parameters  
              cmd.Parameters.AddWithValue(String.Format  
               ("@names{0}", j), patients(j).name)  
              cmd.Parameters.AddWithValue(String.Format  
               ("@code{0}", j), patients(j).code)  
            Next  
            cmd.Connection = connection  
            cmd.CommandText = query.ToString()  
            cmd.ExecuteNonQuery()  
          Catch ex As ApplicationException  
            Throw New Exception(ex.Message)  
          End Try  
          connection.Close()  
          Console.Write("Array successfully saved")  
          Console.ReadLine()  
        '--end main sub 

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.