简体   繁体   English

从DataGridView更新SQL Server CE数据库

[英]Update SQL Server CE database from DataGridView

I have a SQL class with following variables: 我有一个带有以下变量的SQL类:

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 ? 如何使用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. 需要初始化DataAdapter并需要指定与数据库的连接配置以启用更新功能。

Check out an example here: Updating Data Sources with DataAdapters 在此处查看示例: 使用DataAdapter更新数据源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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