1

我确实有一个 ListView,我从数据库(它的数据选择)中调用它,每一行我都有一个删除、更新按钮。我将每个按钮绑定到列表视图中的 ID,因此当我单击它时,只需返回该行值!

但是我想在我的查询函数中调用这个值来更新或删除选定的行。我不确定如何将此值调用到 Query 中。

这是我的绑定示例:

<Button Name="delete" Click="Delete_Click" CommandParameter="{Binding Path=empID}">

我是这样称呼它的:

Button _button = (Button)sender;
string empID = _button.CommandParameter.ToString();

\sql 的东西

SqlCeConnection con = new SqlCeConnection();
                SqlCeDataAdapter ad = new SqlCeDataAdapter();
                SqlCeCommand cmd = new SqlCeCommand(empID);

                String str = "UPDATE employee SET Isdeleted ='1' WHERE empID= " + empID;
                cmd.Parameters.AddWithValue("@empID", empID);

                ad.SelectCommand = cmd;

希望我能得到一些帮助,因为到目前为止这个查询永远不会起作用!

4

2 回答 2

0
SqlCeConnection con = new SqlCeConnection();
            SqlCeDataAdapter ad = new SqlCeDataAdapter();
            SqlCeCommand cmd = new SqlCeCommand(empID);

            String str = "UPDATE employee SET Isdeleted ='1' WHERE empID= " + empID;
            cmd.Parameters.AddWithValue("@empID", empID);

            ad.SelectCommand = cmd;

这是一个奇怪的设置

也许:

SqlCeConnection con = new SqlCeConnection();
            SqlCeDataAdapter ad = new SqlCeDataAdapter();
            String str = "UPDATE employee SET Isdeleted ='1' WHERE empID= @empID";

            SqlCeCommand cmd = new SqlCeCommand(str);


            cmd.Parameters.AddWithValue("@empID", empID);

            ad.UpdateCommand = cmd; 
于 2012-11-22T02:25:51.223 回答
0

不确定它是否会起作用,但你应该试一试,我是为本地 sdf 数据库制作的

var con = new SqlCeConnection("Data Source=" + "|DataDirectory|\\Database1.sdf");
                String str = "UPDATE employee SET Isdeleted ='1' WHERE empID= '"+ empID +"'";
                var cmd = new SqlCeCommand(str,con);

                try
                {
                    con.Open();
                }
                catch (Exception a)
                {
                    Console.WriteLine(a.ToString());
                }
                finally
                {
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
于 2012-11-22T02:47:53.500 回答