当前位置: 代码迷 >> ASP.NET >> datareader = command.ExecuteReader();'=' 附近有语法异常
  详细解决方案

datareader = command.ExecuteReader();'=' 附近有语法异常

热度:9801   发布时间:2013-02-25 00:00:00.0
datareader = command.ExecuteReader();'=' 附近有语法错误。
找了1个小时也没找出错误。
先发下代码
C# code
    private void ShowTableList(string tableName, bool clearTable)    {        if (clearTable == true)        {            this.ShowTable.Rows.Clear();        }        SqlConnection conn = new SqlConnection(SqlConn.Connstring);        SqlCommand command = null;        if (tableName == "mainforum")        {            command = new SqlCommand("SELECT * FROM " + tableName + "WHERE id = " + Session["mainid"].ToString(), conn);        }        else        {            command = new SqlCommand("SELECT * FROM " + tableName + "WHERE titleid = " + Session["mainid"].ToString(), conn);        }        SqlDataReader datareader = null;        int num = GetRecordNumber(tableName);        Label[] label = new Label[num];        HyperLink[] hyperLink = new HyperLink[num];        TextBox[] textbox = new TextBox[num];        conn.Open();        datareader = command.ExecuteReader();//???????????????        int i = 0;        while (datareader.Read())        {            TableRow row = new TableRow();            TableCell cell = new TableCell();            label[i] = new Label();            if (tableName == "mainforum")            {                label[i].Text = "Post author : " + datareader["author"].ToString() +                    "<br>Post title : " + datareader["title"].ToString() +                    "<br>Last modify time : " + datareader["releasetime"].ToString() + "&nbsp&nbsp";            }            else            {                label[i].Text = "Post author : " + datareader["author"].ToString() +                    "<br>Last modify time : " + datareader["lastmodifytime"].ToString() + "&nbsp&nbsp";            }            cell.Controls.Add(label[i]);            hyperLink[i] = new HyperLink();            hyperLink[i].Text = "[Modified post]";            hyperLink[i].Target = "_top";            if (tableName == "mainforum")            {                hyperLink[i].NavigateUrl = "ArticleModify.aspx?state=modifyMain";            }            else            {                hyperLink[i].NavigateUrl = "ArticleModify.aspx?state=modifySub&subid=" + datareader["id"].ToString();            }            cell.Controls.Add(hyperLink[i]);            row.Cells.Add(cell);            this.ShowTable.Rows.Add(row);            row = new TableRow();            cell = new TableCell();            textbox[i] = new TextBox();            textbox[i].TextMode = TextBoxMode.MultiLine;            textbox[i].ReadOnly = true;            textbox[i].Width = 700;            textbox[i].Height = 100;            textbox[i].Text = "" + datareader["content"].ToString();            cell.Controls.Add(textbox[i]);            row.Cells.Add(cell);            this.ShowTable.Rows.Add(row);        }        datareader.Close();        conn.Close();    }


------解决方案--------------------------------------------------------
"SELECT * FROM " + tableName + "WHERE id = " + Session["mainid"].ToString()

语句拼接,中间连空格都没有,sql语法错误
------解决方案--------------------------------------------------------
类似这样,WHERE左边加个空格
command = new SqlCommand("SELECT * FROM " + tableName + " WHERE id =" + Session["mainid"].ToString(), conn);
  相关解决方案