How to execute a custom command against a typed dataset

梦想与她 提交于 2019-12-24 16:27:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!