关键字' d1'附近的语法不正确

时间:2014-04-07 03:37:59

标签: asp.net sql sql-server

SQL查询在SQL Server Management Studio中有效。但是,在Visual Studio中它会出错

  

D1

附近的语法不正确

代码:

private void GetDataByID(string _id)
{
    string connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
         string sqlCommand = "SELECT  d1.*, d2.* FROM KFM.dbo.ToolBoxDocContent as d1, KFM.dbo.ToolBoxDocument as d2" +
               "where d1.DocumentId = d2.DocumentId and = d2.DocumentId =" + _id;

         SqlCommand cmd = new SqlCommand(sqlCommand, connection);
         SqlDataReader MyReader;

         try
         {
                connection.Open();
                MyReader = cmd.ExecuteReader();

                while (MyReader.Read())
                {
                    string sDueWeek = MyReader["DueWeek"].ToString();
                    string sTitle = MyReader["DocumentTitle"].ToString();
                    //string sEnglishBodyContent = MyReader["DocumentBody"].ToString();
                    //string sFrenchBodyContent = MyReader["DocumentBody"].ToString();
                    txb_Week.Text = sDueWeek;
                    txb_Title.Text = sTitle;
                }
         }
         catch (Exception ex)
         {
                Response.Write(ex.Message);
         }
     }
}

4 个答案:

答案 0 :(得分:1)

更改查询,如下所示

 "SELECT  d1.*, d2.* FROM KFM.dbo.ToolBoxDocContent as d1, KFM.dbo.ToolBoxDocument as d2
           where d1.DocumentId = d2.DocumentId and  d2.DocumentId ='" + _id + "'";

答案 1 :(得分:1)

在您的查询中,您还在和之后输入了= sign。

试试此代码

string sqlCommand = "SELECT d1.*, d2.* FROM KFM.dbo.ToolBoxDocContent d1 inner join KFM.dbo.ToolBoxDocument as d2 on d1.DocumentId = d2.DocumentId " + " where d2.DocumentId = " + _id;

最好编写存储过程并从c#代码调用。

答案 2 :(得分:1)

private void GetDataByID(string _id)
{
    string connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
    string sqlCommand = "SELECT  d1.*, d2.* "
                 + " FROM KFM.dbo.ToolBoxDocContent as d1"
                 + " INNER JOIN KFM.dbo.ToolBoxDocument as d2 ON d1.DocumentId = d2.DocumentId" 
                 + " WHERE d2.DocumentId = @ID";    

    using (SqlConnection connection = new SqlConnection(connectionString))
    using (SqlCommand cmd = new SqlCommand(sqlCommand, connection))
    {
        cmd.Parameter.Add("@ID", SqlDbType.Int).Value = int.Parse(_id);
        try
        {
            connection.Open();
            using (SqlDataReader MyReader = cmd.ExecuteReader()_
            {
                while (MyReader.Read())
                {
                    string sDueWeek = MyReader["DueWeek"].ToString();
                    string sTitle = MyReader["DocumentTitle"].ToString();
                    //string sEnglishBodyContent = MyReader["DocumentBody"].ToString();
                    //string sFrenchBodyContent = MyReader["DocumentBody"].ToString();
                    txb_Week.Text = sDueWeek;
                    txb_Title.Text = sTitle;
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
}

答案 3 :(得分:-1)

我在您的查询中注意到的一件事是

  1. 使用'+'进行两次连接之前未正确提供空格。在where
  2. 之前使用空格
  3. where子句的语法不正确。在and
  4. and = d2.DocumentId = " + _id 之后删除额外的'='

    您的最终查询将如下所示:

    string sqlCommand = "SELECT  d1.*, d2.* FROM KFM.dbo.ToolBoxDocContent as d1, KFM.dbo.ToolBoxDocument as d2" +
                       " where d1.DocumentId = d2.DocumentId and d2.DocumentId =" + _id;
    

    <强>更新

    string sqlCommand = "SELECT  d1.*, d2.* FROM KFM.dbo.ToolBoxDocContent as d1, KFM.dbo.ToolBoxDocument as d2" +
                       " where d1.DocumentId = d2.DocumentId and d2.DocumentId = '" + _id + "'";