Domanda Risolto Visual Studio C# salvare il contenuto di DataGridView.

Stato
Discussione chiusa ad ulteriori risposte.

Pacifico437

Utente Gold
27 Aprile 2012
166
78
6
200
Gent.mi,

gradirei gentilmente come da titolo salvare in Database di Access.
Riporto il codice che popola il DataGridView:

Codice:
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Documenti.mdb";
 OleDbConnection Conn;
 OleDbDataAdapter Adapter;
 string tabella = "Archivio";
 DataSet Ds = new DataSet();
DataTable table = new DataTable();
e in Form_Load:
Tabella_DGV.DataSource = table;
Conn = new OleDbConnection(connStr);
 string sSQL = ("SELECT * FROM " + tabella);
 OleDbCommand Cmd = new OleDbCommand(sSQL, Conn);
 Adapter = new OleDbDataAdapter(Cmd);
 OleDbCommandBuilder CB = new OleDbCommandBuilder(Adapter);
 Adapter.Fill(Ds, tabella);
 Tabella_DGV.DataSource = Ds.Tables[tabella];
Il codice che dovrebbe salvare il contenuto del DGV in Database di Access:
Codice:
for (int i = 0; i < Tabella_DGV.Rows.Count - 1; i++)
{
  if (i != Tabella_DGV.Rows.Count - 1)
  {
    string sSQL = "INSERT INTO Archivio(ID, Descrizione, Abitazione,     Casa) VALUES('" + Tabella_DGV.Rows[i].Cells[0].Value + "','" +         Tabella_DGV.Rows[i].Cells[1].Value + "','"                                 + "','" + Tabella_DGV.Rows[i].Cells[2].Value + "','" +      Tabella_DGV.Rows[i].Cells[3].Value + "',')";
  }
  else
  {
     string sSQL = "INSERT INTO Achivio(ID, Descrizione, Abitazione,           Casa) VALUES('" + Tabella_DGV.Rows[i].Cells[0].Value + "','" +           Tabella_DGV.Rows[i].Cells[1].Value + "','"                                + "','" + Tabella_DGV.Rows[i].Cells[2].Value + "','" +            Tabella_DGV.Rows[i].Cells[3].Value + "'')";
  }
  }
Non salva, se gentilmente potete correggere o modificare.
Anticipatamente saluto.
Domenico.
 
VBNET
Codice:
Public adapter As OleDbDataAdapter
    Public dt As DataTable

    Private Sub loaddate()
        dt = New DataTable()
        Dim conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=documenti.accdb; Persist Security Info=False;")
        conn.Open()
        adapter = New OleDbDataAdapter(selectSql, conn)
        adapter.Fill(dt)
        Tabella_DGV.DataSource = dt
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Dim scb = New OleDbCommandBuilder(adapter)
            adapter.Update(dt)
            MessageBox.Show("OK!")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
Messaggio unito automaticamente:

C#
Codice:
class SurroundingClass
{
    public OleDbDataAdapter adapter;
    public DataTable dt;

    private void loaddate()
    {
        dt = new DataTable();
        var conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=documenti.accdb; Persist Security Info=False;");
        conn.Open();
        adapter = new OleDbDataAdapter(selectSql, conn);
        adapter.Fill(dt);
        Tabella_DGV.DataSource = dt;
    }

    private void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            var scb = new OleDbCommandBuilder(adapter);
            adapter.Update(dt);
            MessageBox.Show("OK!");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}
 
Stato
Discussione chiusa ad ulteriori risposte.