阅读从数据库中,并填写数据表 [英] Read from database and fill DataTable

查看:163
本文介绍了阅读从数据库中,并填写数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一组数据的的DataReader ,并分配给一个字符串。现在我需要填写数据表列与查询字段。该数据表连接到电网,以显示填充数据。

查询:

  STRSQL =SELECT的Emp code,的EmpID,EmpName从dbo.Employee
 

数据表的Emp code,的EmpID,EmpName

我需要阅读查询,并分配给数据表的列,并填写表格。我曾尝试如下,但我没有得到正确的输出,

  Me.DtShifts.Tables(NonAllocated)。清除()
Me.DtShifts.Tables(NonAllocated),负载(DR)
 

解决方案

Connection对象是仅作说明。 DataAdapter的是键位:

  STRSQL暗淡作为字符串=SELECT的Emp code,的EmpID,EmpName从dbo.Employee
DIM DTB作为新数据表
使用CNN作为新的SqlConnection(的connectionString)
  cnn.Open()
  用爸爸作为新的SqlDataAdapter(STRSQL,美国有线电视新闻网)
    dad.Fill(DTB)
  结束使用
  cnn.Close()
结束使用
 

I'm getting a set of data by a DataReader and assigning to a string. Now I need to fill the DataTable columns with the query fields. The DataTable is connected to a grid to display the filled data.

query is :

strSQL = "SELECT EmpCode,EmpID,EmpName FROM dbo.Employee

DataTable columns are EmpCode, EmpID, EmpName.

I need to read the query and assign to the columns of DataTable and fill the table. I have tried as below but i dont get the proper output,

Me.DtShifts.Tables("NonAllocated").Clear()
Me.DtShifts.Tables("NonAllocated").Load(dr)

解决方案

Connection object is for illustration only. The DataAdapter is the key bit:

Dim strSql As String = "SELECT EmpCode,EmpID,EmpName FROM dbo.Employee"
Dim dtb As New DataTable
Using cnn As New SqlConnection(connectionString)
  cnn.Open()
  Using dad As New SqlDataAdapter(strSql, cnn)
    dad.Fill(dtb)
  End Using
  cnn.Close()
End Using

这篇关于阅读从数据库中,并填写数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆