Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据库方法的语法不正确_C#_Sql Server - Fatal编程技术网

C# 数据库方法的语法不正确

C# 数据库方法的语法不正确,c#,sql-server,C#,Sql Server,我正在尝试实现一个GetProduct方法,该方法允许我检索产品的产品代码。我使用的数据库文件包含我的products表。但当我跑步时,我收到一条信息说 产品附近的语法不正确 我一辈子都不明白它为什么不起作用。有什么想法吗 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; usi

我正在尝试实现一个
GetProduct
方法,该方法允许我检索产品的产品代码。我使用的数据库文件包含我的products表。但当我跑步时,我收到一条信息说

产品附近的语法不正确

我一辈子都不明白它为什么不起作用。有什么想法吗

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

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

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

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

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

在单价之后或之前缺少空格。为什么不使用逐字字符串@并删除所有这些+来连接字符串呢?就是这样!成功了!旁注-捕获异常只是为了重新抛出它是没有意义的。它所做的只是重置堆栈跟踪,为您提供更少的调试信息。不要捕获异常,除非您计划对其执行某些操作,然后再重新引用它。您可以使用它,因为它更可读,并且更容易在字符串中进行更改。我简直不敢相信?!成功了!这是个可怕的错误,但很高兴问题解决了。非常感谢。我一定会这么做的欢迎你。我花了一分钟才注意到这个变化,所以我想引起大家的注意。