如何从数据库ASP.NET中删除文件 [英] How do I delete files from database ASP.NET

查看:65
本文介绍了如何从数据库ASP.NET中删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!我做了一个公文包,以便用户可以在那里存储文档。我使用一些教程制作了上传和删除方法,没关系,但是现在我必须制作一个删除方法而且我不知道如何。我尝试制作另一个gridview并生成删除方法,但varbinary(max)与sql_variant不兼容。这些是我上传或下载文件的方式。非常感谢!



我的尝试:



< pre lang =cs> protected void Page_Load( object sender,EventArgs e)
{

if (!IsPostBack)
{
BindGrid();
}
}



private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings [ BazadedateConnectionString1]。ConnectionString;
使用(SqlConnection con = new SqlConnection(constr))
{
使用(SqlCommand cmd = new SqlCommand())
{
cmd .CommandText = 从Servieta中选择ServietaID,Nume,其中FKuserID = @ FK;
cmd.Parameters.AddWithValue( @ FK,user.Text);
cmd.Connection = con;
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
}
}


受保护 void 上传( object sender,EventArgs e)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
使用(Stream fs = FileUpload1.PostedFile.InputStream)
{
使用(BinaryReader br = new BinaryReader(fs))
{
byte [] bytes = br.ReadBytes(( Int32 )fs.Length);
string constr = ConfigurationManager.ConnectionStrings [ BazadedateConnectionString1\" ]的ConnectionString。
使用(SqlConnection con = new SqlConnection(constr))
{
string query = 插入Servieta值(@ Nume,@ Continut,@ Data,@ FKuserID);
使用(SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue( @ Nume,filename);
cmd.Parameters.AddWithValue( @ Continut,contentType);
cmd.Parameters.AddWithValue( @ Data,bytes);
cmd.Parameters.AddWithValue( @ FKuserID,Request.Cookies [ 登录] [ 用户ID]的ToString());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}


受保护 void DownloadFile(< span class =code-keyword> object
sender,EventArgs e)
{
// INT ID = int.Parse((发送者作为LinkBut​​ton的).CommandArgument);
<跨度类= 代码关键字> INT ID = Convert.ToInt32(( sender as LinkBut​​ton).CommandArgument);
byte [] bytes;
string fileName,contentType;
string constr = ConfigurationManager.ConnectionStrings [ BazadedateConnectionString1\" ]的ConnectionString。
使用(SqlConnection con = new SqlConnection(constr))
{
使用(SqlCommand cmd = new SqlCommand())
{
cmd .CommandText = 从Servieta中选择Nume,Data,Continut,其中ServietaID = @ id;
cmd.Parameters.AddWithValue( @ ID,id);
cmd.Connection = con;
con.Open();
使用(SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes =( byte [])sdr [ 数据];
contentType = sdr [ Continut]。ToString();
fileName = sdr [ Nume]。ToString();
}





con.Close();
}
}
Response.Clear();
Response.Buffer = true ;
Response.Charset = ;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.AppendHeader( Content-Disposition attachment; filename = + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}

解决方案

我刚解决了这个问题。如果有人感兴趣,这些是代码。感谢您有兴趣帮助我!



 受保护  void  DeleteFile( object  sender,EventArgs e)
{

int id = Convert.ToInt32((sender as LinkBut​​ton).CommandArgument);
string connStr = ConfigurationManager.ConnectionStrings [ BazadedateConnectionString1\" ]的ConnectionString。
string sqlStatement = @ 从Servieta删除ServietaID = @ id;
使用(SqlConnection con = new SqlConnection(connStr))
{
使用(SqlCommand cmd = new SqlCommand())
{
cmd .Connection = con;
cmd.CommandText = sqlStatement;
cmd.CommandType = CommandType.Text;

cmd.Parameters.AddWithValue( @ ID,id);
尝试
{
con.Open();
cmd.ExecuteNonQuery();
BindGrid();
}
catch (SqlException)
{

}
}
}

}


Hello! I made a briefcase so users can store documents there. I made the Upload and Delete methods using some tutorials, it's ok, but now i must made a delete method and I don't know how. I tried to make another gridview and generate delete method but "varbinary(max) is incompatible with sql_variant". These is how I upload or download files. Thank you very much !

What I have tried:

protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindGrid();
            }
        }



        private void BindGrid()
        {
            string constr = ConfigurationManager.ConnectionStrings["BazadedateConnectionString1"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "select ServietaID, Nume from Servieta where FKuserID=@FK";
                    cmd.Parameters.AddWithValue("@FK", user.Text);
                    cmd.Connection = con;
                    con.Open();
                    GridView1.DataSource = cmd.ExecuteReader();
                    GridView1.DataBind();
                    con.Close();
                }
            }
        }


        protected void Upload(object sender, EventArgs e)
        {
            string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
            string contentType = FileUpload1.PostedFile.ContentType;
            using (Stream fs = FileUpload1.PostedFile.InputStream)
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    byte[] bytes = br.ReadBytes((Int32)fs.Length);
                    string constr = ConfigurationManager.ConnectionStrings["BazadedateConnectionString1"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        string query = "insert into Servieta values (@Nume, @Continut, @Data, @FKuserID)";
                        using (SqlCommand cmd = new SqlCommand(query))
                        {
                            cmd.Connection = con;
                            cmd.Parameters.AddWithValue("@Nume", filename);
                            cmd.Parameters.AddWithValue("@Continut", contentType);
                            cmd.Parameters.AddWithValue("@Data", bytes);
                            cmd.Parameters.AddWithValue("@FKuserID", Request.Cookies["Login"]["userID"].ToString());
                            con.Open();
                            cmd.ExecuteNonQuery();
                            con.Close();
                        }
                    }
                }
            }
            Response.Redirect(Request.Url.AbsoluteUri);
        }


            protected void DownloadFile(object sender, EventArgs e)
            {
                //int id = int.Parse((sender as LinkButton).CommandArgument);
                int id = Convert.ToInt32((sender as LinkButton).CommandArgument);
                byte[] bytes;
                string fileName, contentType;
                string constr = ConfigurationManager.ConnectionStrings["BazadedateConnectionString1"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.CommandText = "select Nume, Data, Continut from Servieta where ServietaID=@id";
                        cmd.Parameters.AddWithValue("@ID", id);
                        cmd.Connection = con;
                        con.Open();
                        using (SqlDataReader sdr = cmd.ExecuteReader())
                        {
                            sdr.Read();
                            bytes = (byte[])sdr["Data"];
                            contentType = sdr["Continut"].ToString();
                            fileName = sdr["Nume"].ToString();
                        }





                        con.Close();
                    }
                }
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.ContentType = contentType;
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();
            }

解决方案

I just solved the problem. These is the code if anyone is interested. Thank you for beeing interested to help me!

protected void DeleteFile(object sender, EventArgs e)
    {

        int id = Convert.ToInt32((sender as LinkButton).CommandArgument);
        string connStr = ConfigurationManager.ConnectionStrings["BazadedateConnectionString1"].ConnectionString;
        string sqlStatement = @"Delete from Servieta Where ServietaID=@id";
        using (SqlConnection con = new SqlConnection(connStr))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandText = sqlStatement;
                cmd.CommandType = CommandType.Text;

                cmd.Parameters.AddWithValue("@ID", id);
                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                    BindGrid();
                }
                catch (SqlException)
                {

                }
            }
        }

    }


这篇关于如何从数据库ASP.NET中删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆