2
public DataTable FillDataGrid()
{
    string CmdString = string.Empty;
    using (SqlCeConnection con = new SqlCeConnection(ConString))
    {
        CmdString = "SELECT categories.categories_id as ID, categories_description.categories_name as name,categories.categories_image as Image,categories.parent_id as parentId,categories.sort_order as sortOrder,categories.date_added as dateAdded,categories.last_modified as lastModified FROM categories INNER JOIN categories_description ON categories.categories_id=categories_description.categories_id where categories_description.language_id=1";

        SqlCeCommand cmd = new SqlCeCommand(CmdString, con);
        DataTable dt = new DataTable("categories");
        SqlCeDataAdapter adapter = new SqlCeDataAdapter(cmd);
        adapter.Fill(dt);

        return dt;
    }   
}

public void FillDataGrid()
{
    DatabaseCore db = new DatabaseCore();
    DataTable dt = db.FillDataGrid();
    show_query.ItemsSource = dt.DefaultView;
}

我正在使用 sqlserverCe 作为数据库和 Visual Studio 2012(Windows 演示文稿表单)

我正在使用两个表来填充数据网格视图,并且它工作正常。

我需要从我搜索过的 datagridview 更新数据库,但没有为我的问题找到合适的解决方案.. 单表更新的大部分搜索结果

我使用了 SqlCeCommandBuilder 但它给出了错误,它不适用于基于多个的表,建议我这样做

4

1 回答 1

3

CommandBuilder仅用于简单SELECT语句,即用于单个表,当您使用时它不会更新JOIN...

Automatically generates single-table commands that are used to reconcile changes made to a DataSet with the associated SQL Server database. This class cannot be inherited. (MSDN)

您将不得不使用参数化查询。

这是一个非常简单的参数化查询的示例...

string value = DataGrid.Rows[0].Cells["ID"].Value);; // Takes the value from TextBox1
Cmd = new SqlCommand("DELETE FROM Table WHERE CountryMasterId = @ID", Con); // makes a new SqlCommand object for delete query and `@ID` is the parameter for passing the value...
Cmd.Parameters.AddWithValue("@ID", value);

现在您可以扩展它并设置您使用数据网格中的值来更新表。或者,您可以使用存储过程。您只需要传递参数,然后就可以在存储过程中使用多个更新语句来更新多个表...

这些链接将对您有所帮助...

于 2014-06-13T13:08:02.827 回答