1

如何獲取在策略表中創建的最後一個id並將其存儲到一個變量中,以便可以將其用於名爲backupspec表的另一個表。從SQL Server數據庫表中檢索最後一個id

System.Data.SqlClient.SqlConnection dataConnection = new SqlConnection(); 
      dataConnection.ConnectionString = 
       @"Data Source=JAGMIT-PC\SQLEXPRESS;Initial Catalog=SumooHAgentDB;Integrated Security=True"; 

      System.Data.SqlClient.SqlCommand dataCommand = new SqlCommand(); 
      dataCommand.Connection = dataConnection; 

      //tell the compiler and database that we're using parameters (thus the @first, @last, @nick) 
      dataCommand.CommandText = ("Insert Policies (PolicyName, PolicyDesc, TimeAdded,OSFlag, CreateVSSSnapshot, CreateAuditLogForRecoveries, AllowUsersToOverwriteFiles, AutoHandleEnvErrors, NotifyOnEnvErrorCount, NotifyOnFileFailure, NotifyOnFileFailureCount, NotifyOnLackOfPCContact, NotifyOnLackOfPCContactDays, NotifyOnRecoveryFailures, NotifyOnRecoveryFailureReason) values (@pn,@pd,@TimeAdded,@os,@vss,@al,@uow,@hee,@oeec,@off,@offc,@oloc,@olocd,@orf,@orfr)"); 




     dataCommand.Parameters.AddWithValue("@pn",pn); 
     dataCommand.Parameters.AddWithValue("@pd",pd); 
     dataCommand.Parameters.AddWithValue("@TimeAdded",TimeAdded); 
     dataCommand.Parameters.AddWithValue("@os",os); 
     dataCommand.Parameters.AddWithValue("@vss",vss); 
     dataCommand.Parameters.AddWithValue("@al",al); 
     dataCommand.Parameters.AddWithValue("@uow",uow); 
     dataCommand.Parameters.AddWithValue("@hee",hee); 
     dataCommand.Parameters.AddWithValue("@oeec",oeec); 
     dataCommand.Parameters.AddWithValue("@off",off); 
     dataCommand.Parameters.AddWithValue("@offc",offc); 
     dataCommand.Parameters.AddWithValue("@oloc",oloc); 
     dataCommand.Parameters.AddWithValue("@olocd",olocd); 
     dataCommand.Parameters.AddWithValue("@orf",orf); 
     dataCommand.Parameters.AddWithValue("@orfr",orfr); 

     dataConnection.Open(); 

     dataCommand.ExecuteNonquery(); 


     dataConnection.Close(); 


     ArrayList jaja = (ArrayList)Session["BackupSpecList"]; 


     for (int i = 0; i < jaja.Count; i++) 
     { 
      BackupSpecEntry bsp = (BackupSpecEntry)jaja[i]; 
      string path = bsp.path; 
      string inclExcl = bsp.inclExcl; 
      byte inclExclFlags = bsp.inclExclFlags; 
      bool indexContents = bsp.indexContents; 
      int serverBackupSpecId = bsp.serverBackupSpecId; 
      int freq = bsp.freq; 
      int retention = bsp.retention; 
      int policyID =DONT KNOW HOW TO GET THIS VALUE; 
      long specChangeTime = 0; 
      long backupTime = 0; 

      dataCommand.CommandText = ("Insert BackupSpec (PolicyID, Path, ServerBackupSpecID, Freq, Retention, InclExclFlags, InclExcl, IndexContents, SpecChangeTime, BackupTime) values (@policyID,@path,@serverBackupSpecId,@freq,@retention,@inclExclFlags,@inclExcl,@indexContents,@specChangeTime,@backupTime)"); 

      dataCommand.Parameters.AddWithValue("@policyID", policyID); 
      dataCommand.Parameters.AddWithValue("@path", path); 
      dataCommand.Parameters.AddWithValue("@serverBackupSpecId", serverBackupSpecId); 
      dataCommand.Parameters.AddWithValue("@freq", freq); 
      dataCommand.Parameters.AddWithValue("@retention", retention); 
      dataCommand.Parameters.AddWithValue("@inclExclFlags", inclExclFlags); 
      dataCommand.Parameters.AddWithValue("@inclExcl", inclExcl); 
      dataCommand.Parameters.AddWithValue("@indexContents", indexContents); 
      dataCommand.Parameters.AddWithValue("@specChangeTime", specChangeTime); 
      dataCommand.Parameters.AddWithValue("@backupTime", backupTime); 


      dataConnection.Open(); 
      dataCommand.ExecuteNonQuery(); 
      dataConnection.Close(); 

     } 

我收到錯誤帶有標記ID ...

能約1分幫助我這個..?

我沒有得到插入請幫後創建的最後一個策略ID ... 請幫助

+0

您是否首先創建了一個'INSERT INTO Policies'來創建新的「lastid」值?無法在您的代碼中看到.... – 2009-10-12 05:53:53

回答

1

您可以使用SCOPE_IDENTITY@@ IDENTITY

SCOPE_IDENTITY:

strSQL = "INSERT INTO Policies (...) VALUES (@vals....);SELECT SCOPE_IDENTITY()"; 
SQLCommand.CommandText = strSQL; 
IdReturned = SQLCommand.ExecuteScalar(); 

@@ IDENTITY:

strSQL = "INSERT INTO Policies (...) VALUES (@vals....);SELECT @@Identity"; 
SQLCommand.CommandText = strSQL; 
IdReturned = SQLCommand.ExecuteScalar(); 

對於這兩個建議閱讀本article

+2

不,請使用Scope_identity()代替。 – erikkallen 2009-10-11 22:52:51

+0

這是沒有得到執行...你可以告訴我如何把ID值在一個變量..然後我沒有任何辮子.. – user175084 2009-10-11 22:56:02

+0

@ erikkallen ---你可以告訴我如何使用Scope_identity()並把值變成一個變量.. ?? – user175084 2009-10-11 22:57:07

7

使用SCOPE_IDENTITY之間的差異:

strSQL = "INSERT INTO Policies (...) VALUES (@vals....);SELECT @result = scope_identity()" 
SQLCommand.CommandText = strSQL; 
SQLCommand.Parameters.Add("@result", SqlDbType.Int); 
SQLCommand.ExecuteScalar(); 
int id = SQLCommand.Parameters["@result"].Value; 
+0

+1,你的回答比我的更好。不知道這個功能 – Amirshk 2009-10-11 23:00:10

+0

這是不是與我的代碼上面的工作我已經嘗試所有possibe方式...請幫助 – user175084 2009-10-12 02:40:25

0

如果你做了INSERT INTO Policies()調用首先,爲了獲得lastid,你可以做這樣的事情:

int lastId = 0; 
using(SqlConnection Connection = new SqlConnection("(connection string)")) 
{ 
    string queryStatement = 
    "INSERT INTO dbo.Policies(fields) OUTPUT Inserted.LastID VALUES(....)"; 

    using(SqlCommand Command = new SqlCommand(queryStatement, Connection)) 
    { 
    Connection.Open(); 
    lastId = Command.ExecuteScalar(); 
    Connection.Close(); 
    } 
} 

使用OUTPUT .......子句返回新插入的lastId

然後繼續並在主要查詢中使用該值。

Marc

+0

只是一個問題,最後的ID只會是我輸入的最後一個PolicyID或最後插入的所有值。 ..至於它是如何獲得政策ID形式它創建..我已經修改我的代碼請看看..謝謝 – user175084 2009-10-12 14:10:55

相關問題