简体   繁体   中英

Incorrect syntax near keyword 'd1'

The SQL query works in SQL Server Management Studio. But, in Visual studio it gives an error

Incorrect syntax near D1

Code:

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);
         }
     }
}

Change the query as shown below

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

In your query, you also have entered = sign after and.

Try this code

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;

Also it's better to write store procedure instead and call from your c# code.

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);
        }
    }
}

One thing I have noticed in your query is that

  1. space is not provided properly before two joins using '+'. Use space before where
  2. incorrect syntax at where clause. remove extra '=' after and at and = d2.DocumentId = " + _id

Your final query will look like as mentioned below:

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;

Update:

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 + "'";

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