SqlCommand Comd = new SqlCommand(sqlstr, sqlConn);
                SqlDataAdapter Da = new SqlDataAdapter();
                Da.SelectCommand = Comd;
                Comd.CommandType = CommandType.Text;
                
                DataSet Ds = new DataSet("Attractions");
                Da.Fill(Ds, "Attractions");
                DataTable table = new DataTable();
                table = Ds.Tables["Attractions"];
第6句代码报异常:“对象名 'Attractions' 无效。”,可是我在数据库的表名称就是Attractions,这是怎么回事??请问是哪里错误???

解决方案 »

  1.   

    DataSet Ds = new DataSet();
     Da.Fill(Ds, "Attractions");最后ds.close();
      

  2.   

     DataSet Ds = new DataSet("MyDataSet");
     Ds.Tables.Add("Attractions");//先加DataTable
     Da.Fill(Ds, "Attractions");
                
      

  3.   

    这个错误应该是SqlCommand Comd = new SqlCommand(sqlstr, sqlConn);
    sqlstr的sql语句中的表名写错了,可以检查,和datatable无关
      

  4.   

    查询分析器执行sqlstr,数据连接字符串
      

  5.   


    string conn = "Trusted_Connection=SSPI";
                SqlConnection sqlConn = new SqlConnection(conn );
                sqlConn.Open();        
              
                    string a = String.Empty;
                    string sqlstr = String.Empty;
                    string txtNS = txtInputName.Text;
                    string combNS = String.Empty;
                    combNS = comboBoxSelectKind.SelectedItem.ToString();
                    if (txtNS.Length   == 0  || combNS.Length  == 0 )
                    {
                        MessageBox.Show("请输入景点名称");
                    }
                    string NF = String.Empty;
                    string[,] s = new string[,] { { "地质", "tp_Geology" }, { "土壤", "tp_Soil" }, { "植物", "tp_Plant" }, { "气候", "tp_Climate" }, { "地貌", "tp_Relief" } };
                    for (int i = 0; i < 5; i++)
                    {
                        
                        if (combNS == s[i, 0])
                        {
                            NF = s[i, 0];
                            a = s[i, 1];
                        }
                        
                    }
                         sqlstr = "select Kind ,NF from Attractions where Kind = txtNS and a = combNS";
                try
                {
                    SqlCommand Comd = new SqlCommand(sqlstr, sqlConn);
                    SqlDataAdapter Da = new SqlDataAdapter();
                    Da.SelectCommand = Comd;
                    Comd.CommandType = CommandType.Text;
                    
                    DataSet Ds = new DataSet("Attractions");
                    DataTable table = new DataTable();
                    table = Ds.Tables.Add ("Attractions");
                    Da.Fill(Ds, "Attractions");
                   
                    DataRow row = table.NewRow();
                    
                    string bh = Ds.Tables["Attractions"].Rows[Convert.ToInt32(txtNS)][a].ToString();
                    lblShow.Text = bh;
                    sqlConn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }完整的代码是这样的,请各位帮我看看。我主要想实现的功能是:根据用户输入的姓名(txtNS)和下拉列表的选择,得出相应的内容。下拉列表的选项是 地质、地貌、气候、土壤、植物,数据库中对应的字段名称就是s数组中英文的部分,
      

  6.   

    sqlstr = "select Kind ,NF from Attractions where Kind = txtNS and a = combNS";
    明显有问题~
      

  7.   

    额。。 。我不知道你DataSet ds = new DataSet("Attractions")是什么意思 DataSet ds = new DataSet(string newDataSetName);这是DataSet的第二个重载, 你这样子声明那么Attractions就是你这个DataSet的名字了。 
    然后你的表名也是这个, 会不会 冲突了?
      

  8.   

    Attractions 有没有拼写错误 啊,连接的数据库对不对啊
      

  9.   


    你用查询分析器走下这个语句看看
    我感觉是Kind = txtNS and a = combNS 这有问题
      

  10.   

    跟进中,学习,这是DataSet数据集吧
      

  11.   

    现在其他问题都没有啦,就是运行的时候,数据库登录失败。我修改连接数据库的代码如下:string conn = "Initial Catalog = lushan;Data Source = CHY;" + "Integrated Security = SSPI";
                SqlConnection sqlConn = new SqlConnection(conn );
                sqlConn.Open();请各位帮忙看看
      

  12.   

    SqlDataAdapter Da = new SqlDataAdapter(sqlstr,sqlConn);
    就行了,为啥要用Sqlcommand
      

  13.   

    string conn = "Data Source=(local); Initial Catalog=" + DataBaseName + ";" + "Integrated Security=SSPI";
    试试这个
      

  14.   

    去掉试试DataSet Ds = new DataSet();
    DataTable table = new DataTable();
    table = Ds.Tables.Add ("Attractions");
    Da.Fill(Ds, "Attractions");