上载新文件fisrt检查此文件是否已存在于数据库中,如果不存在,则将其保存在数据库中

编程入门 行业动态 更新时间:2024-10-26 14:34:09
本文介绍了上载新文件fisrt检查此文件是否已存在于数据库中,如果不存在,则将其保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我试图创建包含的sql数据库 图片ID(int) 图像名称(varchar(50)) 图片(图片) 然后在aspx中,在上传按钮中写入以下代码:

Hello to all, I''m tried to creat sql database that contain Image Id (int) Imagename (varchar(50)) Image (image) and in aspx write in upload button this code:

protected void btnUpload_Click(object sender, EventArgs e) { //Condition to check if the file uploaded or not if (fileuploadImage.HasFile) { //getting length of uploaded file int length = fileuploadImage.PostedFile.ContentLength; //create a byte array to store the binary image data byte[] imgbyte = new byte[length]; //store the currently selected file in memeory HttpPostedFile img = fileuploadImage.PostedFile; //set the binary data img.InputStream.Read(imgbyte, 0, length); string imagename = txtImageName.Text; //use the web.config to store the connection string SqlConnection connection = new SqlConnection(strcon); connection.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO Image (ImageName,Image) VALUES (@imagename,@imagedata)", connection); cmd.Parameters.Add("@imagename", SqlDbType.VarChar, 50).Value = imagename; cmd.Parameters.Add("@imagedata", SqlDbType.Image).Value = imgbyte; int count = cmd.ExecuteNonQuery(); connection.Close(); if (count == 1) { BindGridData(); txtImageName.Text = string.Empty; ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + imagename + " image inserted successfully')", true); } } }

但是我需要在上载新文件时首先检查该文件是否已存在于数据库中,否则不存在将其保存在数据库中 请我该怎么做?

but i need when i''m upload new file fisrt check if this file exist already in database or not then if not exist save that in database please how i can do that ?

推荐答案

当我上传新文件时,首先检查此文件是否已存在于数据库中,否则不存在将其保存在数据库中 您需要对其进行多个查询.您的代码中有两个查询,或者使用存储过程.第一个查询将获取图片计数-如果不是,则为0. 1(如果存在).基于此计数值,可以触发INSERT查询或UPDATE查询. when i''m upload new file fisrt check if this file exist already in database or not then if not exist save that in database You need multiple queries for it. Either have two queries in your code or use a stored procedure. First query would fetch the count of image - 0 if not & 1 if exists. Based on this count value, either trigger an INSERT query or an UPDATE one.

更多推荐

上载新文件fisrt检查此文件是否已存在于数据库中,如果不存在,则将其保存在数据库中

本文发布于:2023-10-19 18:17:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   不存在   则将   新文件   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!