首先,请帮帮我!我再也无法忍受了。我找不到错误所在的位置。这是我的问题:

我正在尝试通过c#winform应用程序更新一行。从应用程序生成的更新查询的格式正确。我在sql服务器环境中对其进行了测试,效果很好。当我从应用程序运行它时,我得到0行更新。

这是使用反射生成更新语句的代码段-不要试图弄清楚。在代码部分之后继续阅读:

        public void Update(int cusID)
        {
            SqlCommand objSqlCommand = new SqlCommand();
            Customer cust = new Customer();

            string SQL = null;

            try
            {
                if ((cusID != 0))
                {
                        foreach (PropertyInfo PropertyItem in this.GetType().GetProperties())
                        {
                            if (!(PropertyItem.Name.ToString() == cust.PKName))
                            {
                                if (PropertyItem.Name.ToString() != "TableName")
                                {
                                    if (SQL == null)
                                    {
                                        SQL = PropertyItem.Name.ToString() + " = @" + PropertyItem.Name.ToString();
                                    }
                                    else
                                    {
                                        SQL = SQL + ", " + PropertyItem.Name.ToString() + " = @" + PropertyItem.Name.ToString();
                                    }
                                 }
                                 else
                                 {
                                    break;
                                 }
                            }
                       }

                        objSqlCommand.CommandText = "UPDATE " + this.TableName + " SET " + SQL + " WHERE " + cust.PKName + " = @cusID AND PhoneNumber = " + "'" + "@phNum" + "'";

                        foreach (PropertyInfo PropertyItem in this.GetType().GetProperties())
                        {
                            if (!(PropertyItem.Name.ToString() == cust.PKName))
                            {
                                if (PropertyItem.Name.ToString() != "TableName")
                                {
                                    objSqlCommand.Parameters.AddWithValue("@" + PropertyItem.Name.ToString(), PropertyItem.GetValue(this, null));
                                }
                                else
                                {
                                    break;
                                }

                            }
                        }

                        objSqlCommand.Parameters.AddWithValue("@cusID", cusID);
                        objSqlCommand.Parameters.AddWithValue("@phNum", this.PhoneNumber);
                        DAL.ExecuteSQL(objSqlCommand);
                }
                else
                {
                    //AppEventLog.AddWarning("Primary Key is not provided for Update.")
                }

            }
            catch (Exception ex)
            {
                //AppEventLog.AddError(ex.Message.ToString)
            }
        }


以下部分:

objSqlCommand.CommandText = "UPDATE " + this.TableName + " SET " + SQL + " WHERE " + cust.PKName + " = @cusID AND PhoneNumber = " + "'" + "@phNum" + "'";


生成dml:

UPDATE CustomerPhone SET PhoneTypeID = @PhoneTypeID, PhoneNumber = @PhoneNumber WHERE CustomerID = @cusID AND PhoneNumber = '@phNum'


@PhoneTypeID和@PhoneNumber从两个属性中获得。我们从用户输入文本框中为表示层中的这些属性分配了值。以下获取值的部分:

objSqlCommand.Parameters.AddWithValue("@" + PropertyItem.Name.ToString(), PropertyItem.GetValue(this, null));


下面的代码填充WHERE的值:

                    objSqlCommand.Parameters.AddWithValue("@cusID", cusID);
                    objSqlCommand.Parameters.AddWithValue("@phNum", this.PhoneNumber);


最终代码应如下所示:

UPDATE CustomerPhone
SET PhoneTypeID = 7, PhoneNumber = 999444
WHERE CustomerID = 500 AND PhoneNumber = '911';


电话类型ID为7-从文本框中获取的用户值
电话号码是999444-从文本框中获取的用户值

上面的最终更新语句适用于sql环境,但是在运行时
通过应用程序,执行非查询运行正常,并更新0行!我想知道为什么?

最佳答案

这就是问题:

AND PhoneNumber = '@phNum'


那是在寻找完全是文本'@phNum'的电话号码-它没有使用名为phNum的参数。你要

AND PhoneNumber = @phNum


您也无缘无故地分解字符串文字。这个说法:

objSqlCommand.CommandText = "UPDATE " + this.TableName + " SET " + SQL +
    " WHERE " + cust.PKName + " = @cusID AND PhoneNumber = " +
    "'" + "@phNum" + "'";


将更容易理解为:

objSqlCommand.CommandText = "UPDATE " + this.TableName + " SET " + SQL +
    " WHERE " cust.PKName + " = @cusID AND PhoneNumber = '@phNum'";


显然,您希望删除其中的单引号,使其成为:

objSqlCommand.CommandText = "UPDATE " + this.TableName + " SET " + SQL +
    " WHERE " cust.PKName + " = @cusID AND PhoneNumber = @phNum";


一点重构也不会出错。这个循环:

foreach (PropertyInfo PropertyItem in this.GetType().GetProperties())
{
    if (!(PropertyItem.Name.ToString() == cust.PKName))
    {
        if (PropertyItem.Name.ToString() != "TableName")
        {
            if (SQL == null)
            {
                SQL = PropertyItem.Name.ToString() + " = @" + PropertyItem.Name.ToString();
            }
            else
            {
                SQL = SQL + ", " + PropertyItem.Name.ToString() + " = @" + PropertyItem.Name.ToString();
            }
         }
         else
         {
            break;
         }
    }


}

这样会更简单易读:

StringBuilder sqlBuilder = new StringBuilder();
foreach (PropertyInfo property in this.GetType().GetProperties())
{
    string name = property.Name;
    // I believe you had a bug before - the properties being updated
    // would depend on the ordering of the properties - if it
    // ran into "TableName" first, it would exit early!
    // I *suspect* this is what you want
    if (name != cust.PKName && name != "TableName")
    {
        sqlBuilder.AppendFormat("{0} = @{0}, ", name);
    }
}
// Remove the trailing ", "
if (sqlBuilder.Length > 0)
{
    sqlBuilder.Length -= 2;
}


您也可以对最终循环执行类似的操作。

关于c# - 更新语句通过C#Winform应用程序更新0行吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2934526/

10-17 00:20