很菜的问题:C#中catch那块代码如何写??请指点…… C# + Sql Server2000 开发WebForm使用try …… catch 来捕捉连接数据库时的异常请问catch 那块的代码如何写的啊???小弟很菜,请大侠指点……谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 try{}catch(Exception err){.......} catch要做的几件事:1,给出错误信息, 2,关闭错误产生后,没有关闭的相关资源 try { ..... }catch(Exception e) { ...... }finaly { 关闭相关资源 } try{ .....}catch(Exception e){ label1.Text = e.Message;//假设label1为web上的一个Label组件}finaly{ 关闭相关资源} 我各种的Exception。SqlException,IOException综合起来就是Exception最后用finally释放所有资源 try{ ...}catch(Exception ex){ throw ex;}try{ ...}catch{ ....//其他处理} try{ 我的数据库连接.open();}catch{ 可以直接写 MessageBox.Show("数据库连接异常,请检查!");}finally{ 我的数据库连接.close();} public void Connection(){ // 连接字符串 string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"]; string Sql = "SELECT * FROM Employees";// 创建 SqlConnection 和 SqlCommand 对象 SqlConnection thisConnection = new SqlConnection(ConnectionString); SqlCommand thisCommand = new SqlCommand(Sql, thisConnection);try{ thisConnection.Open(); thisCommand.ExecuteNonQuery(); myLabel.Text = "未出现异常!";}catch(SqlException ex){ // 异常信息显示 myLabel.Text = "<b>数据库执行错误</b><br>"; myLabel.Text += "错误信息:" + ex.Source + "<br>"; myLabel.Text += "错误行号:" + ex.LineNumber +"行<br>"; myLabel.Text += "详细信息:" + ex.Message + "<br>";}finally{ // 关闭数据库连接 thisConnection.Close();}} [WebMethod (EnableSession=true)] public DataSet GetDetailByPKID(int svcFrmID,out string msg) { msg = ""; if (!SecurityHelper.VerifyCredentials(AppGlobal.UPDSFORM,out msg)) { return null; } //string sqlRead = "SELECT * FROM ServiceForm WHERE ID=" + svcFrmID; string sqlRead = "SELECT A.CustomerName AS CstmName,A.*,B.CustomerName AS EvtCustmName,B.* FROM ServiceForm as A"; sqlRead += " INNER JOIN CustomerEvent as B"; sqlRead += " ON A.CustomerEventID = B.ID"; sqlRead += " WHERE A.ID =" + svcFrmID; try { DataSet ds = new DataSet(); string[] svcFrmDetail = new string[] {"ServiceFormDetail"}; SqlHelper.FillDataset(SqlHelper.CONN_STRING,CommandType.Text,sqlRead,ds,svcFrmDetail); return ds; } catch(Exception ex) { msg = ex.Message.Trim(); return null; } } 关于dataGridView1循环删除有什么办法? [求教]使用FreeTextBox的问题 c#操作应用程序问题!! DataGridView 合图片行... 求助,一个方法,如何把string中的小写字母,转化为大写字母。 C#做的高校排课系统,跪求! 用子线程调用excel,用尽网上所有释放excel方法...依然无解!!! C#为什么总是先执行串口的程序? 控制台程序,如何清屏? C#用direcx9画三维 求一个好用的Zip压缩库 绑定的问题
{
}
catch(Exception err)
{
.......
}
2,关闭错误产生后,没有关闭的相关资源
{
.....
}
catch(Exception e)
{
......
}
finaly
{
关闭相关资源
}
{
.....
}
catch(Exception e)
{
label1.Text = e.Message;//假设label1为web上的一个Label组件
}
finaly
{
关闭相关资源
}
SqlException,IOException综合起来就是Exception
最后用finally释放所有资源
{
...
}
catch(Exception ex)
{
throw ex;
}try
{
...
}
catch
{
....//其他处理
}
{
我的数据库连接.open();
}
catch
{
可以直接写 MessageBox.Show("数据库连接异常,请检查!");
}
finally
{
我的数据库连接.close();
}
{
// 连接字符串
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];
string Sql = "SELECT * FROM Employees";
// 创建 SqlConnection 和 SqlCommand 对象
SqlConnection thisConnection = new SqlConnection(ConnectionString);
SqlCommand thisCommand = new SqlCommand(Sql, thisConnection);
try
{
thisConnection.Open();
thisCommand.ExecuteNonQuery();
myLabel.Text = "未出现异常!";
}
catch(SqlException ex)
{
// 异常信息显示
myLabel.Text = "<b>数据库执行错误</b><br>";
myLabel.Text += "错误信息:" + ex.Source + "<br>";
myLabel.Text += "错误行号:" + ex.LineNumber +"行<br>";
myLabel.Text += "详细信息:" + ex.Message + "<br>";
}
finally
{
// 关闭数据库连接
thisConnection.Close();
}
}
public DataSet GetDetailByPKID(int svcFrmID,out string msg)
{
msg = "";
if (!SecurityHelper.VerifyCredentials(AppGlobal.UPDSFORM,out msg))
{
return null;
}
//string sqlRead = "SELECT * FROM ServiceForm WHERE ID=" + svcFrmID;
string sqlRead = "SELECT A.CustomerName AS CstmName,A.*,B.CustomerName AS EvtCustmName,B.* FROM ServiceForm as A";
sqlRead += " INNER JOIN CustomerEvent as B";
sqlRead += " ON A.CustomerEventID = B.ID";
sqlRead += " WHERE A.ID =" + svcFrmID;
try
{
DataSet ds = new DataSet();
string[] svcFrmDetail = new string[] {"ServiceFormDetail"};
SqlHelper.FillDataset(SqlHelper.CONN_STRING,CommandType.Text,sqlRead,ds,svcFrmDetail);
return ds;
}
catch(Exception ex)
{
msg = ex.Message.Trim();
return null;
}
}