VB.net查询Datagrid视图

时间:2016-06-29 09:38:04

标签: sql vb.net

我需要在datagrid中显示值而不是id。 我有两张桌子 表1,具有empid,receivedby,releasebyid,此ID仅在表2中引用 其中Table2具有id,name。

我需要在datagrid上显示Table1尊重NAME而不是ID。 ex表1 ID是1,3,3    表2数据是,1 - Name1,2 - Name2,3-Name3

输出应该是,

  Name1, Name3, Name3

1 个答案:

答案 0 :(得分:0)

对于C#

         StringBuilder sb = new StringBuilder();
         sb.appendLine(" select b.name as EmpName,c.Name as Receivedby,d.name as releasedby from table1 as a ");
         sb.appendLine(" inner join table2 as b on a.empid = b.id ");
         sb.appendLine(" inner join table2 as c on a.receivedbyid = b.id ");
         sb.appendLine(" inner join table2 as d on a.releasebyid = b.id ");

         SqlConnection conn = new SqlConnection(MyConString);
         SqlCommand cmd = new SqlCommand(sb.tostring(), conn);
                conn.Open();
                DataTable dataTable = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);

                da.Fill(dataTable);


                GridVIew.DataSource = dataTable;
                GridVIew.DataBind();

对于vb

Dim sb As New StringBuilder()
sb.appendLine(" select b.name as EmpName,c.Name as Receivedby,d.name as releasedby from table1 as a ")
sb.appendLine(" inner join table2 as b on a.empid = b.id ")
sb.appendLine(" inner join table2 as c on a.receivedbyid = b.id ")
sb.appendLine(" inner join table2 as d on a.releasebyid = b.id ")

Dim conn As New SqlConnection(MyConString)
Dim cmd As New SqlCommand(sb.tostring(), conn)
conn.Open()
Dim dataTable As New DataTable()
Dim da As New SqlDataAdapter(cmd)

da.Fill(dataTable)


GridVIew.DataSource = dataTable
GridVIew.DataBind()