0

私有 int DBUpdate() {

DAL dal = new DAL();
string upd = "UPDATE [RPform] SET [ProjectName] = '@pname', [ProjectCode] = '@pcode', [Country] = @cnt, ";
upd += "[StartDate] = '@startdate', [FinishDate] = '@finishdate', [TotalParticipants] = @totpart, [ArrivalDate] = '@arrivedate', ";
upd += "[AirportTransfer] = @airtran, [AirportDate] = '@airdate', [AirportHours] = @airhour, [AirportMinutes] = @airmin, ";
upd += "[Problems] = '@problems', [FirstDayActivities] = '@fdayact' ";
upd += "WHERE [UserID]=@usid";

    OleDbParameter[] parm = new OleDbParameter[] { 
    new OleDbParameter("@pname",projname.Text),
    new OleDbParameter("@pcode",projcode.Text),
    new OleDbParameter("@cnt",countries.SelectedIndex),
    new OleDbParameter("@startdate",datestart.Text),
    new OleDbParameter("@finishdate",datefinish.Text),
    new OleDbParameter("@totpart",totalparticipants.Text),
    new OleDbParameter("@arrivedate",datearrival.Text),
    new OleDbParameter("@airtran",RadioButtonList1.SelectedValue),
    new OleDbParameter("@airdate",dateairport.Text),
    new OleDbParameter("@airhour",airporthours.SelectedIndex),
    new OleDbParameter("@airmin",airportminutes.SelectedIndex),
    new OleDbParameter("@problems",problems.Value),
    new OleDbParameter("@fdayact",firstday.Value),
    new OleDbParameter("@usid",user.ID)
    };
 return (dal.UpdateRow(upd,false,parm));
}

/// 它不会导致异常,但返回 0 行受影响。当从 MS Access 中执行相同的查询时,它工作正常。因此,我认为问题在于参数的处理......但是什么?谢谢


Sergio:这样可以明确设置 OleDbTypes 吗?

///whatever ...
        new OleDbParameter("@problems",problems.Value),
        new OleDbParameter("@fdayact",firstday.Value),
        new OleDbParameter("@usid",user.ID)
        };
//then telling each one what they will be ... 
        parm[0].OleDbType = OleDbType.VarWChar;
        parm[1].OleDbType = OleDbType.VarWChar;

///
     return (dal.UpdateRow(upd,false,parm));
4

7 回答 7

6

检查user.ID的值是否设置正确。

在您的upd命令字符串中,您用引号将参数括起来,如下所示:

[StartDate] = '@startdate'

删除所有参数中的引号。

于 2008-12-23T11:54:13.467 回答
3

只是评论一下,而不是连接,如果您使用多行字符串文字,它更具可读性(并且更容易复制/粘贴)。此外,您不应将参数名称用单引号括起来,即使对于字符串参数也是如此(仅对文字使用单引号)。因此,您的原始 SQL 可能类似于:

string upd = @"
UPDATE [RPform] SET [ProjectName] = @pname, [ProjectCode] = @pcode, [Country] = @cnt, 
[StartDate] = @startdate, [FinishDate] = @finishdate, [TotalParticipants] = @totpart, [ArrivalDate] = @arrivedate, 
[AirportTransfer] = @airtran, [AirportDate] = @airdate, [AirportHours] = @airhour, [AirportMinutes] = @airmin, 
[Problems] = @problems, [FirstDayActivities] = @fdayact 
WHERE [UserID]=@usid
";

正如其他人指出的那样,OleDb 不使用命名参数,因此您实际上应该使用以下内容,确保按照它们在 SQL 语句中出现的相同顺序将参数添加到 OleDbCommand:

string upd = @"
UPDATE [RPform] SET [ProjectName] = ?, [ProjectCode] = ?, [Country] = ?, 
[StartDate] = ?, [FinishDate] = ?, [TotalParticipants] = ?, [ArrivalDate] = ?, 
[AirportTransfer] = ?, [AirportDate] = ?, [AirportHours] = ?, [AirportMinutes] = ?, 
[Problems] = ?, [FirstDayActivities] = ? 
WHERE [UserID]=?
";
于 2008-12-23T15:51:17.137 回答
3

抱歉,我忽略了 OleDb 提供程序如果您的 SQL 命令语法用于 SQL 提供程序,则它是正确的。

因此,您的命令应如下所示:

string upd = "UPDATE [RPform] SET [ProjectName] = ?, [ProjectCode] = ?, [Country] = ?, ";
upd += "[StartDate] = ?, [FinishDate] = ?, [TotalParticipants] = ?, [ArrivalDate] = ?, ";
upd += "[AirportTransfer] = ?, [AirportDate] = ?, [AirportHours] = ?, [AirportMinutes] = ?, ";
upd += "[Problems] = ?, [FirstDayActivities] = ? ";
upd += "WHERE [UserID]=?";

然后你应该像你已经拥有的一样添加你的 OleDb 参数,但是你必须小心按照它们在 SQL 命令中出现的顺序添加它们。

还有一件事,但这与您的问题无关:您不应该连接字符串,因为该操作在性能方面不是很好。

相反,为了很好地布置你的 SQL 命令,试试这个:

StringBuilder upd = new StringBuilder();
upd.Append("UPDATE [RPform] SET [ProjectName] = ?, [ProjectCode] = ?, [Country] = ?, ");
upd.Append("[StartDate] = ?, [FinishDate] = ?, [TotalParticipants] = ?, [ArrivalDate] = ?, ");
upd.Append("[AirportTransfer] = ?, [AirportDate] = ?, [AirportHours] = ?, [AirportMinutes] = ?, ");
upd.Append("[Problems] = ?, [FirstDayActivities] = ? ");
upd.Append("WHERE [UserID]=?");

然后,要获取您的命令字符串,您只需要:

upd.ToString();

希望这可以帮助你。

于 2008-12-23T13:37:03.213 回答
2

在这种情况下不要使用 StringBuilder。将逐字字符串 (@) 与格式良好的 SQL 代码一起使用:

var updateStatement = @"
    UPDATE [RPform]
    SET     [ProjectName]        = @pname     ,
            [ProjectCode]        = @pcode     ,
            [Country]            = @cnt       ,
            [StartDate]          = @startdate ,
            [FinishDate]         = @finishdate,
            [TotalParticipants]  = @totpart   ,
            [ArrivalDate]        = @arrivedate,
            [AirportTransfer]    = @airtran   ,
            [AirportDate]        = @airdate   ,
            [AirportHours]       = @airhour   ,
            [AirportMinutes]     = @airmin    ,
            [Problems]           = @problems  ,
            [FirstDayActivities] = @fdayact
    WHERE   [UserID]             =@usid";

但我必须注意+= 不会被优化!简单的 + 将在编译时执行。例如

string s = "A" + "B" + C"; 

将导致“ABC”没有性能命中但是

string s = "A" + variable + C";

不会优化。

于 2008-12-23T16:38:13.283 回答
2

如果在每个新的 OleDbParameter 中设置参数类型,则不需要在 sql 上放置单引号,从而使指令更不容易出现键入错误。

于 2008-12-23T11:52:22.987 回答
1

顺便说一句,您可能希望使用 StringBuilder 而不是字符串来创建 upd。每次使用 += 时,您都会丢弃旧字符串并创建一个新字符串。

尝试

StringBuilder upd = new StringBuilder();
upd.Append("UPDATE [RPform] SET [ProjectName] = '@pname', [ProjectCode] = '@pcode', [Country] = @cnt, ");
upd.Append("[StartDate] = '@startdate', [FinishDate] = '@finishdate', [TotalParticipants] = @totpart, [ArrivalDate] = '@arrivedate', ");
upd.Append("[AirportTransfer] = @airtran, [AirportDate] = '@airdate', [AirportHours] = @airhour, [AirportMinutes] = @airmin, ");
upd.Append("[Problems] = '@problems', [FirstDayActivities] = '@fdayact' ");
upd.Append("WHERE [UserID]=@usid");

要使用它,只需

upd.ToString();
于 2008-12-23T12:35:29.280 回答
0

Aight! I solved it listening to Deltron 3030 .. it always works :)

This OleDbParameter object sets its oledbtype internally, according to the type I have passed (as object to the ctor) .. so I have passed an integer coming from a TextBox (client side validated but still text, so it crashes) .. also the bool one was radiobutton.Value .. which sends True but still text so it assumes as string and encapsulates it in single quotes ... But I cannot see what names to give these params since they are nameless ...

Sorry I hate reading long articles, instead I use one of the great virtues of a programmer which is lazyness .. So I ask here :)

Thank you all for your time and efforts .. Wish you guys all the success

于 2008-12-23T16:16:06.683 回答