这看起来有什么问题?

时间:2008-12-23 11:43:39

标签: c# .net oledbparameter

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

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


塞尔吉奥:这是否可以,明确设置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));

7 个答案:

答案 0 :(得分:6)

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

在您的 upd 命令字符串中,您使用引号包围参数,如下所示:

[StartDate] = '@startdate'

删除所有参数中的引号。

答案 1 :(得分:3)

抱歉 emre ,我忽视了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();

希望这可以帮助你。

答案 2 :(得分: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]=?
";

答案 3 :(得分:2)

如果在每个新的OleDbParameter中设置参数类型,则不需要在sql上放置单引号,使得指令不易于输入错误。

答案 4 :(得分: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";

将不会进行优化。

答案 5 :(得分: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();

答案 6 :(得分:0)

Aight!我解决了它听Deltron 3030 ..它总是有效:))

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

抱歉,我讨厌阅读很长篇文章,而是使用程序员的一个很好的优点,那就是懒惰..所以我在这里问:)

谢谢大家的时间和精力。祝大家一切顺利