我有一个ASP.NET(4.5)网站。使图像路径唯一
下面的这个按钮命令插入雇员名,employeelastname,employeephoto到图像文件夹和imagepath到db。
我想让图像路径自动uniquename +扩展;
string filepath = //"uniquename" + ext;
,这样,雇员是独一无二的,所以没有混淆或重复的照片。
我该如何做到这一点?
谢谢。
protected void Button1_Click(object sender, EventArgs e) //Insert
{
if (FileUploadControl.HasFile)
{
string ext = Path.GetExtension(FileUploadControl.FileName);
if (ext == ".jpg" || ext == ".png" || ext == ".jpeg" || ext == ".gif")
{
try
{
cnn.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Employees (EmployeeFirstName, EmployeeLastName, EmployeePhotoPath) VALUES (@item1,@item2,@img)", cnn);
cmd.Parameters.AddWithValue("@item1", TextBox1.Text);
cmd.Parameters.AddWithValue("@item2", TextBox2.Text);
string filepath = //"uniquename" + ext;
FileUploadControl.SaveAs(Server.MapPath("Images/") + filepath);
cmd.Parameters.AddWithValue("@img", filepath);
cmd.ExecuteNonQuery();
cnn.Close();
Label3.Text = "successfully inserted";
}
catch (Exception ex)
{
Label3.Text = ex.Message;
}
temizle();
}
else
{
Label3.Text = "selected file is not a photo";
}
}
else
{
Label3.Text = "please upload employee photo";
}
}
}
您只需先插入员工,获取ID然后使用它 - 然后使用位置更新该行。 –
如果EmployeeID是标识列,那么在执行插入操作之前您无法捕获该值 – Steve
那么您可以使用确切的日期时间和用户姓氏 - 它永远不会相同。除非你同时添加1000个用户? –