验证 TableAdapter 连接字符串是否成功打开?
有什么比 try/catch 包装更好的方法来验证 TableAdapter 上的连接是否已打开或将成功打开?
public class MyItemParser
{
private myTableAdapter fa;
public FacultyParser()
{
this.fa = new facultyTableAdapter();
}
public bool HasValidConnection()
{
try
{
this.fa.Connection.Open();
}
catch(exception e)
{
return false;
}
return true;
}
public void FillList(IList<myItem> list)
{
foreach (var row in this.fa.GetData())
/**** DoSomething ****/
}
}
What is a better way than try/catch wrapping to verify that the connection on a TableAdapter opened or will open successfully?
public class MyItemParser
{
private myTableAdapter fa;
public FacultyParser()
{
this.fa = new facultyTableAdapter();
}
public bool HasValidConnection()
{
try
{
this.fa.Connection.Open();
}
catch(exception e)
{
return false;
}
return true;
}
public void FillList(IList<myItem> list)
{
foreach (var row in this.fa.GetData())
/**** DoSomething ****/
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 try 和 catch (我通常将连接处理放在不同的类上)
您可以检查(如果保持连接打开)连接状态,但如果状态打开(不够可靠),则没有任何意义。在一个项目中,我什至发出了一个虚拟 SQL 请求来测试连接,然后再将其处理到使用它的实际类。
You should use the try and catch (I usually put the connection handling on a different class)
You might check (if you keep the connection open ) for the connection state but it doesn't mean anything if the state is open (not reliable enough). In one project I even issued a dummy SQL request to test the connection before I handle it to the actuall class that uses it.