问题
I want to execute a custom command against a typed dataset I created using the dataset designer. For this I need to get a reference to the underlying connection, right? How to go about this? In which autogenerated file can I find the connection?
回答1:
You can set the ConnectionModifier property of the TableAdapter in the designer, is defaulted to Internal, so you can use it in the same project/assembly, change it to Public and use it everywhere you need it. Or a better aproach will be create a partial class based in your TableAdapter name/namespace and encapsulate the logic within the same table adapter:
// if DataSet name is Sales and Table name is Order
namespace SalesTableAdapters // use full namespace here
{
public partial class OrderTableAdapter
{
public void CustomCommand()
{
// here you can use the property this.Connection
// and execute your command
}
}
}
回答2:
typedTableAdapter ta = new myNameSpace.myDataSet.myDataSetTableAdapters.typedTableAdapter;
SqlClient.SqlCommand com = new SqlClient.SqlCommand("my query");
com.Connection = ta.Connection;
来源:https://stackoverflow.com/questions/2134927/how-to-execute-a-custom-command-against-a-typed-dataset