I'm trying to create sql database that contains
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);
}
}
}
When I'm uploading a new image I need to first check if this image already exists in database and if it doesn't exist save that in database.
Please how I can do that?
4条答案
按热度按时间ar5n3qh51#
Add a method that is responsible for checking if the filename already exists in the table.
Then I would call this like so
oxf4rvwz2#
If you want to verify imagedata, You can try to use
DATALENGTH
as first line of check for the two images. If theDATALENGTH
is different, then you suppose to have a "different" picture".You can also use
SUBSTRING(Image, 1, 8000)
to check first 8000 bytes.And also
SUBSTRING(Image, DATALENGTH(Image) - 7999, 8000)
to check last 8000 bytes.06odsfpq3#
One of the fastest ways is to do an UPDATE and then INSERT if update returns no updates.
Or if query on Image itself:
tjvv9vkg4#
how do i solve this in ssis
First check Filechapter table whether the same file name exists or not. If yes then delete the corresponding records from employee & file configuration table. After that insert new log into filechapter table with status as 'InProgress' and then load the data from new file into table 2. Load only valid records into destination table. Move all error records into a CSV. 3. Implement the SCD type 1 (Insert/ Update) 4. Once the execution is completed then update the filestatus like below. A. If execution is successful then populate the value as 'Success' B. If execution Fails then populate the value as 'Failed'.