0

我编写了一个简单的类,可以从 mssql 紧凑型数据库中选择、插入和删除。我在 ADO.NET 中使用了连接层。但不幸的是,代码的选择块有效,但插入和删除代码块并没有像我预期的那样工作。当我运行插入或删除操作时,我看不到数据库中有任何变化。但是代码运行成功。这几天我一直在想办法。这是下面的代码。

class Logic
    {
        SqlCeConnection con = null;

        //code the constructor of the class
        public Logic() { 
            //now we are going to collect the database configuration string
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectedCrud.Properties.Settings.DatabaseConnectionString"].ConnectionString;

            con = new SqlCeConnection(connectionString);

            Console.WriteLine(con.Database);
        }

        //This method is going to delete from the database
        public void deleteValue(int r){
            int k = 0;
            string sql = string.Format("delete from Details where id = {0}",r);
            using (SqlCeCommand command = new SqlCeCommand(sql, this.con)){
                try
                {
                    //open the connection 
                    this.con.Open();
                    k = command.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally{
                    this.con.Close();
                }
            }

            if (k > 0)
            {
                Console.WriteLine("The rows as been deleted");
            }
            else
                Console.WriteLine("The row was not deleted");

        }
}
4

0 回答 0