将VB参数注入存储过程时出现问题(FireBird)

时间:2009-03-17 22:21:29

标签: vb.net stored-procedures parameters firebird

这里的每个人都是直接或间接的帮助。并且非常希望这再次成为现实。

为了澄清问题,存储过程在FireBird下运行,VB是.NET变种

我有一个存储过程(下面摘录,重要的是WHERE)

  select pn, pnm.description, si_number, entry_date, cmp_auto_key, 
  parts_flat_price,    labor_flat_price, misc_flat_price, woo_auto_key, 
  wwt_auto_key
  from parts_master pnm, wo_operation woo
 where pn like :i_pn || '%'
   and pnm.pnm_auto_key = woo.pnm_auto_key
  into :pn, :description, :work_order, :entry_date, :cmp, :parts_price,
       :labor_price, :misc_price, :woo, :wwt

我正在尝试从vb app传递一个参数,该参数使用参数I_PN,其代码如下所示(MyServer和MyPassword的变量是从代码的早期部分确定的。)

    Try
        Dim FBConn As New FirebirdSql.Data.FirebirdClient.FbConnection()
        Dim FBCmd As FirebirdSql.Data.FirebirdClient.FbCommand

        Dim MyConnectionString As String
        MyConnectionString = _
        "datasource=" & MyServer & ";database=" & TextBox4.Text & "; & _
        user id=SYSDBA;password=" & MyPassword & ";initial catalog=;"

        FBConn = New FirebirdSql.Data.FirebirdClient. & _
        FbConnection(MyConnectionString)

        FBConn.Open()
        FBConn.CreateCommand.CommandType = CommandType.StoredProcedure

        FBCmd = New FirebirdSql.Data.FirebirdClient. & _
        FbCommand("WIP_COSTS", FBConn)

        FBCmd.CommandText = "WIP_COSTS"

        FBConn.CreateCommand.Parameters. & _
        Add("@I_PN", FirebirdSql.Data.FirebirdClient.FbDbType.Text). & _
        Value = TextBox1.Text

        Dim I_PN As Object = New Object()
        Me.WIP_COSTSTableAdapter.Fill(Me.WOCostDataSet.WIP_COSTS, @I_PN)
        FBConn.Close()
    Catch ex As System.Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    End Try

当我执行VB.App并尝试运行程序时,我收到以下错误:

  

动态SQL错误
  SQL错误代码= -206
  列未知
  I_PN
  在第1行第29栏

我无法完全理解实际问题。意思是,我不知道我的逻辑在VB端是错误的,还是在存储过程中。

所包含的任何编码都是通过我在GoogleFu长途旅行期间发现的各种代码中找到的示例进行整理的。

任何拥有超过一个月或两年经验(与我不同)的人都可以只用一眼就能证明 - 我的代码可能非常糟糕而且形式不好 - 当然不是优雅的,而且最确定的是在操作中。我当然是张开双臂欢迎各种建议。

像往常一样,如果您有其他问题,我会尽我所能回答。

再次感谢。

Jasoomian

3 个答案:

答案 0 :(得分:1)

经过一番重新思考和更多研究后,我终于让我的代码工作了......

        Try

        ' Code for checking server location and required credentials

        Dim FBConn As FbConnection
        ' Dim FBAdapter As FbDataAdapter
        Dim MyConnectionString As String

        MyConnectionString = "datasource=" _
                        & MyServer & ";database=" _
                        & TextBox4.Text & ";user id=SYSDBA;password=" _
                        & MyPassword & ";initial catalog=;Charset=NONE"

        FBConn = New FbConnection(MyConnectionString)
        Dim FBCmd As New FbCommand("WIP_COSTS", FBConn)

        FBCmd.CommandType = CommandType.StoredProcedure
        FBCmd.Parameters.Add("@I_PN", FbDbType.VarChar, 40)
        FBCmd.Parameters("@I_PN").Value = TextBox1.Text.ToUpper


        Dim FBadapter As New FbDataAdapter(FBCmd)
        Dim dsResult As New DataSet
        FBadapter.Fill(dsResult)

        Me.WIP_COSTSDataGridView.DataSource = dsResult.Tables(0)

        Dim RecordCount As Integer
        RecordCount = Me.WIP_COSTSDataGridView.RowCount
        Label4.Text = RecordCount

    Catch ex As System.Exception
        System.Windows.Forms.MessageBox.Show _
        ("There was an error in generating the DataStream, " & _
        "please check the system credentials and try again. " &_ 
        "If the problem persists please contact your friendly " &_ 
        "local IT department.")
    End Try

    ' // end of line

我还以为我需要对实际的存储过程进行更改,但结果证明这是不正确的。

代码可能不太漂亮,我需要在TRY块中做更多工作才能更好地处理错误;但是,它有效。

感谢所有插话并帮助我走上正轨的人。

答案 1 :(得分:0)

尝试更改此内容:

FBConn.CreateCommand.Parameters. & _
        Add("@I_PN", FirebirdSql.Data.FirebirdClient.FbDbType.Text). & _
        Value = TextBox1.Text

......对此:

FBCmd.Parameters.AddWithValue("@I_PN", TextBox1.Text)

基本上,您希望将存储过程参数添加到Command对象,而不是Connection对象。

答案 2 :(得分:0)

Andreik,

这是整个存储过程。我们的Firebird是版本1.5.3,使用IbExpert版本2006.12.13,Dialect 3

编写
Begin
For
select pn, pnm.description, si_number, entry_date, cmp_auto_key, parts_flat_price,
       labor_flat_price, misc_flat_price, woo_auto_key, wwt_auto_key
  from parts_master pnm, wo_operation woo
 where pn like :i_pn || '%'
   and pnm.pnm_auto_key = woo.pnm_auto_key
  into :pn, :description, :work_order, :entry_date, :cmp, :parts_price,
       :labor_price, :misc_price, :woo, :wwt

Do begin
   labor_hours = null;
   work_type = null;
   parts_cost = null;
   labor_cost = null;
   ro_cost = null;
   customer = null;

   select company_name
     from companies
    where cmp_auto_key = :cmp
     into :customer;

   select work_type
     from wo_work_type
    where wwt_auto_key = :wwt
     into :work_type;

   select sum(sti.qty*stm.unit_cost)
     from stock_ti sti, stock stm, wo_bom wob
    where sti.wob_auto_key = wob.wob_auto_key
      and sti.stm_auto_key = stm.stm_auto_key
      and wob.woo_auto_key = :woo
      and sti.ti_type = 'I'
      and wob.activity <> 'Work Order'
      and wob.activity <> 'Repair'
     into :parts_cost;

   select sum(sti.qty*stm.unit_cost)
     from stock_ti sti, stock stm, wo_bom wob
    where sti.wob_auto_key = wob.wob_auto_key
      and sti.stm_auto_key = stm.stm_auto_key
      and wob.woo_auto_key = :woo
      and sti.ti_type = 'I'
      and wob.activity = 'Repair'
     into :ro_cost;

   select sum(wtl.hours*(wtl.fixed_overhead+wtl.variable_overhead+wtl.burden_rate)),
          sum(wtl.hours)
     from wo_task_labor wtl, wo_task wot
    where wtl.wot_auto_key = wot.wot_auto_key
      and wot.woo_auto_key = :woo
     into :labor_cost, :labor_hours;

   suspend;
   end
End

硬编码 - 我在评论中回答了你的建议。