0

テーブルへの挿入、更新、削除などのすべての変更を監査する必要があり、自動的に生成された INSERT/UPDATE/DELETE ステートメントで単純な SQLDataSource と GridView を使用しています。問題は、行が更新されたときに ID (主キー) を取得し、それを監査テーブルに入れたいということですが、UPDATE クエリの唯一のパラメーターは実際のデータであり、主キーではないため、わかりません。この情報にアクセスする方法。これが私のUPDATEクエリです:

InsertCommand="INSERT INTO [NominalCode] ([VAXCode], [Reference], [CostCentre], [Department], [ReportingCategory]) VALUES (@VAXCode, @Reference, @CostCentre, @Department, @ReportingCategory)" 

そして、監査のためのコードビハインドは次のとおりです。

protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
    {
        string fields = e.Command.Parameters[0].Value.ToString() + "," + e.Command.Parameters[1].Value.ToString() + "," + e.Command.Parameters[2].Value.ToString() + "," + e.Command.Parameters[3].Value.ToString() + "," + e.Command.Parameters[4].Value.ToString();
        System.Security.Principal.  WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;
        string[] namearray = p.Identity.Name.Split('\\');
        string name = namearray[1];
        string queryString = "INSERT INTO Audit (source, action, item, userid, timestamp) VALUES (@source, @action, @item, @userid, @timestamp)";
        using (SqlConnection connection = new SqlConnection("con string removed for privacy"))
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            command.Parameters.AddWithValue("@source", "Nominal");
            command.Parameters.AddWithValue("@action", "Update");
            command.Parameters.AddWithValue("@item", fields);
            command.Parameters.AddWithValue("@userid", name);
            command.Parameters.AddWithValue("@timestamp", DateTime.Now);
            connection.Open();
            try
            {
                command.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                Response.Write(x);
            }
            finally
            {
                connection.Close();
            }
        }
    }
4

1 に答える 1

0

わかりました、私はばかだと思います。もちろん、ID は SQL クエリの WHERE 句にありました。そして、何らかの理由でINSERTコマンドを貼り付けました。

于 2010-10-07T08:50:08.123 に答える