代码之家  ›  专栏  ›  技术社区  ›  Doug

若数据库中存在记录,则更新单个列

  •  0
  • Doug  · 技术社区  · 16 年前

    我有一个批量上传对象,用于一次批量上传大约25-40个图像文件。每个图像的大小约为100-150KB。

    这是我的c#代码:

    protected void graphicMultiFileButton_Click(object sender, EventArgs e)
    
     {
    
     //graphicMultiFile is the ID of the bulk uploading object ( provided by Dean Brettle: http://www.brettle.com/neatupload  )
    
       if (graphicMultiFile.Files.Length > 0)
    
              {
               foreach (UploadedFile file in graphicMultiFile.Files)
                 {
                     //strip ".jpg" from file name (will be assigned as SKU)
                      string sku = file.FileName.Substring(0, file.FileName.Length - 4);
    
                     //assign the directory where the images will be stored on the server
                     string directoryPath = Server.MapPath("~/images/graphicsLib/" + file.FileName);
    
                     //ensure that if image existes on server that it will get overwritten next time it's uploaded:
                     file.MoveTo(directoryPath, MoveToOptions.Overwrite);
    
                     //current sql that inserts a record to the db
                    SqlCommand comm;
                    SqlConnection conn;
                    string connectionString = ConfigurationManager.ConnectionStrings["DataConnect"].ConnectionString;
                    conn = new SqlConnection(connectionString);
                     comm = new SqlCommand("INSERT INTO GraphicsLibrary (sku, imagePath, DateUpdated) VALUES (@sku, @imagePath, @DateUpdated)", conn);
    
                    comm.Parameters.Add("@sku", System.Data.SqlDbType.VarChar, 50);
                    comm.Parameters["@sku"].Value = sku;
    
                   comm.Parameters.Add("@imagePath", System.Data.SqlDbType.VarChar, 300);
                   comm.Parameters["@imagePath"].Value = "images/graphicsLib/" + file.FileName;
    
                   comm.Parameters.Add("@DateUpdated", System.Data.SqlDbType.DateTime);
                  comm.Parameters["@DateUpdated"].Value = DateTime.Now;
    
                 conn.Open();
                 comm.ExecuteNonQuery();
                 conn.Close();
    
    
               }
             }
          }
    

    上传图像后,管理员将返回并重新上传以前上传的图像。

    这是因为这些产品图像总是在被修改和改进。

    对于每个新的/改进的图像 文件名和扩展名 将保持不变-以便在图像321-54321.jpg首次上载到服务器时,该图像的新/改进版本仍将具有图像文件名321-54321.jpg。

    我不能确定文件大小是否会保持在100-150KB的范围内。我假设图像文件的大小最终会增加。

    当图像上传(再次)时,数据库中当然会有该图像的现有记录。 最好的方法是什么:

    1. 检查数据库中的现有记录(存储过程或SqlDataReader或创建数据集…?)
    2. 然后,如果记录存在,只需更新该记录,使DateUpdated列获得今天的日期。
    3. 如果不存在记录,则按正常方式插入该记录。

    需要考虑的事项:

    我们在托管环境(DiscountAsp)上使用SQLServer2000。

    虽然我是一名jr开发人员,但我猜存储过程将是一种发展方向。似乎更有效-从for each循环中进行此记录检查。。。但不确定。我需要额外的帮助来编写存储过程,因为我没有太多的经验。

    谢谢大家。。。

    2 回复  |  直到 16 年前
        1
  •  0
  •   Doug    16 年前

    好的,我在回答我自己的问题。使用StoredProcedure如下:(我已经测试过,目前正在按需要工作…)

    更改过程dbo.addOrUpdateImageRecord

    (
    @addToZip bit,
    @sku varchar(50),
    @imagePath varchar(300),
    @DateCreated DateTime, 
    @DateUpdated DateTime
    )
    

    开始 从GraphicsLibrary中选择sku 其中sku=@sku 终止

    如果(@@RowCount=0)

    开始 值(@addToZip、@sku、@imagePath、@DateCreated) 终止

    更新图形库 其中sku=@sku

    /* SET NOCOUNT ON */
    RETURN
    
        2
  •  0
  •   Carl Rippon    16 年前

    在托管代码中(而不是在存储过程中)循环会更快。我将在以下存储过程中使用以下代码:

    if (graphicMultiFile.Files.Length > 0)          
            {       
                string connectionString = ConfigurationManager.ConnectionStrings["DataConnect"].ConnectionString;                
                foreach (UploadedFile file in graphicMultiFile.Files)             
                {                 
                    string sku = file.FileName.Substring(0, file.FileName.Length - 4);                 
                    string directoryPath = Server.MapPath("~/images/graphicsLib/" + file.FileName);                 
                    file.MoveTo(directoryPath, MoveToOptions.Overwrite);                 
    
                    SqlConnection conn = new SqlConnection(connectionString);       
                    SqlCommand comm = new SqlCommand("exec addOrUpdateImageRecord @sku, @imagePath");                
                    comm.Parameters.Add("@sku", System.Data.SqlDbType.VarChar, 50);                
                    comm.Parameters["@sku"].Value = sku;               
                    comm.Parameters.Add("@imagePath", System.Data.SqlDbType.VarChar, 300);               
                    comm.Parameters["@imagePath"].Value = "images/graphicsLib/" + file.FileName;               
                    conn.Open();             
                    comm.ExecuteNonQuery();             
                    conn.Close();           
                }         
            }      
    

    CREATE PROCEDURE dbo.addOrUpdateImageRecord(
            @sku varchar(50),
            @imagePath varchar(300))
    
    AS
    
        DECLARE @ExistenceCheck int
        SELECT @ExistenceCheck = COUNT(*)
        FROM GraphicsLibrary 
        WHERE sku=@sku 
    
        IF(@ExistenceCheck=0)
        BEGIN 
            INSERT INTO GraphicsLibrary (sku, imagePath, DateCreated) 
            VALUES(@sku, @imagePath, GETDATE()) 
        END
        ELSE
        BEGIN
            UPDATE GraphicsLibrary 
            SET DateUpdated = GETDATE() 
            WHERE sku = @sku
        END
    
    
    GO