我的IF声明有什么问题?

时间:2010-10-07 09:47:26

标签: c# sql if-statement sqldatasource

我正在创建一个审核表,我完成了简单的插入和删除审核方法。我有点卡在Update方法上 - 我需要能够获取数据库中的当前值,查询参数中的新值,并比较两者,以便我可以将旧值和更改的值输入到表中在数据库中。

这是我的代码:

    protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
    {
        string[] fields = null;
        string fieldsstring = null;
        string fieldID = e.Command.Parameters[5].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 queryStringupdatecheck = "SELECT VAXCode, Reference, CostCentre, Department, ReportingCategory FROM NominalCode WHERE ID = @ID";
        string queryString = "INSERT INTO Audit (source, action, itemID, item, userid, timestamp) VALUES (@source, @action, @itemID, @item, @userid, @timestamp)";
        using (SqlConnection connection = new SqlConnection("con string = deleted for privacy"))
        {
            SqlCommand commandCheck = new SqlCommand(queryStringupdatecheck, connection);
            commandCheck.Parameters.AddWithValue("@ID", fieldID);
            connection.Open();
            SqlDataReader reader = commandCheck.ExecuteReader();
            while (reader.Read())
            {
                for (int i = 0; i < reader.FieldCount - 1; i++)
                {
                    if (reader[i].ToString() != e.Command.Parameters[i].Value.ToString())
                    {
                        fields[i] = e.Command.Parameters[i].Value.ToString() + "Old value: " + reader[i].ToString();
                    }
                    else
                    {

                    }
                }
            }
                fieldsstring = String.Join(",", fields);

                reader.Close();

            SqlCommand command = new SqlCommand(queryString, connection);
            command.Parameters.AddWithValue("@source", "Nominal");
            command.Parameters.AddWithValue("@action", "Update");
            command.Parameters.AddWithValue("@itemID", fieldID);
            command.Parameters.AddWithValue("@item", fieldsstring);
            command.Parameters.AddWithValue("@userid", name);
            command.Parameters.AddWithValue("@timestamp", DateTime.Now);
            try
            {
                command.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                Response.Write(x);
            }
            finally
            {
                connection.Close();
            }
        }
    }

我遇到的问题是fields []数组总是为空。即使VS调试窗口显示e.Command.Parameter.Value [i]和reader [i]不同,字段变量似乎从未输入过。

由于

3 个答案:

答案 0 :(得分:1)

您永远不会将fields[]设置为null以外的其他内容,因此当您尝试访问它时它为null。您需要先创建数组,然后才能为其赋值。尝试:

           SqlDataReader reader = commandCheck.ExecuteReader();
           fields = new string[reader.FieldCount]

答案 1 :(得分:0)

我真的不明白你在这里做了什么,但是如果你的审计,为什么不把每一个更改都插入你的审计表中以及时间戳呢?

答案 2 :(得分:0)

执行fields = new string[reader.FieldCount]以便分配数组。您正试图写信至null[0]

相关问题