这看起来有什么问题吗?

发布于 2024-07-10 22:10:32 字数 1956 浏览 7 评论 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));

private 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));
}

/// It causes no exceptions, but returns 0 rows affected. When same query executed from within MS Access it works fine. Hence I suppose the problem is sth with the handling of parameters ... but what? Thank you


Sergio: is this OK, for setting OleDbTypes explicitly?

///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));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

唐婉 2024-07-17 22:10:32

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

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

[StartDate] = '@startdate'

删除所有参数中的这些引号。

Check if the value of user.ID is being correctly set.

In your upd command string you are surrounding the parameters with quotes, like this:

[StartDate] = '@startdate'

Remove those quotes in all your parameters.

蓝色星空 2024-07-17 22:10:32

抱歉 emre,我忽略了 OleDb Provider。
如果您的 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();

希望这可以帮助您。

Sorry emre, I overlooked the OleDb Provider.
Your SQL command syntax is correct if it was intended for an SQL provider.

So, your command should look like this:

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

Then you should add your OleDb parameters just like you already have, but you have to be careful to add them in the same order they appear in you SQL command.

One more thing, but that don't have to do with your problem:
you shouldn't concatenate strings, because that operation isn't very good in terms of performance.

Instead, to lay out your SQL command nicely, try this:

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]=?");

Then, to get your command string, you just need:

upd.ToString();

Hope that this could help you.

能怎样 2024-07-17 22:10:32

只是为了评论一下,如果您使用多行字符串文字,而不是连接,它会更具可读性(并且更容易复制/粘贴)。 另外,即使对于字符串参数,也不应该将参数名称括在单引号中(仅对文字使用单引号)。 因此,您的原始 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]=?
";

Just to comment that rather than concatenating, it's more readable (and easier to copy/paste) if you use a multiline string literal. Also you should not enclose parameter names in single quotes, even for string parameters (use single quotes for literals only). So your original SQL could look something like:

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
";

As others have pointed out, OleDb does not use named parameters, so you should actually be using the following, ensuring that you add parameters to your OleDbCommand in the same order they appear in the SQL statement:

string upd = @"
UPDATE [RPform] SET [ProjectName] = ?, [ProjectCode] = ?, [Country] = ?, 
[StartDate] = ?, [FinishDate] = ?, [TotalParticipants] = ?, [ArrivalDate] = ?, 
[AirportTransfer] = ?, [AirportDate] = ?, [AirportHours] = ?, [AirportMinutes] = ?, 
[Problems] = ?, [FirstDayActivities] = ? 
WHERE [UserID]=?
";
妳是的陽光 2024-07-17 22:10:32

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

If you set the parameter type in each new OleDbParameter you will not need to place single quotes on your sql making the instruction less prone to typing mistakes.

别想她 2024-07-17 22:10:32

在这种情况下不要使用 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";

不会进行优化。

Don't use the StringBuilder in this case. Use verbatim strings(@) with well formatted SQL code:

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";

But I have to note that += will not be optimized! The simple + will be performed at compile time. For example

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

will result in "ABC" no perf hit
However

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

will be not optimized.

末が日狂欢 2024-07-17 22:10:32

顺便说一句,您可能希望使用 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();

By the way, you'll probably want to use a StringBuilder instead of a string to create upd. Every time you use += you're throwing away your old string a creating a new one.

Try

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");

And to use it, simply

upd.ToString();
屋顶上的小猫咪 2024-07-17 22:10:32

哎呀! 我解决了这个问题,听 Deltron 3030 ..它总是有效:)

这个 OleDbParameter 对象根据我传递的类型(作为 ctor 的对象)在内部设置其 oledbtype ..所以我传递了一个来自 TextBox (客户端)的整数侧面验证但仍然是文本,所以它崩溃了)..布尔值也是 radiobutton.Value ..它发送 True 但仍然是文本,因此它假定为字符串并将其封装在单引号中...但我看不到要给出什么名称这些参数因为它们是无名的...

抱歉,我讨厌阅读长篇文章,相反,我使用程序员的伟大美德之一,那就是懒惰..所以我在这里问:)

谢谢大家的时间和努力..祝你好运伙计们一切成功

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文