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,但它给出的错误,它是不适用于多基表建议我做这件事
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);
现在您可以扩展它并设置您使用数据网格中的值来更新表。或者,您可以使用存储过程。你只需要传递参数,然后你可以在存储过程中使用多个更新语句来更新多个表…
这些链接将对你有帮助…
- Link1
- Link2