如果参数的数据类型是对象,我如何将查询的值

How do I assign the value I queried to a String which is an argument in a procedure if the argument#39;s data type is an Object?(如果参数的数据类型是对象,我如何将查询的值分配给作为过程中的参数的字符串?)
本文介绍了如果参数的数据类型是对象,我如何将查询的值分配给作为过程中的参数的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了这个过程来帮助我重用一个选择查询:

I made this procedure to help me re-use a select query:

Private Sub selectQry(ByVal myColumn As String, ByVal myTable As String, ByVal myFilter As String, ByVal myObjectOne As Object, ByVal myObj As Object)
    Dim qrySlctCaldate As String = "SELECT " + myColumn + " FROM " + myTable + " WHERE " + myFilter + " = '" & Replace(myObjectOne, "'", "''") & "'"
    Dim cmdSlct As New SqlCommand(qrySlctCaldate, transConn.Connection)


    Dim readSCalDate As SqlDataReader
    readSCalDate = cmdSlct.ExecuteReader

    While readSCalDate.Read
        If TypeOf (myObj) Is TextBox Or TypeOf (myObj) Is ComboBox Then
            myObj.Text = readSCalDate.Item(myColumn).ToString
        Else
            myObj = readSCalDate.Item(myColumn).ToString

        End If
    End While
    readSCalDate.Close()

如果我希望将所选值放置在文本框中并且它工作正常,我会像这样使用它

And I use it like this if I would want the selected value placed in a textbox and it works fine

selectQry("ProcConvDescription", "Line", "LineCode", nameValue.Value, txtProcess)

但是,如果我希望以这样的字符串传递值:

However if I want the value to be passed in a string like so:

 selectQry("LastCalibrationDate", "EquipmentItem", "ControlNo", txtControlNo.Text, strCalDate)

该字符串最终具有一个空字符串值.如何将我查询的值分配给该字符串?

The string ends up having an empty string value. How do I assign the value I queried to that String?

推荐答案

你应该将你的方法修改为一个返回字符串的函数,而不是试图在函数内部分配它.

You should modify your method to be a function returning the string rather than trying to assign it inside the function.

Private Function selectQry(ByVal myColumn As String, ByVal myTable As String, 
                           ByVal myFilter As String, ByVal myObjectOne As Object
                          ) As String
    Dim qrySlctCaldate As String = _
        String.Format("SELECT {0} FROM {1} WHERE {2} = '{3}'",
                      myColumn, myTable, myFilter,
                      Replace(myObjectOne, "'", "''"))
    Dim cmdSlct As New SqlCommand(qrySlctCaldate, transConn.Connection)

    Using readSCalDate As SqlDataReader = cmdSlct.ExecuteReader
        While readSCalDate.Read
            Return readSCalDate.Item(myColumn).ToString
        End While
    End Using
End Sub

txtProcess.Text = selectQry("ProcConvDescription", "Line", "LineCode", nameValue.Value)
strCalDate = selectQry("LastCalibrationDate", "EquipmentItem", "ControlNo", txtControlNo.Text)

此外,我强烈建议您启用 Option Strict 以获得更好的类型安全性.

Also, I would highly recommend turning on Option Strict to get better type safety.

这篇关于如果参数的数据类型是对象,我如何将查询的值分配给作为过程中的参数的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)