INSERT INTO in Visual Studio 2005

venerdì 12 settembre 2008 - 09.28

Mau67 Profilo | Expert

Buongiorno a tutti, ho la necessità di fare una insert into in una tabella prendendo alcuni dati da un altra tabella e nello stesso tempo generare un
codice come chiave per ogni inserimento.

Mi spiego Devo prendere i dati Della tabella AMMINISTRATI campo IDUtente e con un ciclo FOR ...NEXT devo inserire i dati presenti nel campo IDUtente li devo inserire con un insert into nella tabella INDENNITA', e nello stesso tempo per ogni IDUtente mi deve generare un codice che decido io cioè (IN1000 - IN1001 ecc) che inserirò nel campo IDIndennità della tabella INDENNITA'.

I dati della tabella ANAGRAFICA dovrebbe leggerli in questo modo con i parametri che ho impostato

cmd.CommandText = "SELECT IDUtente,Grado, Cognome, Nome, Gruppo, DataInserimento FROM Anagrafica Where DataInserimento <= '" & data & "' And Gruppo = '" & cboGruppo.Text & "'"


Vi posto il codice che ho generato per la INSERT INTO al quale non so come inserirgli la stringa cmd.commandText

If Not File.Exists(FILE_NAME) Then
MsgBox(FILE_NAME & " Stringa di connessione mancante.")
Return
End If
Dim sr As StreamReader = File.OpenText(FILE_NAME)
Dim sqlString As String
sqlString = sr.ReadLine()
Me.ToolStripStatusLabel2.Text = (sqlString) 'Questo è solo per verificare!
sr.Close()

Dim objConnection As New SqlConnection(sqlString)
Dim data As Date = Microsoft.VisualBasic.DateValue(Now)


' Declare local variables and objects...
Dim intPosition As Integer, intMaxID As Integer
Dim strID As String
Dim objCommand As SqlCommand = New SqlCommand()

' Save the current record position...
intPosition = objCurrencyManager.Position
' Create a new SqlCommand object...
Dim maxIdCommand As SqlCommand = New SqlCommand _
("SELECT MAX(IDIndennità) AS MaxID " & _
"FROM Indennità WHERE IDIndennità LIKE 'IN%'", objConnection)

' Open the connection, execute the command
objConnection.Open()
Dim maxId As Object = maxIdCommand.ExecuteScalar()

' If the MaxID column is null...
If maxId Is DBNull.Value Then
' Set a default value of 1000...
intMaxID = 1000
Else
' otherwise set the strID variable to the value in MaxID...
strID = CType(maxId, String)
' Get the integer part of the string...
intMaxID = CType(strID.Remove(0, 2), Integer)
' Increment the value...
intMaxID += 1
End If

' Finally, set the new ID...
strID = "IN" & intMaxID.ToString

' Set the SqlCommand object properties...
objCommand.Connection = objConnection
objCommand.CommandText = "INSERT INTO Indennità (IDUtente,Giorni, Mese, Anno," & _
"Motivo, DataPagamento)" & _
"VALUES (@IDUtente, @Giorni, @Mese, @Anno," & _
"@Motivo, @DataPagamento)"

' Add parameters for the placeholders in the SQL in the
' CommandText property...

objCommand.Parameters.Clear()

' Parameter for the title column...
objCommand.Parameters.AddWithValue("@IDIndennità", strID)

' Parameter for the title column...
objCommand.Parameters.AddWithValue("@IDUtente", QUI NON SO COME FARGLI LEGGERE I DATI DALLA TABELLA ANAGRAFICA)


' Parameter for the title column...
objCommand.Parameters.AddWithValue("@Giorni", 30)

' Parameter for the type column
objCommand.Parameters.AddWithValue("@Mese", cboMese.Text)

' Parameter for the price column...
objCommand.Parameters.AddWithValue("@Anno", txtAnno.Text)

' Parameter for the price column...
objCommand.Parameters.AddWithValue("@Motivo", "Indennità )")


' Parameter for the price column...
objCommand.Parameters.AddWithValue("@DataPagamento", data).DbType _
= DbType.DateTime




' Execute the SqlCommand object to insert the new data...
Try
objCommand.ExecuteNonQuery()
Catch SqlExceptionErr As SqlException
MessageBox.Show(SqlExceptionErr.Message)
End Try

' Close the connection...
objConnection.Close()


AIUTATEMI per me è importante e urgente grazie.
Mau67
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5