本文介绍了连接2个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能这么友善并帮助我吗?

我需要连接来自同一表ManutencaoFuncionarios的两个字符串,列Nome和Apelido,将结果放在ComboBox中,ComboBoxNomeFuncionario

Could you be so kind and HELP me:

I need to concatenate two strings, columns Nome and Apelido, from the same table, ManutencaoFuncionarios, placing the result in a ComboBox, ComboBoxNomeFuncionario

Imports System.Data
Imports System.Data.SqlClient
Public Class Teste

    Dim con As SqlConnection
    Dim da As SqlDataAdapter
    Dim connectionString As String = ("Data Source=Server;Initial Catalog=DataBaseName;Persist Security Info=True;User ID=sa;Password=MyPassword")
    Dim DataSetManutencaoFuncionarios As DataSet
    Dim comando As SqlCommand







Sub Colocar_Nomes_Funcionarios()
        con = New SqlConnection(connectionString)
        comando = New SqlCommand
        comando.Connection = con
        comando.CommandType = CommandType.Text
        comando.CommandText = "Select Nome+' ' +Apelido as ComboBoxNomeFuncionario from ManutencaoFuncionarios"
        da = New SqlDataAdapter
        da.SelectCommand = comando
        DataSetManutencaoFuncionarios = New DataSet
        con.Open()
        da.Fill(DataSetManutencaoFuncionarios, "ManutencaoFuncionarios")
        ComboBoxNomeFuncionario.DataSource = DataSetManutencaoFuncionarios.Tables("ManutencaoFuncionarios")
        ComboBoxNomeFuncionario.DisplayMember = ("Nome")
        con.Close()
        ComboBoxNomeFuncionario.SelectedIndex = -1
    End Sub







Private Sub Teste_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
              Colocar_Nomes_Funcionarios()
          End Sub
  End Class

推荐答案

Sub Colocar_Nomes_Funcionarios()
        con = New SqlConnection(connectionString)
        comando = New SqlCommand
        comando.Connection = con
        comando.CommandType = CommandType.Text
        comando.CommandText = "Select Nome + ' ' + Apelido as ComboBoxNomeFuncionario from ManutencaoFuncionarios"
        da = New SqlDataAdapter
        da.SelectCommand = comando
        DataSetManutencaoFuncionarios = New DataSet
        con.Open()
        da.Fill(DataSetManutencaoFuncionarios, "ManutencaoFuncionarios")
        ComboBoxNomeFuncionario.DataSource = DataSetManutencaoFuncionarios.Tables("ManutencaoFuncionarios")
        ComboBoxNomeFuncionario.DisplayMember = ("ComboBoxNomeFuncionario")
        con.Close()
        ComboBoxNomeFuncionario.SelectedIndex = -1
    End Sub


comando.CommandText = "Select Nome+' ' +Apelido as ComboBoxNomeFuncionario from ManutencaoFuncionarios"
da = New SqlDataAdapter
da.SelectCommand = comando DataSetManutencaoFuncionarios = New DataSet
con.Open()
da.Fill(DataSetManutencaoFuncionarios, "ManutencaoFuncionarios")

ComboBoxNomeFuncionario.DataSource = DataSetManutencaoFuncionarios.Tables("ManutencaoFuncionarios")

ComboBoxNomeFuncionario.DisplayMember = ("Nome")



将您的SQL更改为此
Select Nome, Nome + '' '' + Apelido as ComboBoxNomeFuncionario from ManutencaoFuncionarios



Change your SQL to this
Select Nome, Nome + '' '' + Apelido as ComboBoxNomeFuncionario from ManutencaoFuncionarios


这篇关于连接2个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-13 02:01