Wednesday, March 7, 2012

Code a function to return a dataset in which there are two tables and relationship

I used a function to create dataset as below:

PublicFunction GetSQLDataSet(ByVal SQLAsString)As DataSet

.....

MyConnection =

New SqlConnection(MyConnectionString)

MyCommand =

New SqlCommand(SQL, MyConnection)

MyDataSet =

New DataSet

MySQLDataAdapter =

New SqlDataAdapter(MyCommand)

MySQLDataAdapter.Fill(MyDataSet)

.....

End function

It works fine.

How to code a function to return a dataset in which there are two tables and relationship?

You can use a SqlCommand which returns 2 tables--that can be a stored procedure which returns 2 tables, or a batch contains 2 SELECT commands seperated by ';' (e.g. "select * from t1; select * from t2). Then when you use the SqlDataAdapter to fill the DataSet, the DataSet will contain 2 tables. Then you can add relationships between the 2 tables as you like, you can refer to:

http://msdn2.microsoft.com/en-us/library/ay82azad.aspx

No comments:

Post a Comment