C# 正在更新要保存到SQL的DATAGRID行
大家好,我想了解如何将行保存并编辑到数据库中C# 正在更新要保存到SQL的DATAGRID行,c#,sql,wpf,datagrid,C#,Sql,Wpf,Datagrid,大家好,我想了解如何将行保存并编辑到数据库中 private void BudgetGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e) { SqlCommand gridcmd = new SqlCommand(); SqlConnection rwConn = null; rwConn = new SqlConnectio
private void BudgetGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
SqlCommand gridcmd = new SqlCommand();
SqlConnection rwConn = null;
rwConn = new SqlConnection("server=localhost;" + "Trusted_Connection=yes;" + "database=Production; " + "connection timeout=30");
gridcmd.Connection = rwConn;
rwConn.Open();
//gridcmd.CommandText = "SELECT Id, Name, Quantity, Rate, Time FROM Budget";
gridcmd.CommandText = "UPDATE Budget SET Id = @id, Name = @Name, Quantity = @Qty, Rate = @Rte WHERE Time = @Time";
SqlDataAdapter gridda = new SqlDataAdapter(gridcmd);
string strId = "@id".ToString();
int intID;
bool bintID = Int32.TryParse(strId, out intID);
string strName = "@Name".ToString();
string strQty = "@Qty".ToString();
int intQty;
bool bintQty = Int32.TryParse(strQty, out intQty);
string strRte = "@Rte".ToString();
int intRte;
bool bintRte = Int32.TryParse(strRte, out intRte);
string strTime = "@Time".ToString();
gridda.SelectCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
gridda.SelectCommand.Parameters["@id"].SqlValue = intID;
gridda.SelectCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar));
gridda.SelectCommand.Parameters["@Name"].SqlValue = strName;
gridda.SelectCommand.Parameters.Add(new SqlParameter("@Qty", SqlDbType.Int));
gridda.SelectCommand.Parameters["@Qty"].SqlValue = strQty;
gridda.SelectCommand.Parameters.Add(new SqlParameter("@Rte", SqlDbType.Int));
gridda.SelectCommand.Parameters["@Rte"].SqlValue = strRte;
gridda.SelectCommand.Parameters.Add(new SqlParameter("@Time", SqlDbType.VarChar));
gridda.SelectCommand.Parameters["@Time"].SqlValue = strTime;
DataTable griddt = new DataTable("Budget");
gridda.Fill(griddt);
gridda.UpdateCommand = new SqlCommandBuilder(gridda).GetUpdateCommand();
BudgetGrid.ItemsSource = griddt.DefaultView;
gridda.Update(griddt);
rwConn.Close();
}
它显示得很好。我可以编辑它,但当我点击另一个选项卡时,它不会更新,它会返回到原始数据
我读过的大部分代码要么已经过时。。或者不是我想要的
这是数据库
这是应用程序
所以基本上如果我点击下一行的标签。在事件BudgetGrid\u RowEditEnding
下,它应该更新数据库。。但现在不是了
SqlConnection uniConn = null;
SqlCommand cmd = null;
SqlDataAdapter sda = null;
DataTable dt = new DataTable();
uniConn = new SqlConnection("server=localhost;" + "Trusted_Connection=yes;" + "database=Production; " + "connection timeout=30");
cmd = new SqlCommand("UPDATE Budget(id, Name, Quantity, Rate, Time)", uniConn);
uniConn.Open();
sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
BudgetGrid.ItemsSource = dt.DefaultView;
uniConn.Close();
你放弃关闭连接了吗
您放弃关闭连接了吗?您的SQL语法必须更正如下:
SqlCommand update_comm = new SqlCommand();
update_comm.Connection = Conn;
update_comm.CommandText = "UPDATE Budget SET id= @u_id, Name= @u_name WHERE person= @psn";
var update_da = new SqlDataAdapter(update_comm);
update_da.SelectCommand.Parameters.Add(new SqlParameter("@u_id", SqlDbType.Int));
update_da.SelectCommand.Parameters["@u_id"].Value = yourintvalue;
update_da.SelectCommand.Parameters.Add(new SqlParameter("@u_name", SqlDbType.NVarChar));
update_da.SelectCommand.Parameters["@u_name"].Value = yourstringvalue;
update_da.SelectCommand.Parameters.Add(new SqlParameter("@psn", SqlDbType.NVarChar));
update_da.SelectCommand.Parameters["@psn"].Value = yourstringvalue;
var update_ds = new DataSet();
update_da.Fill(update_ds);
“UPDATE”应与“SET”一起使用
如果要使用DataGrid的编辑行的值更新实际SQL数据库,请尝试此操作
da.UpdateCommand = new SqlCommandBuilder(da).GetUpdateCommand();
da.Update(griddt);
您的SQL语法必须进行如下更正:
SqlCommand update_comm = new SqlCommand();
update_comm.Connection = Conn;
update_comm.CommandText = "UPDATE Budget SET id= @u_id, Name= @u_name WHERE person= @psn";
var update_da = new SqlDataAdapter(update_comm);
update_da.SelectCommand.Parameters.Add(new SqlParameter("@u_id", SqlDbType.Int));
update_da.SelectCommand.Parameters["@u_id"].Value = yourintvalue;
update_da.SelectCommand.Parameters.Add(new SqlParameter("@u_name", SqlDbType.NVarChar));
update_da.SelectCommand.Parameters["@u_name"].Value = yourstringvalue;
update_da.SelectCommand.Parameters.Add(new SqlParameter("@psn", SqlDbType.NVarChar));
update_da.SelectCommand.Parameters["@psn"].Value = yourstringvalue;
var update_ds = new DataSet();
update_da.Fill(update_ds);
“UPDATE”应与“SET”一起使用
如果要使用DataGrid的编辑行的值更新实际SQL数据库,请尝试此操作
da.UpdateCommand = new SqlCommandBuilder(da).GetUpdateCommand();
da.Update(griddt);
只需复制下面的代码。我已经创造了你所有的东西,并且成功地测试了。而不是第一种方式,我试图让你走更受欢迎的方式。因此,我花了很长时间才采纳了 希望这对你有帮助
SqlDataAdapter da;
DataTable dt;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SqlConnection Conn = new SqlConnection();
Conn.ConnectionString = yourConnectionString;
Conn.Open();
SqlCommand gridcomm = new SqlCommand();
gridcomm.Connection = Conn;
gridcomm.CommandText = "SELECT Id, Name, Quantity, Rate, Time FROM Budget";
da = new SqlDataAdapter(gridcomm);
SqlDataReader gridreader = gridcomm.ExecuteReader();
while (gridreader.Read())
{
}
gridreader.Close();
dt= new DataTable("Budget");
da.Fill(dt);
dataGrid_Budget.ItemsSource = dt.DefaultView;
Conn.Close();
}
private void dataGrid_Budget_RowEditEnding(object sender, System.Windows.Controls.DataGridRowEditEndingEventArgs e)
{
DataGridRow editedrow = e.Row;
int row_index = (DataGrid)sender).ItemContainerGenerator.IndexFromContainer(editedrow);
for (int k=0;k< 5;k++)
{
DataGridCell cell = GetCell(row_index, k);
TextBlock tb = cell.Content as TextBlock;
if (k==1)
{
dt.Rows[row_index][k] = tb.Text;
}
else if (k == 4)
{
if (tb.Text != "")
{
dt.Rows[row_index][k] = Convert.ToDateTime(tb.Text);
}
}
else
{
dt.Rows[row_index][k] = Convert.ToInt32(tb.Text);
}
}
da.UpdateCommand = new SqlCommandBuilder(da).GetUpdateCommand();
da.Update(dt);
}
public DataGridCell GetCell(int row, int column)
{
DataGridRow rowContainer = GetRow(row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
dataGrid_Budget.ScrollIntoView(rowContainer, dataGrid_Budget.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
return null;
}
public DataGridRow GetRow(int index)
{
DataGridRow row = (DataGridRow)dataGrid_Budget.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
dataGrid_Budget.UpdateLayout();
dataGrid_Budget.ScrollIntoView(dataGrid_Budget.Items[index]);
row = (DataGridRow)dataGrid_Budget.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
SqlDataAdapter;
数据表dt;
已加载私有无效窗口(对象发送器、路由目标)
{
SqlConnection Conn=新的SqlConnection();
Conn.ConnectionString=您的ConnectionString;
Conn.Open();
SqlCommand gridcomm=新SqlCommand();
gridcomm.Connection=Conn;
gridcomm.CommandText=“从预算中选择Id、名称、数量、费率、时间”;
da=新的SqlDataAdapter(gridcomm);
SqlDataReader gridreader=gridcomm.ExecuteReader();
while(gridreader.Read())
{
}
gridreader.Close();
dt=新数据表(“预算”);
da.填充(dt);
dataGrid_Budget.ItemsSource=dt.DefaultView;
康涅狄格州关闭();
}
私有无效dataGrid\u Budget\u RowEditEnding(对象发送者,System.Windows.Controls.DataGridRowEditEndingEventArgs e)
{
DataGridRow editedrow=e.行;
int row_index=(DataGrid)sender).ItemContainerGenerator.IndexFromContainer(editedrow);
对于(int k=0;k<5;k++)
{
DataGridCell=GetCell(行索引,k);
TextBlock tb=单元格。内容为TextBlock;
如果(k==1)
{
dt.Rows[row_index][k]=tb.Text;
}
else如果(k==4)
{
如果(tb.Text!=“”)
{
dt.Rows[row_index][k]=Convert.ToDateTime(tb.Text);
}
}
其他的
{
dt.Rows[row_index][k]=转换为32(tb.Text);
}
}
da.UpdateCommand=新的SqlCommandBuilder(da).GetUpdateCommand();
数据更新(dt);
}
公共DataGridCell GetCell(int行,int列)
{
DataGridRow rowContainer=GetRow(行);
if(rowContainer!=null)
{
DataGridCellsPresenter=GetVisualChild(行容器);
DataGridCell=(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(列);
if(单元格==null)
{
dataGrid_Budget.ScrollIntoView(行容器,dataGrid_Budget.Columns[column]);
单元格=(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(列);
}
返回单元;
}
返回null;
}
公共DataGridRow GetRow(int索引)
{
DataGridRow=(DataGridRow)dataGrid_Budget.ItemContainerGenerator.ContainerFromIndex(index);
if(行==null)
{
dataGrid_Budget.UpdateLayout();
dataGrid_Budget.ScrollIntoView(dataGrid_Budget.Items[索引]);
行=(DataGridRow)dataGrid_Budget.ItemContainerGenerator.ContainerFromIndex(index);
}
返回行;
}
公共静态T GetVisualChild(可视父级),其中T:VisualChild
{
T child=默认值(T);
int numVisuals=VisualTreeHelper.GetChildrenCount(父级);
对于(int i=0;i
只需复制下面的代码即可。我已经创造了你所有的东西,并且成功地测试了。而不是第一种方式,我试图让你走更受欢迎的方式。因此,我花了很长时间才采纳了
希望这对你有帮助
SqlDataAdapter da;
DataTable dt;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SqlConnection Conn = new SqlConnection();
Conn.ConnectionString = yourConnectionString;
Conn.Open();
SqlCommand gridcomm = new SqlCommand();
gridcomm.Connection = Conn;
gridcomm.CommandText = "SELECT Id, Name, Quantity, Rate, Time FROM Budget";
da = new SqlDataAdapter(gridcomm);
SqlDataReader gridreader = gridcomm.ExecuteReader();
while (gridreader.Read())
{
}
gridreader.Close();
dt= new DataTable("Budget");
da.Fill(dt);
dataGrid_Budget.ItemsSource = dt.DefaultView;
Conn.Close();
}
private void dataGrid_Budget_RowEditEnding(object sender, System.Windows.Controls.DataGridRowEditEndingEventArgs e)
{
DataGridRow editedrow = e.Row;
int row_index = (DataGrid)sender).ItemContainerGenerator.IndexFromContainer(editedrow);
for (int k=0;k< 5;k++)
{
DataGridCell cell = GetCell(row_index, k);
TextBlock tb = cell.Content as TextBlock;
if (k==1)
{
dt.Rows[row_index][k] = tb.Text;
}
else if (k == 4)
{
if (tb.Text != "")
{
dt.Rows[row_index][k] = Convert.ToDateTime(tb.Text);
}
}
else
{
dt.Rows[row_index][k] = Convert.ToInt32(tb.Text);
}
}
da.UpdateCommand = new SqlCommandBuilder(da).GetUpdateCommand();
da.Update(dt);
}
public DataGridCell GetCell(int row, int column)
{
DataGridRow rowContainer = GetRow(row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
dataGrid_Budget.ScrollIntoView(rowContainer, dataGrid_Budget.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
return null;
}
public DataGridRow GetRow(int index)
{
DataGridRow row = (DataGridRow)dataGrid_Budget.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
dataGrid_Budget.UpdateLayout();
dataGrid_Budget.ScrollIntoView(dataGrid_Budget.Items[index]);
row = (DataGridRow)dataGrid_Budget.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
SqlDataAdapter;
数据表dt;
已加载私有无效窗口(对象发送器、路由目标)
{
SqlConnection Conn=新的SqlConnection();
Conn.ConnectionString=您的ConnectionString;
Conn.Open();
SqlCommand gridcomm=新SqlCommand();
gridcomm.Connection=Conn;
gridcomm.CommandText=“从预算中选择Id、名称、数量、费率、时间”;
da=新的SqlDataAdapter(gridcomm);
SqlDataReader gridreader=gridcomm.ExecuteReader();
while(gridreader.Read())
{
}
gridreader.Close();
dt=新数据表(“预算”);
da.填充(dt);
dataGrid_Budget.ItemsSource=dt.DefaultView;
康涅狄格州关闭();
}
私有无效dataGrid\u Budget\u RowEditEnding(对象发送者,System.Windows.Controls.DataGridRowEditEndingEventArgs e)
{
DataGridRow editedrow=e.行;
int row_index=(DataGrid)sender).ItemContainerGenerator.IndexFromContainer(editedrow);
对于(int k=0;k<5;k++)
{
DataGridCell=GetCell(行索引,k);
TextBlock tb=单元格。内容为TextBlock;
我