从SQL Server语句存储多个值

时间:2015-10-19 09:47:47

标签: vb.net

考虑以下代码

 Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName = @username and UserPass = @password", con)
 cmd.Parameters.AddWithValue("@username", login_username.Text)
 cmd.Parameters.AddWithValue("@password", hash_pass)

 Dim da As New SqlDataAdapter(cmd)

假设存在列名status我想将status的结果存储在变量中。

P.S我是VB.NET的初学者所以请耐心等待我

2 个答案:

答案 0 :(得分:1)

您可以在SqlDataReader上执行该语句或填充DataTable。使用SqlDataReader的一个例子是

AutoRichLabel

这是DataTable版本

{\colortbl ;\red0\green77\blue187;}
{\pard\b BOLD\b0  \i ITALIC\i0  \ul UNDERLINE\ul0 \\\{\}\par}
{\pard\cf1\b BOLD\b0  \i ITALIC\i0  \ul UNDERLINE\ul0\cf0 \\\{\}\par}

答案 1 :(得分:0)

如果status返回单个单元格值(状态列中的单行),则可以使用以下方法

Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName = @username and UserPass = @password", con)
cmd.Parameters.AddWithValue("@username", login_username.Text)
cmd.Parameters.AddWithValue("@password", hash_pass)
Dim status As String
status = IIf(IsDBNull(cmd.ExecuteScalar), "Not Available", cmd.ExecuteScalar)
        'IsDBNull is used tocheck whether cmd.ExecuteScalar is null or what
        'IIF() is used to handle null here, You can assign custom value for status variable if
        'select returns null value
        'If you dont need to use them its possible to write
        ' status = cmd.ExecuteScalar

详细了解ExecuteScalar()