上载新文件fisrt检查此文件是否已存在于数据库中,如果不存在,则将其保存在数据库中 [英] upload new file fisrt check if this file exist already in database or not then if not exist save that in database

查看:47
本文介绍了上载新文件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检查此文件是否已存在于数据库中,如果不存在,则将其保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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