你的连接myCon可能根本没连上

解决方案 »

  1.   

    数据库可能没有连接上
    把myCon.Open()去掉
    或者交换这两句位置:
    myCon.Open();
    SqlDataAdapter sda = new SqlDataAdapter(strSQL,myCon);             //此处strSQL出错
      

  2.   

    strSQl是我写错了,改过来就对了
    但是myCon.Close();   这句的myCon有错,怎样测试myCon有没有连上呢
      

  3.   

    是throw new Exception(e.Message);
    的问题
    你去掉试一下
      

  4.   

    错误提示:The type or namespace name 'myCon' could not be found (are you missing a using directive or an assembly reference?)去掉throw new Exception(e.Message)后还是一样
      

  5.   


    myCon的名子空间不存在,可路径不正确
      

  6.   

    protected string strCon;
    protected string strSQl;你在什么地方定义的这2个变量?
    应该是在类中定义
      

  7.   

    to  cmsoft : myCon的名子空间不存在,可路径不正确
    是什么意思
      

  8.   

    难道在finally{}中不能用所在的方法的局部变量吗
      

  9.   

    you should define SqlConnection myCon outside of try block
    like:
    SqlConnection myCon; 
    try
    {
    strCon = "initial catalog=Kennel;persist security info=False;user id=KennelUser;password=digi;Data Source=192.168.0.18;packet size=4096";
    strSQl = "SELECT * FROM " + tablename;
    myCon = new SqlConnection(strCon);
    myCon.Open();
       .....
      

  10.   

    在try块中定义的变量或其他什么东东,不能用于try块外的地方.
      

  11.   

    最好是使用using...:using(SqlConnection conn=new SqlConnection(connString))
    {
        conn.Open();
        try
        {
        }
        catch(SqlException x)
        {
            throw(new Exception(x.Message));
        }
    }//即使发生异常,conn.Dispose()仍然会调用。
      

  12.   

    C++的using的模拟:http://www.lostinet.com/files/CppUsing.rar
      

  13.   

    mycon在try{}区域内有效,
    private DataSet getDataSet(string tablename)
    {
              SqlConnection mycon=null ....;
    try
    {
                mycon = new SqlConnection(......);
              }          catch()
               finally
              {
                 if( null != null )
                    if( mycon.State == open )
                       mycon.close()
              }
               }具体代码及不太清楚,