0
我知道,這似乎是一百萬次問的問題,但我再次問,因爲,早期的答案不能解決我的問題。即使命令在SQL Server 2008中成功執行,數據仍未保存到數據庫中?
我有這個存儲過程:
ALTER PROCEDURE [dbo].[My_Save_Command_SP]
@Param1 varchar (50),
@Param2 varchar (500),
@Param3 varchar(500),
@Param4 varchar (500),
@Param5 varchar (500),
@Param6 varchar (500)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @ParamID BIGINT
SET @ParamID = 0
SET @ParamID = (SELECT ISNULL(ParamID,0) AS ParamID FROM t_MyTable WHERE Param1 = @Param1)
IF @ParamID > 0
BEGIN
UPDATE t_MyTable
SET Param2 = @Param2, Param3 = @Param3, Param4 = @Param4,
Param5 = @Param5, Param6 = @Param6
WHERE Param1 = @Param1
END
ELSE
BEGIN
SET @ParamID = (select ISNULL(MAX(ParamID),0) AS ParamID from t_MyTable)
SET @ParamID = @ParamID + 1
INSERT INTO t_MyTable (ParamID, Param1, Param2, Param3, Param4, Param5, Param6)
VALUES (@ParamID, @Param1, @Param2, @Param3, @Param4, @Param5, @Param6)
END
END
及以下的後端代碼 -
Boolean bResult = false;
Int32 nCounter = 0;
Int32 nAffectedRow = -1;
using (SqlConnection connection = DBAccessDA.CreateConnection())
{
SqlCommand command = DBAccessDA.CreateCommand(connection);
SqlTransaction transaction = connection.BeginTransaction();
DBAccessDA.CreateStoredprocedure(command, "My_Save_Command_SP");
for (int i = 0; i < pList.Count; i++)
{
nAffectedRow = -1;
command.Parameters.Clear();
DBAccessDA.AddInParameter(command, "@Param1", SqlDbType.VarChar, pList[i].Param1);
DBAccessDA.AddInParameter(command, "@Param2", SqlDbType.VarChar, pList[i].Param2);
DBAccessDA.AddInParameter(command, "@Param3", SqlDbType.VarChar, pList[i].Param3);
DBAccessDA.AddInParameter(command, "@Param4", SqlDbType.VarChar, pList[i].Param4);
DBAccessDA.AddInParameter(command, "@Param5", SqlDbType.VarChar, pList[i].Param5);
DBAccessDA.AddInParameter(command, "@Param6", SqlDbType.VarChar, pList[i].Param6);
// This line returns -1
nAffectedRow = DBAccessDA.ExecuteNonQuery(command, transaction);
if (nAffectedRow > 0)
{
nCounter++;
}
}
if (nCounter == pList.Count)
{
transaction.Commit();
bResult = true;
}
else
{
transaction.Rollback();
bResult = false;
}
}
如果我從帶有相同參數的SQL查詢窗口中的存儲過程,它在插入數據,但不是來自代碼。請幫忙。謝謝。
請問您可以告訴DBAccessDA是什麼? – heq
這很難說,但乍一看似乎你沒有通過參數大小。您可以使用'SQL Profiler'來獲取您的命令在服務器上執行的語句。 –
@heq,DBAccessDA是一個提供數據庫連接,命令,在命令中添加參數等的類。簡而言之,它用於消除編寫類似數據庫相關代碼的需要。 –