在ADO.NET中使用连接层时,插入和删除操作不起作用

时间:2013-02-11 19:32:25

标签: .net ado.net

我编写了一个简单的类,可以从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");

        }
}

0 个答案:

没有答案