如何读取数据库中的特定列?

时间:2016-10-09 18:02:20

标签: vb.net tsql

我有一个名为DTR_Table的表,它有8列,即: EmployeeID,Date,MorningTime-In,MorningTime-Out,AfternoonTime-In,AfternoonTime-Out,UnderTime,Time-Rendered。我想阅读列" AfternoonTime-In"。 以下是我的代码。它读取我的" AfternoonTime-In"字段,但它继续显示"有行"即使该栏目中没有任何内容。

我该如何解决这个问题?

Connect = New SqlConnection(ConnectionString)
Connect.Open()

Dim Query1 As String = "Select  [AfternoonTime-Out] From Table_DTR Where Date = @Date and EmployeeID = @EmpID "
Dim cmd1 As SqlCommand = New SqlCommand(Query1, Connect)
cmd1.Parameters.AddWithValue("@Date", DTRform.datetoday.Text)
cmd1.Parameters.AddWithValue("@EmpID", DTRform.DTRempID.Text)

Using Reader As SqlDataReader = cmd1.ExecuteReader()
    If Reader.HasRows Then
        MsgBox("Has rows")
        Reader.Close()

    Else
        MsgBox("empty")
    End If
End Using`

1 个答案:

答案 0 :(得分:1)

返回DataReader后,如果要从查询中提取值,则需要从中读取数据。

10-09 21:46:51.793 4867-4867/benlab.app1ben E/AndroidRuntime: FATAL EXCEPTION: main
                                                              Process: benlab.app1ben, PID: 4867
                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{benlab.app1ben/benlab.app1ben.game_page}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.Random.nextInt()' on a null object reference
                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
                                                                  at android.app.ActivityThread.access$900(ActivityThread.java:154)
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                  at android.os.Looper.loop(Looper.java:234)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5526)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.Random.nextInt()' on a null object reference
                                                                  at benlab.app1ben.game_page.newProblem(game_page.java:47)
                                                                  at benlab.app1ben.game_page.onCreate(game_page.java:33)
                                                                  at android.app.Activity.performCreate(Activity.java:6285)
                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) 
                                                                  at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) 
                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                  at android.os.Looper.loop(Looper.java:234) 
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5526) 
                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

请注意,我已使用更精确的Add更改了AddWithValue,并指定了参数类型。否则,您的代码将掌握数据库引擎决定使用的任何转换规则,以将传递给AddWithValue的字符串转换为DateTime。 此转换通常会生成无效值,尤其是日期