简体   繁体   中英

SQL CE Insert Query works only outside of C#

I am trying to insert a row in my User table (SQL Server Compact 3.5). This is my query (LastName, FirstName and UserPassword are NVARCHAR, IsActive is BIT):

INSERT INTO Users (LastName, FirstName, IsActive, UserPassword) VALUES ('J', 'R', 1, 'T')

I converted this to the following code block in C#:

string returnQuery = "INSERT INTO Users (LastName, FirstName, IsActive, UserPassword)"
                                   + "VALUES(" + "'" + "@LastName" + "', '" + "@FirstName" + "', @IsActive" + ",'" + "@UserPassword" + "'";

        SqlCeCommand returnQueryCommand =
            new SqlCeCommand(returnQuery, connection) { CommandType = CommandType.Text };
        returnQueryCommand.Parameters.AddWithValue("@LastName", newUser.LastName);
        returnQueryCommand.Parameters.AddWithValue("@FirstName", newUser.FirstName);
        returnQueryCommand.Parameters.AddWithValue("@IsActive", newUser.IsActive);
        returnQueryCommand.Parameters.AddWithValue("@UserPassword", newUser.UserPassword);

I get a parsing error when running my code. However, when I run the query straight through CompactView, the row gets inserted just fine.

I am executed the query in another method (I am returning SqlCeCommand from this method).

Thoughts?

值后缺少右括号

您的给定代码似乎缺少用于关闭“ VALUES”数据的右括号。

string returnQuery = "INSERT INTO Users (LastName, FirstName, IsActive, UserPassword)" + "VALUES(" + "'" + "@LastName" + "', '" + "@FirstName" + "', @IsActive" + ",'" + "@UserPassword" + "')";

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