如何使用GUID在c#中添加记录

本文关键字:添加 加记录 何使用 GUID | 更新日期: 2023-09-27 18:22:25

我正在使用c#和asp.net创建学生记录。为此,我添加了5个字段-名称、类、节、地址和图像。

插入student1.jpg时,效果良好。当我插入具有不同图像的相同名称(student1.jpg)的第二条记录时,出现问题。插入失败,出现一些错误,如"图像已存在"。

在我了解GUID的用途后,我想将这些用于我的工作。

有可能这样做吗?如果有,有人能帮我吗?

这是我的完整代码隐藏文件:

protected void btnsub_Click(object sender, EventArgs e)
        {
            SqlConnection con = Connection.DBconnection();
            if (Textid.Text.Trim().Length > 0)
            {
                SqlCommand com = new SqlCommand("StoredProcedure3", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@id", Textid.Text.Trim());
                com.Parameters.AddWithValue("@Name", Textusername.Text.Trim());
                com.Parameters.AddWithValue("@Class", Textclass.Text.Trim());
                com.Parameters.AddWithValue("@Section", Textsection.Text.Trim());
                com.Parameters.AddWithValue("@Address", Textaddress.Text.Trim());
                try
                {
                    string filename = Image1.ImageUrl.Substring(Image1.ImageUrl.IndexOf('/')+1);
                    if (fileupload.PostedFile.FileName.Length > 0)
                    {      
                        filename = Path.GetFileName(fileupload.PostedFile.FileName);                        
                        fileupload.SaveAs(Server.MapPath("~/Images/" + filename));
                    }                  
                    com.Parameters.AddWithValue("@Image",(filename.Length>0)? "Images/" + filename:string.Empty);
                    com.ExecuteNonQuery();                    
                }
                catch (Exception ex)
                {
                    btnsub.Text = ex.Message;
                }
                    Response.Redirect("studententry.aspx");
            }
            else
            {
                SqlCommand com = new SqlCommand("StoredProcedure1", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@Name", Textusername.Text.Trim());
                com.Parameters.AddWithValue("@Class", Textclass.Text.Trim());
                com.Parameters.AddWithValue("@Section", Textsection.Text.Trim());
                com.Parameters.AddWithValue("@Address", Textaddress.Text.Trim());
                try
                {
                    string filename = string.Empty;
                    if (fileupload.PostedFile.FileName.Length > 0)
                    {
                        filename = Path.GetFileName(fileupload.PostedFile.FileName);
                        if (File.Exists(Server.MapPath("~/Images/" + filename)))
                        {
                            Label6.Visible = true;
                            return;
                        }
                        fileupload.SaveAs(Server.MapPath("~/Images/" + filename));
                    }
                        com.Parameters.AddWithValue("@Image",(filename.Length>0)? "Images/" + filename:string.Empty);
                        com.ExecuteNonQuery();                    
                }
                catch (Exception ex)
                {
                    btnsub.Text = ex.Message;
                }
                    Response.Redirect("studententry.aspx");
            }
        }           

谢谢。

如何使用GUID在c#中添加记录

命名空间:使用System.IO;

对于存储产品3:

string filename = Image1.ImageUrl.Substring(Image1.ImageUrl.IndexOf('/')+1);
if (fileupload.PostedFile.FileName.Length > 0)
{      
    filename = Path.GetFileName(fileupload.PostedFile.FileName); 
    string fileExtension = Path.GetExtension(filename).ToLower();  // this will give you file extension.
    string uniqueFileName = Guid.NewGuid().ToString() + fileExtension;   // this will give you unique filename.
    fileupload.SaveAs(Server.MapPath("~/Images/" + uniqueFileName)); // save the image with guid name.
    com.Parameters.AddWithValue("@Image",(filename.Length > 0) ? "Images/" + uniqueFileName : string.Empty); // now you can save the new filename in database for associated record.
} 

对于存储产品1:

string filename = string.Empty;
if (fileupload.PostedFile.FileName.Length > 0)
{
    filename = Path.GetFileName(fileupload.PostedFile.FileName);
    if (File.Exists(Server.MapPath("~/Images/" + filename)))
    {
        Label6.Visible = true;
        return;
    }
    string fileExtension = Path.GetExtension(filename).ToLower();  // this will give you file extension.
    string uniqueFileName = Guid.NewGuid().ToString() + fileExtension;   // this will give you unique filename.
    fileupload.SaveAs(Server.MapPath("~/Images/" + uniqueFileName)); // save the image with guid name.
}
com.Parameters.AddWithValue("@Image",(filename.Length > 0)? "Images/" + uniqueFileName : string.Empty);
com.ExecuteNonQuery();

现在,即使原始文件名相同但图像不同,所有将保存到驱动器的上传图像都将具有唯一的名称。

假设Id是表上的主键,那么只需替换

com.Parameters.AddWithValue("@id", Textid.Text.Trim());

com.Parameters.AddWithValue("@id", System.Guid.NewGuid().ToString());

如果这不能解决您的问题,如果您提供表的结构或存储过程中的代码,我可以提供进一步的帮助。

--编辑--随着您的编辑,我的答案不再有意义,因为我们现在知道varchar作为id是不可接受的,并且错误是在文件上传时抛出的。SaveAs()。

Suprabat Biswal是在正确的轨道上,因为你应该使用GUID来更改你的文件名。

下面是一些更新的代码,注意我将id改回了Textid.Text.Trim()。

protected void btnsub_Click(object sender, EventArgs e)
{
    SqlConnection con = Connection.DBconnection();
    if (Textid.Text.Trim().Length > 0)
    {
        SqlCommand com = new SqlCommand("StoredProcedure3", con);
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.AddWithValue("@id", Textid.Text.Trim());
        com.Parameters.AddWithValue("@Name", Textusername.Text.Trim());
        com.Parameters.AddWithValue("@Class", Textclass.Text.Trim());
        com.Parameters.AddWithValue("@Section", Textsection.Text.Trim());
        com.Parameters.AddWithValue("@Address", Textaddress.Text.Trim());
        try
        {
            var filename = string.Format("{0}.{1}", Guid.NewGuid().ToString(), Path.GetExtension(fileupload.PostedFile.FileName).ToLower());
            var full_path = Server.MapPath("~/Images/", filename);
            if (fileupload.PostedFile.FileName.Length > 0)
            {                        ;
                fileupload.SaveAs(full_path);
            }
            com.Parameters.AddWithValue("@Image", (filename.Length > 0) ? "Images/" + filename : string.Empty);
            com.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            btnsub.Text = ex.Message;
        }
        Response.Redirect("studententry.aspx");
    }
    else
    {
        SqlCommand com = new SqlCommand("StoredProcedure1", con);
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.AddWithValue("@Name", Textusername.Text.Trim());
        com.Parameters.AddWithValue("@Class", Textclass.Text.Trim());
        com.Parameters.AddWithValue("@Section", Textsection.Text.Trim());
        com.Parameters.AddWithValue("@Address", Textaddress.Text.Trim());
        try
        {
            string filename = string.Empty;
            if (fileupload.PostedFile.FileName.Length > 0)
            {
                var filename = string.Format("{0}.{1}", Guid.NewGuid().ToString(), Path.GetExtension(fileupload.PostedFile.FileName).ToLower());
                var full_path = Server.MapPath("~/Images/", filename);                        
                // This IF statement should never be hit since we're using GUID for the file name.
                if (File.Exists(full_path))
                {
                    Label6.Visible = true;                            
                    return;
                }
                fileupload.SaveAs(full_path);
            }
            com.Parameters.AddWithValue("@Image", (filename.Length > 0) ? "Images/" + filename : string.Empty);
            com.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            btnsub.Text = ex.Message;
        }
        Response.Redirect("studententry.aspx");
    }
}

如果我理解你的问题:

这些更改对您的原始代码来说已经足够使用GUID了。

  1. 在数据库表"Student"中将列Id从"int"更改为"NVARCHAR(36)"

  2. 通过"Id"参数值后的表单代码为

    string ID=Guid.NewGuid().ToString();

    com.Parameters.AddWithValue("@id",id);

  3. 您可以将图像名称保留为原始名称。

或------

U可以直接在数据库中生成GUID,也可以如下

ALTER PROCEDURE StoredProcedure3
(    
   @Name Varchar (100),
   @Class varchar (50),
   @Section Varchar (50),
   @Address Varchar (50),
   @Image Varchar (50)
)
AS
begin
   DECLARE  @id NVARCHAR(36);
   SET @id = NEWID();
   Update Student set Name = @Name, Class= @Class, Section= @Section,  Address = @Address, Image = @Image where id=@id
End