CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

try ... catch 问题,大伙帮看看

楼主duanqp(...)2006-07-03 02:20:50 在 .NET技术 / C# 提问

初学,盼大伙讲解一下  
  问题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

相关问题

关键词

得分解答快速导航

  • 帖主:duanqp
  • celas
  • zhgroup

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo