use*_*084 1 c# asp.net sql-server-2005 visual-studio-2008 scope-identity
如何获取策略表中创建的最后一个ID并将其存储到变量中,以便我可以将其用于另一个名为backupspec table的表.
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();
}
Run Code Online (Sandbox Code Playgroud)
我的标签ID出错...
可以帮助我一些... .. ??
插入后我没有得到最后一个policyID请帮助...请帮助
使用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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3432 次 |
| 最近记录: |