首先,请帮帮我!我再也无法忍受了。我找不到错误的位置。这是我的问题:
我正在尝试通过c#winform应用程序更新一行。从应用程序生成的更新查询格式正确。我在sql server环境中测试过它,效果很好。当我从应用程序运行它时,我得到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行!我想知道为什么?
答案 0 :(得分:3)
这是问题所在:
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;
}
您也可以使用最终循环执行类似的操作。
答案 1 :(得分:2)
PhoneNumber
是字符串还是整数?
我认为你是SET
整数,但是以WHERE
作为文字检查。这不是问题吗?
如果是整数,请尝试:
UPDATE CustomerPhone
SET PhoneTypeID = 7, PhoneNumber = 999444
WHERE CustomerID = 500 AND PhoneNumber = 911;
如果是字符串,请尝试:
UPDATE CustomerPhone
SET PhoneTypeID = 7, PhoneNumber = '999444'
WHERE CustomerID = 500 AND PhoneNumber = '911';