//一段查询的代码,先输入景点的名称,然后再下拉框中选择条件,然后弹出结果,但是老实报“对象名Attractions无效”,这是怎么回事,请大侠帮忙,数据库中有Attractions这个表
namespace DataConnect
{
    public partial class NameSelect : Form
    {
        public NameSelect()
        {
            InitializeComponent();
        }        private void btnNameSelect_Click(object sender, EventArgs e)
        {
            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  == null  && combNS == null )
                {
                    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++)
                {
                    //for (int j = 0; j < 2; j++)
                    //{
                    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;
                DataSet Ds = new DataSet();
                int Row = Da.Fill(Ds, "Attractions");
                string bh = Ds.Tables["Attractions"].Rows[Convert.ToInt32(txtNS)][a].ToString();
                lblShow.Text = bh;
                sqlConn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
//一段查询的代码,先输入景点的名称,然后再下拉框中选择条件,然后弹出结果,但是老实报“对象名Attractions无效”,这是怎么回事,请大侠帮忙,数据库中有Attractions这个表,请大侠帮帮小弟

解决方案 »

  1.   

    sqlstr = "select Kind ,NF from Attractions where Kind = '"+txtNS+"' and a = '"+combNS+"'";
    查询分析器执行
    连接字符串
      

  2.   

    跟一下你的sql语句是否有问题~
      

  3.   

    string conn = "Trusted_Connection=SSPI";
      SqlConnection sqlConn = new SqlConnection(conn );
      sqlConn.Open();????那有Connection的Text是这样的??"Trusted_Connection=SSPI";
      

  4.   

    服务器,数据库也不指明, sqlConn.Open();没报错???
      

  5.   

    sqlstr = "select Kind ,NF from 库名..Attractions where Kind = txtNS and a = combNS";
      

  6.   

    人问过这问题,参考下http://topic.csdn.net/u/20100713/09/5dfd2fa1-fefe-4f2a-854b-5077b669ccd2.html
      

  7.   

    你说的库名是什么意思,是不是dbo.Attractions,还是什么意思,谢谢大侠的帮忙
      

  8.   


    sqlstr = "select Kind ,NF from Attractions where Kind = '"+txtNS+"' and a = '"+combNS+"'";1楼的大神已经说的很明显了,自己不仔细检查~ 你的字符串是写死的~~ 要用活的 就要'"+txtNS+"'
    还有,你的and a = ……   你这个a 是怎么回事?  你的数据库里有 a 这个字段吗?
    你还是先看看书吧。
      

  9.   

    Attractions两边加[],就是[Attractions]就行了