这是从数据表获取数据的get方法

Private Function GetData() As PagedDataSource
' Declarations
Dim dt As New DataTable
Dim dr As DataRow
Dim pg As New PagedDataSource

' Add some columns
dt.Columns.Add("Column1")
dt.Columns.Add("Column2")

' Add some test data
For i As Integer = 0 To 10
    dr = dt.NewRow
    dr("Column1") = i
    dr("Column2") = "Some Text " & (i * 5)
    dt.Rows.Add(dr)
Next

' Add a DataView from the DataTable to the PagedDataSource
pg.DataSource = dt.DefaultView

' Return the DataTable
Return pg
End Function

它将数据表返回为“pg”
要从数据库中的表中获取记录,必须对此get方法进行哪些更改?
C示例也可以,但很高兴看到我的代码的回复,然后更改…

最佳答案

如果linq to sql不是一个选项,那么您可以回到ado.net。实际上,您需要创建到数据库的连接,并创建和运行一个命令来检索所需的数据并填充一个数据表。下面是一个例子,如果c:

// Create a connection to the database
SqlConnection conn = new SqlConnection("Data Source=MyDBServer;Initial Catalog=MyDB;Integrated Security=True");
// Create a command to extract the required data and assign it the connection string
SqlCommand cmd = new SqlCommand("SELECT Column1, Colum2 FROM MyTable", conn);
cmd.CommandType = CommandType.Text;
// Create a DataAdapter to run the command and fill the DataTable
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);

关于c# - 用数据库中的记录填充DataTable?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3888810/

10-11 03:02
查看更多