数据库方法的语法不正确

时间:2016-10-05 22:58:48

标签: c# sql-server

我正在尝试实现一种GetProduct方法,让我可以检索产品的产品代码。我正在使用具有我的产品表的数据库文件。但是当我跑步时,我得到一条消息说

  

Product

附近的语法不正确

我不能为我的生活理解为什么它不起作用。有什么想法吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace ProductMaintenance
{
    class ProductDB
    {
        static Product product = new Product();

        public static Product GetProduct(string code)
        {
            SqlConnection connection = MMABooksDB.GetConnection();

            string select = "SELECT ProductCode, Description, UnitPrice"
                          + "FROM Products"
                          + "WHERE ProductCode = @ProductCode";

            SqlCommand selectCommand = new SqlCommand(select, connection);
            selectCommand.Parameters.AddWithValue("@ProductCode", code);

            try
            {
                connection.Open();

                SqlDataReader prodReader = selectCommand.ExecuteReader(CommandBehavior.SingleRow);

                if (prodReader.Read())
                {
                    product.Code = prodReader["ProductCode"].ToString(); ;
                    product.Description = prodReader["Description"].ToString();
                    product.Price = ((decimal)prodReader["Price"]);

                    return product;
                }
                else
                {
                    return null;
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                connection.Close();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

由此生成的sql缺少段之间的空格

void merge(void *base, size_t num, size_t el_size, size_t size,
           int (*compar)(const void*, const void*)) {
    size_t split = size * el_size;
    char *first = malloc(split);
    char *p1 = memcpy(first, base, split);
    char *dest = base, *p2 = dest + split;
    size_t i = 0, j = size;
    while (i < size) {
        if (j == num || compar(p1, p2) <= 0) {
            for (size_t k = 0; k < el_size; k++)
                *dest++ = *p1++;
            i++;
        } else {
            for (size_t k = 0; k < el_size; k++)
                *dest++ = *p2++;
            j++;
        }
    }
    free(first);
}

void merge_sort(void *base, size_t num, size_t el_size,
                int (*compar)(const void*, const void*)) {
    if (num > 1) {
        size_t s = (num + 1) / 2;
        char *char_base = base;
        merge_sort(char_base, s, el_size, compar);
        merge_sort(char_base + s * el_size, num - s, el_size, compar);
        merge(char_base, num, el_size, s, compar);
    }
}

更改为:

string select = "SELECT ProductCode, Description, UnitPrice"
           + "FROM Products"
           + "WHERE ProductCode = @ProductCode";

更好的还是:

                                                //        add space
                                                //         ↓
string select = "SELECT ProductCode, Description, UnitPrice "
           + "FROM Products "
           + "WHERE ProductCode = @ProductCode";