代码之家  ›  专栏  ›  技术社区  ›  Liam neesan

为什么不使用ASP.NET在SQL Server中删除我的记录?

  •  1
  • Liam neesan  · 技术社区  · 6 年前

    我试图在数据库删除目录后从数据库中删除记录。

    我有一个要删除的按钮,一旦我点击这个按钮,它就必须调用ajax,然后删除 Folder 如果存在,则删除文件夹,然后删除数据库中的一条记录

    以下是我的Ajax代码:

    // Delete Folder
    $("#btnDeleteFolder").click(function () {
        var dirPath = $('#ContentPlaceHolder1_txtPath_I').val();
        var folderName = $('#txtFolderName').val();
        var location = $('#ddlLocation option:selected').text();
        alert("FolderName = "+folderName);
        alert("Location = "+location);
    
        //alert('calling path =' + dirPath);
        $.ajax({
            method: 'post',
            url: "GetAllFolderDetails.asmx/setDeleteFolder",
    
            data: {
                dirLocation: dirPath,
                folderName: folderName,
                location: location
            },
    
            dataType: "json",
            success: function (data) {
                location.reload(true);
                //alert("Success");
    
            },
            error: function (result) {
                alert("Error");
            }
        });
    });
    

    Asmx.cs

    public void setDeleteFolder(string dirLocation,string folderName,string location)
    {
        // First check Whether Other Folder or Files Exists inside the folder
        // If it Exist, Delete those files and then delete your SELECTED Folder
        string insidePath = Server.MapPath("~/" + dirLocation);
        string[] files = Directory.GetFiles(insidePath, "*", SearchOption.AllDirectories);
    
        foreach (string file in files)
        {
            File.Delete(file);
        }
    
        if (Directory.Exists(insidePath))
        {
            Directory.Delete(insidePath);
    
            // Once the File Directory has deleted successfully, Then Delete the record from Database
            var locationID = 0;
    
            switch (location)
            {
                case "Store1":
                    locationID = 1;
                    break;
    
                case "Store1":
                    locationID = 2;
                    break;
    
                case "Store1":
                    locationID = 3;
                    break;
            }
    
            string cs = ConfigurationManager.ConnectionStrings["webConfigConnectionString"].ConnectionString;
    
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
    
                cmd.CommandText = "DELETE Folder WHERE StoreID = @Storeid AND FolderName = @FolderName)";
    
                cmd.Parameters.AddWithValue("@Storeid", 1);  // For testing I directly give the value for parameters
                cmd.Parameters.AddWithValue("@FolderName", "Sales");
    
                con.Open();
                cmd.ExecuteNonQuery();
            }
        }
    
        JavaScriptSerializer js = new JavaScriptSerializer();
        Context.Response.Write(js.Serialize("Successfully deleted"));
    }
    

    错误消息:

    内部服务器错误

    注意

    删除成功 . 但在数据库中,记录不会被删除

    1 回复  |  直到 6 年前
        1
  •  2
  •   Milad Aghamohammadi    6 年前

    换条线

    cmd.CommandText = "DELETE Folder WHERE StoreID = @Storeid AND FolderName = @FolderName)";
    

    cmd.CommandText = "DELETE from Folder WHERE StoreID = @Storeid AND FolderName = @FolderName";