我想针对使用数据集设计器创建的类型化数据集执行自定义命令。为此,我需要获得对底层连接的引用,对吧?怎么去这个?我可以在哪个自动生成的文件中找到连接?
答案 0 :(得分:2)
您可以在设计器中设置TableAdapter的ConnectionModifier属性,默认为Internal,这样您就可以在同一个项目/程序集中使用它,将其更改为Public并在任何需要的地方使用它。或者更好的方法是在TableAdapter名称/命名空间中创建一个部分类,并将逻辑封装在同一个表适配器中:
// 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
}
}
}
答案 1 :(得分:0)
typedTableAdapter ta = new myNameSpace.myDataSet.myDataSetTableAdapters.typedTableAdapter;
SqlClient.SqlCommand com = new SqlClient.SqlCommand("my query");
com.Connection = ta.Connection;