选择Statement with Variable not not C#

时间:2017-09-12 01:51:59

标签: c# sql asp.net sql-server oledb

我已经使用AddWithValue和Add添加了一堆Select语句,但这个我似乎无法开始工作。当我输入数字而不是@orderNum时,这很好用。试过@orderNum和' @ orderNum'当我把'''按钮点击没有任何反应,但是当我使用@orderNum时它表示它无法在表格中找到ORDERNUM。 SQL表中的值是CHAR FOR BIT DATA,所以这可能与它有关吗?想法?

我的选择声明结束:

WHERE POITEM.ORDNO  = @orderNum

添加:

cmd.Parameters.Add(new OleDbParameter("@orderNum", poTextBox.Text));

整个SELECT语句(从较小的块连接起来的大C#字符串):

SELECT 
    CAST(POITEM.ITNBR as char(15) ccsid 37) as itemno, 
    CAST(POITEM.ITDSC as char(15) ccsid 37) as ITDSC, 
    CAST(POITEM.HOUSE as char(15) ccsid 37) as hou, 
    CAST(POITEM.REFNO as char(15) ccsid 37) as REF, 
    CAST(POITEM.STAIC as char(15) ccsid 37) as staic,
    POITEM.QTYOR,
    CAST(POITEM.UNMSR as char(15) ccsid 37) as unmsr,
    POITEM.UMCNV,
    POITEM.DKQTY,
    POITEM.STKQT,
    CAST(POITEM.JOBNO as char(15) ccsid 37) as job,
    CAST(POITEM.DPTNO as char(15) ccsid 37) as dept, 
    CAST(POITEM.VCLNB as char(15) ccsid 37) as vclnb, 
    CAST(POITEM.WHSLC as char(15) ccsid 37) as whsloc,
    POITEM.UCORQ, 
    CAST(POITEM.BLCOD as char(15) ccsid 37) as blcod, 
    CAST(POMAST.VNDNR as char(15) ccsid 37) as vendor, 
    CAST(POMAST.PSTTS as char(15) ccsid 37) as pstts,
    CAST(VENNAM.VNAME as char(15) ccsid 37) as vname, 
    CAST(OVERRD.BNAME as char(15) ccsid 37) as bname 
FROM POMAST 
LEFT OUTER JOIN POITEM ON POMAST.ORDNO = POITEM.ORDNO
LEFT OUTER JOIN OVERRD ON POMAST.ORDNO = OVERRD.ORDNO
LEFT OUTER JOIN VENNAM ON POMAST.VNDNR = VENNAM.VNDNR 
WHERE POITEM.ORDNO = @orderNum

4 个答案:

答案 0 :(得分:1)

您说您可以输入一个数字而不是@orderNum,这是否意味着您在CHAR FOR BIT DATA列类型中存储整数订单号,或者可以存在以下订单号:{{1} }?

正如其他人建议您需要转换此行:

PO1234

WHERE POITEM.ORDNO  = @orderNum

然后如果WHERE POITEM.ORDNO = ? 列只包含没有字母字符的整数,则设置如下参数:

POITEM.ORDNO

如果您的int poNum = 0; if (!string.IsNullOrWhiteSpace(poTextBox.Text)) Int32.TryParse(poTextBox.Text.Trim(), out poNum); cmd.Parameters.AddWithValue("?", poNum); 列包含字母字符,则只需将字符串而不是int作为参数发送:

POITEM.ORDNO

尝试一下,让我们知道它是否有效。

答案 1 :(得分:1)

不要使用字符串连接来构建sql,特别是使用字符串类型的字段。

在使用值添加参数时,“@”在sql-server中被识别为参数名称。但是,请将“@”从参数名称引用中删除。参数方法的重点是它是一个参数,sql-server会识别它。

cmd.Parameters.Add(new OleDbParameter("@orderNum", poTextBox.Text));

更改为

cmd.Parameters.Add(new OleDbParameter("orderNum", poTextBox.Text));

答案 2 :(得分:0)

您正在使用OleDB。 OleDB不使用像@orderNum这样的命名参数。它使用?占位符。所以你需要这个:

WHERE POITEM.ORDNO  = ?

并且:

cmd.Parameters.Add("?", OleDbType.Integer).Value = poTextBox.Text;

OleDb将根据您向集合中添加参数的顺序,匹配查询文本中哪个占位符的参数。

或者,如果你正在与Sql Server交谈,你可以使用SqlClient而不是OleDb(System.Data.SqlClientSqlConnectionSqlCommand代替System.Data.OleDbOleDbConnectionOleDbCommand)。然后命名的参数将起作用。

答案 3 :(得分:-1)

这是有效的......奇怪的是参数没有。这里的另一位程序员想到尝试这个并且它有效!

OleDbCommand poSearch = new OleDbCommand("SELECT CAST(POITEM.ITNBR as char(15) ccsid 37) as itemno, CAST(POITEM.ITDSC  as char(30) ccsid 37) as ITDSC,"
      + "CAST(POITEM.HOUSE as char(15) ccsid 37) as hou,  CAST(POITEM.REFNO as char(15) ccsid 37) as REF, CAST(POITEM.STAIC as char(15) ccsid 37) as staic, "
      + "POITEM.QTYOR, CAST(POITEM.UNMSR as char(15) ccsid 37) as unmsr, POITEM.UMCNV, POITEM.DKQTY, POITEM.STKQT, CAST(POITEM.JOBNO as char(15) ccsid 37) as job,"
      + "CAST(POITEM.DPTNO as char(15) ccsid 37) as dept, CAST(POITEM.VCLNB as char(15) ccsid 37) as vclnb, CAST(POITEM.WHSLC as char(15) ccsid 37) as whsloc,"
      + "POITEM.UCORQ, CAST(POITEM.BLCOD as char(15) ccsid 37) as blcod, CAST(POMAST.VNDNR as char(30) ccsid 37) as vendor, CAST(POMAST.PSTTS as char(15) ccsid 37) as pstts,"
      + "CAST(VENNAM.VNAME as char(30) ccsid 37) as vname, CAST(OVERRD.BNAME as char(15) ccsid 37) as bname  FROM POMAST LEFT OUTER JOIN POITEM ON POMAST.ORDNO = POITEM.ORDNO "
      + $"LEFT OUTER JOIN OVERRD ON POMAST.ORDNO = OVERRD.ORDNO LEFT OUTER JOIN VENNAM ON POMAST.VNDNR = VENNAM.VNDNR  WHERE POITEM.ORDNO  = '{poTextBox.Text}'", cn);