2010-08-12 23 views
0

这里是更新记录我的存储过程:存储语句中的问题错误处理?

ALTER 
PROCEDURE [dbo].[sp_UpdatetoShipped] 
(
@Date datetime, 
@SerialNumber 
varchar(50), 
@User 
varchar(50), 
@WorkWeek 
varchar(50) 
) 
AS 
BEGIN 

UPDATE dbo.FG_FILLIN SET Status='SHIPPED',[email protected],[email protected],[email protected] where (Status='KITTED')and [email protected] 

END 

然后,这是我的DAL:

public int UpdatetoShipped(FillinEntity fin) 
{ 
SqlConnection conn = new SqlConnection(connStr); 
conn.Open(); 
SqlCommand cmd = new SqlCommand("sp_UpdatetoShipped", conn); 
cmd.CommandType =CommandType.StoredProcedure; 
try 
{ 
cmd.Parameters.Add("@SerialNumber", SqlDbType.VarChar,50).Value = fin.SerialNumber; 
cmd.Parameters.Add("@WorkWeek", SqlDbType.VarChar, 50).Value = fin.WorkWeek; 
cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Now.ToString(); 
cmd.Parameters.AddWithValue("@User", fin.ModifiedBy); 
return cmd.ExecuteNonQuery(); 
} 
catch 
{ 
throw; 
} 
finally 
{ 
cmd.Dispose(); 
conn.Close(); 
conn.Dispose(); 
} 
} 

我BLL:

public int UpdatetoShipped(FillinEntity fin) 
{ 
DAL pDAL = new DAL(); 
try 
{ 
return pDAL.UpdatetoShipped(fin); 
} 
catch 
{ 
throw; 
} 
finally 
{ 
pDAL = null; 
} 
} 

和我的UI:

string filepath2 = txtPath2.Text; 
Stream stream2 = new FileStream(filepath2, FileMode.Open, FileAccess.Read, FileShare.Read); 
ExcelMapper<FillinEntity> exceltoshipped = new ExcelMapper<FillinEntity>(); 
IExcelParser excelParser2 = new ExcelReaderExcelParser(stream2); 
IExcelRowMapper<FillinEntity> mapper2 = new ShippedRowMapper(); 
IEnumerable<FillinEntity> fillin2 = exceltoshipped.ListAll(excelParser2, mapper2); 
int intResult = 0; 
BAL pBAL = new BAL(); 
try 
{ 
foreach (FillinEntity fin in fillin2) 
{ 
fin.ModifiedBy = loggedUser; 
intResult = pBAL.UpdatetoShipped(fin); 
} 
if (intResult > 0) 
MessageBox.Show("Record Updated Successfully."); 
else 
MessageBox.Show("Record couldn't Updated Check Serial"); 
} 
catch (Exception ee) 
{ 
MessageBox.Show(ee.Message.ToString()); 
} 
finally 
{ 
pBAL =null; 
} 

我的问题是它总是说更新成功。但是,如果我再次更新它作为重复更新我想显示串行已更新。

+2

有很多毛病,仅在SQL你的方法,包括命名的存储过程的约定,以及如何运行更新。存储过程中没有检查该行是否已经更新了该序列号,所以我会重新考虑如何执行此操作。 – 2010-08-12 08:17:23

回答

1

你需要做的关键的变化是下面一行SQL从您的存储过程:

UPDATE dbo.FG_FILLIN 
SET Status='SHIPPED', 
     [email protected], 
     [email protected], 
     [email protected] 
WHERE (Status='KITTED') 
AND [email protected] 

你需要返回一个值,它允许你确定这是否UPDATE已经发生与否,例如:

DECLARE @iUpdateAlreadyComplete INT 
SET @iUpdateAlreadyComplete = 0; 
IF EXISTS 
(
    SELECT 1 
    FROM dbo.FG_FILLIN 
    WHERE Status='SHIPPED' 
    AND [email protected] 
) 
BEGIN 
    SET @iUpdateAlreadyComplete = 1 
END 
ELSE 
BEGIN 
    UPDATE dbo.FG_FILLIN 
    SET Status='SHIPPED', 
      [email protected], 
      [email protected], 
      [email protected] 
    WHERE (Status='KITTED') 
    AND [email protected] 
END 

SELECT @iUpdateAlreadyComplete AS Result 

您可以将DAL然后更改从return cmd.ExecuteNonQuery();到:

var result = Convert.ToInt32(cmd.ExecuteScalar()); 

return result; 

对于已更新的记录,返回值现在为0,对于不需要更新的1,因为它已经处理。

其他注意事项

有一对夫妇,你应该考虑改变其他的东西:

  • sp_UpdatetoShipped是存储过程的名。 Do not use the sp_ prefix
  • 您的DAL故意catch es和re- throw是一个例外(承认采用“最佳”方式),您真的需要吗?
  • 而不是明确地调用Dipose(),而是使用using() {}语法,因为这可以确保调用Dispose(),即使发生异常。

using语法:

using(SqlConnection conn = new SqlConnection(connStr)) 
{ 
    conn.Open(); 
    using (SqlCommand cmd = new SqlCommand("sp_UpdatetoShipped", conn)) 
    { 
    } 
} 
+0

我将在哪里放置返回结果... 在我的dal和ui上。 感谢致 – Crimsonland 2010-08-13 00:22:10

+0

如何更改此DAL谢谢。 – Crimsonland 2010-08-13 00:25:28

+0

@Crimsonland,我已经说过如何在我的答案中改变你的DAL,至于UI,它仍然返回一个int,所以它取决于你对它做什么=) – Rob 2010-08-13 07:56:03

0

这似乎更像是一个业务规则问题,而不是任何与错误有关的事情。你可能想要做的是创建一个字典来保存已更新的序列号。

例如

Dictoinary<string,string> updatedSerialNumbers = new Dictionary<string, string>(); 

foreach (FillinEntity fin in fillin2) 
{ 
fin.ModifiedBy = loggedUser; 
if (updatedSerialNumbers.Contains(fin.SerialNumber) == false) 
{ 
    intResult = pBAL.UpdatetoShipped(fin); 
    updatedSerialNumbers.Add(fin.SerialNumber,fin.SerialNumber); 
} 

这样的事情应该解决你的问题。