GRAVAR DATAGRIDVIEW NO BANCO DE DADOS
Como fazer isto gravar os dados editados no datagridview na tabela.
Estou usado vb.net2010 e o banco de dados sdf
Já fiz dezenas de pesquisas e não consigo
Obrigado
Estou usado vb.net2010 e o banco de dados sdf
Já fiz dezenas de pesquisas e não consigo
Imports System.Data
Imports System.Data.SqlServerCe
Public Class Form1
Dim myConnectionString As String = _
[Ô]Data Source=[ô]|DataDirectory|\DADOS.sdf[ô];[Ô]
Dim SQL As String = [Ô]INSERT INTO PERFIS([REF]) VALUES (@referencia);[Ô]
Dim connection As New SqlCeConnection(myConnectionString)
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
connection.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
connection.Open()
[ô]TODO: This line of code loads data into the [ô]DADOSDataSet.PERFIS[ô] table. You can move, or remove it, as needed.
Me.PERFISTableAdapter.Fill(Me.DADOSDataSet.PERFIS)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
[ô]Inserir
Dim command As New SqlCeCommand(SQL, connection)
command.Parameters.Add([Ô]@referencia[Ô], SqlDbType.NVarChar).Value = TextBox1.Text
Dim IdRegistoInserido As Integer = command.ExecuteScalar()
Me.PERFISTableAdapter.Fill(Me.DADOSDataSet.PERFIS)
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
[ô]Salvar
Dim ds As New DADOSDataSet
Dim adp As New SqlCeDataAdapter([Ô]select * from Perfis[Ô], myConnectionString)
Dim cb As New SqlCeCommandBuilder(adp)
adp.Fill(ds, [Ô]Perfis[Ô])
adp.Update(ds, [Ô]PERFIS[Ô])
adp.AcceptChangesDuringUpdate = True
End Sub
End Class
Obrigado
Eu gravo os dados do datagrid assim
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i = 0 To DG.Rows.Count - 1
Sql = [Ô]INSERT INTO Clientes(Nome,Bairro,Cidade,Telefone)Values([ô][Ô] & _
DG.Item([Ô]Nome[Ô], i).Value & [Ô][ô],[ô][Ô] & _
DG.Item([Ô]Bairro[Ô], i).Value & [Ô][ô],[ô][Ô] & _
DG.Item([Ô]Cidade[Ô], i).Value & [Ô][ô],[ô][Ô] & _
DG.Item([Ô]Telefone[Ô], i).Value & [Ô][ô])[Ô]
CMD = New SqlCommand(Sql, CNN)
CMD.ExecuteNonQuery()
Next
End Sub
Onde DG é o nome do meu datagrid
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i = 0 To DG.Rows.Count - 1
Sql = [Ô]INSERT INTO Clientes(Nome,Bairro,Cidade,Telefone)Values([ô][Ô] & _
DG.Item([Ô]Nome[Ô], i).Value & [Ô][ô],[ô][Ô] & _
DG.Item([Ô]Bairro[Ô], i).Value & [Ô][ô],[ô][Ô] & _
DG.Item([Ô]Cidade[Ô], i).Value & [Ô][ô],[ô][Ô] & _
DG.Item([Ô]Telefone[Ô], i).Value & [Ô][ô])[Ô]
CMD = New SqlCommand(Sql, CNN)
CMD.ExecuteNonQuery()
Next
End Sub
Onde DG é o nome do meu datagrid
Ok é uma solução, obrigado
Outra forma?
Outra forma?
Assim duplica os dados na tabela
Tentei simplificar mas deu erro
Tentei simplificar mas deu erro
Imports System.Data
Imports System.Data.SqlServerCe
Public Class Form1
Dim myConnectionString As String = _
[Ô]Data Source=[ô]|DataDirectory|\DADOS.sdf[ô];[Ô]
Dim connection As New SqlCeConnection(myConnectionString)
Dim da As New SqlCeDataAdapter([Ô]select * from PERFIS[Ô], connection)
Dim ds As New DADOSDataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
[ô]ds = New DataSet
da.Fill(ds, [Ô]PERFIS[Ô])
DataGridView1.DataSource = ds.Tables([Ô]Perfis[Ô])
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
[ô]Salvar
Dim cb As New SqlCeCommandBuilder(da)
da.Update(ds, [Ô]PERFIS[Ô])
End Sub
End Class
Só para dizer que já consegui
Na tabela tinha que ter um campo com chave primária, era só isto.
Eis aqui o código funcionando
Na tabela tinha que ter um campo com chave primária, era só isto.
Eis aqui o código funcionando
Imports System.Data
Imports System.Data.SqlServerCe
Public Class Form1
Dim myConnectionString As String = _
[Ô]Data Source=[ô]D:\Projects\Stocks\MyDados\DADOS.sdf[ô];[Ô]
[ô][Ô]Data Source=[ô]|DataDirectory|\DADOS.sdf[ô];[Ô]
Dim connection As New SqlCeConnection(myConnectionString)
Dim da As New SqlCeDataAdapter([Ô]select * from PERFIS[Ô], connection)
Dim ds As New DataSet
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
If Not ds.GetChanges Is Nothing Then
If MsgBox([Ô]Existe alterações, deseja guarda-las?[Ô], MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
guardar()
End If
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
da.Fill(ds, [Ô]PERFIS[Ô])
DataGridView1.DataSource = ds.Tables([Ô]Perfis[Ô])
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
[ô]Salvar
guardar()
End Sub
Private Sub guardar()
Dim cb As New SqlCeCommandBuilder(da)
Try
da.Update(ds, [Ô]PERFIS[Ô])
Catch exc As Exception
MsgBox([Ô]Ocorreu um erro[Ô])
Finally
MsgBox([Ô]Guardado com sucesso![Ô])
End Try
End Sub
End Class
Tópico encerrado , respostas não são mais permitidas