简体   繁体   中英

Update SQL Server CE database from DataGridView

I have a SQL class with following variables:

private SqlCeConnection _objSql = null;
private SqlCeCommand Command = null;
private SqlCeDataAdapter DataApater = null;
private SqlCeCommandBuilder CommandBuilder = null;

public void getTableData(string TableName, ref DataTable Data)
{
    // DataTable Data = new DataTable();
    try
    {
        string strSqlCommand = string.Format("SELECT * FROM {0}", TableName);
        Command = new SqlCeCommand(strSqlCommand, _objSql);
        DataApater = new SqlCeDataAdapter(Command);
        CommandBuilder = new SqlCeCommandBuilder(DataApater);
        DataApater.Fill(Data);
    }
    catch (Exception ex)
    {
        throw ex;
    }
    // return Data;
}

Above function is used to get data from the database.

I am using the following code to update:

public void DoUpdate(DataTable Data)
{
    try
    {
        DataApater.Update(Data);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

But this function is not updating the database.

How to update database from a data grid using SqlCeConnection ? Please give the exact code if possible.

DataAdapter need to be initialized and the connection configuration to the database needs to be specified to enable the update function.

Check out an example here: Updating Data Sources with DataAdapters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM