try ... catch 问题,大伙帮看看
初学,盼大伙讲解一下
问题1
LinkDataBase link = new LinkDataBase();
string sendTableName = "用户清单";
try
{
this.ds = link.SelectDataBase(sendStrSQL,sendTableName);
}
catch(Exception e)
{...}
this.myTable = ds.Tables[0];
//如果连接帐户,密码错误,捕获不到数据库连接的异常,只是提示找不到表0
//应该在哪儿捕获异常合适?(仔细看了一下,这个地方应该是不会有异常的)
//LinkDataBase 是一个连接数据库的类,付后
问题2
string sendTableName = "用户清单";
try
{
LinkDataBase link = new LinkDataBase();
}
catch(Exception e)
{}
this.ds = link.SelectDataBase(sendStrSQL,sendTableName);
//出错:找不到Link
this.myTable = ds.Tables[0];
问题3
string sendTableName = "用户清单";
LinkDataBase link;
try
{
link = new LinkDataBase();
}
catch(Exception e)
{}
this.ds = link.SelectDataBase(sendStrSQL,sendTableName);
//出错:使用了未赋值的局部变量Link
this.myTable = ds.Tables[0];
问题点数:100、回复次数:5Top
1 楼duanqp(...)回复于 2006-07-03 02:24:36 得分 0
LinkDataBase.cs
--------------------------------------------
using System;
using System.Data;
using System.Data.SqlClient;
namespace AthSoft
{
/// <summary>
/// LinkDataBase 的摘要说明。
/// </summary>
public class LinkDataBase
{
private string strSQL;
private string connectionstring="server=127.0.0.1;uid=sa;pwd=nopassword1;database=jxcbook";
private SqlConnection myConnection;
private SqlCommandBuilder sqlCmdBld;
private DataSet ds = new DataSet();
private SqlDataAdapter da;
public LinkDataBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//-----操作脱机数据库(创建了该类的实例时直接用)----
//根据输入的sql语句检索数据库数据
public DataSet SelectDataBase(string tempStrSQL,string tempTableName)
{
this.strSQL = tempStrSQL;
this.myConnection = new SqlConnection(connectionstring);
this.da = new SqlDataAdapter(this.strSQL,this.myConnection);
this.ds.Clear();
try
{
this.da.Fill(ds,tempTableName);
}
catch(Exception e)
{
}
return ds;//返回填充了数据的DataSet,其中数据表以tempTableName给出的字符串命名
}
//数据库更新(传DataSet和DataTable对象)
public DataSet UpdateDataBase(DataSet changedDataSet,string tableName)
{
this.myConnection = new SqlConnection(this.connectionstring);
this.da = new SqlDataAdapter(this.strSQL,this.myConnection);
this.sqlCmdBld = new SqlCommandBuilder(this.da);
this.da.Update(changedDataSet,tableName);
return changedDataSet;//返回更新后的表
}
//----直接操作数据库(未创建该类的实例时直接用)----
//检索数据库数据(传字符串,直接操作数据库)
public DataTable SelectDataBase(string tempStrSQL)
{
this.myConnection = new SqlConnection(this.connectionstring);
DataSet tempDataSet = new DataSet();
this.da = new SqlDataAdapter(tempStrSQL,this.myConnection);
this.da.Fill(tempDataSet);
return tempDataSet.Tables[0];
}
//数据库更新(传字符串,直接操作数据库)
public int updateDataBase(string tempStrSQL)
{
this.myConnection = new SqlConnection(this.connectionstring);
//使用Command之前一定要先打开连接,后关闭连接,而DataAdapter则会自动打开关闭连接
myConnection.Open();
SqlCommand tempSqlCommand = new SqlCommand(tempStrSQL,this.myConnection);
int intNumber = tempSqlCommand.ExecuteNonQuery();
myConnection.Close();
return intNumber; //返回数据库中影响的行数
}
}
}Top
2 楼celas(长刀公主)回复于 2006-07-03 05:12:44 得分 50
你要这样:
LinkDataBase link = null; // 变量声明在 try 外部 !
try {
link = new LinkDataBase();
} catch {
...
}
if (link != null) {
...
}Top
3 楼celas(长刀公主)回复于 2006-07-03 05:19:08 得分 0
另:
不推荐在无值默认构造函数中直接连接数据库, 有 heavy constructor 嫌疑.
Top
4 楼celas(长刀公主)回复于 2006-07-03 05:20:00 得分 0
无参默认构造函数
Top
5 楼zhgroup(王员外)回复于 2006-07-03 07:59:25 得分 50
问题2
string sendTableName = "用户清单";
LinkDataBase = null;
try
{
link = new LinkDataBase()
}
catch(Exception e)
{}
this.ds = link.SelectDataBase(sendStrSQL,sendTableName);
//出错:找不到Link
this.myTable = ds.Tables[0];
变量声明顺序有问题,要先声明,后使用.Top




