Home > Blockchain >  System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'Transaction'
System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'Transaction'

Time:09-30

I Have been looking at this for 2 hours where is this incorrect syntax!?

Here is the line the error happens https://i.stack.imgur.com/08tGY.png

public static class InvestorProcessor
    {
        public static int CreateInvestment(int id, string tickerSymbol, Sale transactionType, string companyName, int quantity, int sharePrice)
        {
            Investor data = new Investor

            {
                Id = id,
                TickerSymbol = tickerSymbol,
                TransactionType = transactionType,
                CompanyName = companyName,
                Quantity = quantity,
                SharePrice = sharePrice

            };


        string sql = @"insert into DB.Transaction (TickerSymbol, TransactionType, CompanyName, Quantity, SharePrice)
                     values (@TickerSymbol, @TransactionType, @CompanyName, @Quantity, @SharePrice);";

            return SqlDataAccess.SaveData(sql, data);

        }

        public static List<Investor> LoadInvestors()
        {
            string sql = @"select Id, TickerSymbol, TransactionType, CompanyName, Quantity, SharePrice
                           from DB.Transaction;";
            return SqlDataAccess.LoadData<Investor>(sql);
        }
    }
} 

CodePudding user response:

TRANSACTION is a T-SQL reserved keyword. If you want to have it as an object name (and generally you should avoid using reserved keywords as object names), as the docs say (my emphasis):

Although it is syntactically possible to use SQL Server reserved keywords as identifiers and object names in Transact-SQL scripts, you can do this only by using delimited identifiers.

This fails, with the same error you are getting:

create table transaction ( id int )

this succeeds:

create table [transaction] ( id int )

CodePudding user response:

You should try without DB. and check data types:

 string sql = @"insert into Transaction (TickerSymbol, TransactionType, CompanyName, Quantity, SharePrice)
                values (@TickerSymbol, @TransactionType, @CompanyName, @Quantity, @SharePrice);";

If the error persists, try to take the query into SQL Server Management Studio. You have more details there.

  • Related