简体   繁体   中英

SQL exception unhandled , invalid object name

I am trying to get a query to execute on button click in visual studio 2010. As soon I reach the ExecuteReader statement, it says

"Invalid object name 'task'."

The table name is task and the field name is task as well. The SQL connection runs fine. What could be the problem ?

        string MyConString = "Data Source=.\\SQLExpress;Initial Catalog=filter;Integrated Security=SSPI;";
        SqlConnection connection = new SqlConnection(MyConString);

        string mycmd;
        connection.Open();
        SqlCommand cmd;
        //mycmd = "select task from task where task='" + textBox3.Text + "'";
        mycmd = "SELECT task.id FROM task WHERE task.task='plan1'";
        cmd = new SqlCommand(mycmd, connection);
        SqlDataReader sdr;
        string val = "";
        //try
        //{
        //cmd.ExecuteNonQuery();
        sdr = cmd.ExecuteReader();

first suggestion is to change the columnname or table name from task to something else so keep the two different names but for a solution:

You could do with a table alias:

mycmd = "SELECT t.id FROM task t WHERE t.[task]='plan1'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM