简体   繁体   中英

How to insert an integer into a database through command prompt

I am trying to insert a integer into a database in C# using the code below, but everytime I run the compiler informs me that my integer is not a valid column "Invalid Column Name UserID"

Does anyone have any insight on this? Thanks.

Console.WriteLine("Please enter a new User Id");
        string line = Console.ReadLine();
        int UserID;
        if (int.TryParse(line, out UserID))
        {
            Console.WriteLine(UserID);
            Console.ReadLine();
        }



        //Prepare the command string
        string insertString = @"INSERT INTO tb_User(ID,f_Name, l_Name) VALUES (UserID,'Ted','Turner')";

First things first, I would get into the habit of using parameterised queries, if you are not planning to use stored procedures. In your example, I would:

using (var command = new SqlCommand("INSERT INTO tb_User(ID, f_Name, l_Name) VALUES (@id, @forename, @surname)", conn))
{
  command.Parameters.AddWithValue("id", id);
  command.Parameters.AddWithValue("forename", forename);
  command.Parameters.AddWithValue("surname", surname);

  command.ExecuteNonQuery();
}

Where id , forename , surname are the appropriate variables. Notice I am also using using blocks, this ensures that my objects are cleaned up after it has completed.

it is because the 'UserID' within your insertString : ..."VALUES (UserID"... is invalid.

you need to pass a value for the UserID such as: ..."VALUES ('myUserIDGoesHere'"...

Your string is not dynamically reading the variables. Use something like this:

string insertString = string.Format(@"INSERT INTO tb_User(ID,f_Name, l_Name) VALUES ({0},'{1}','{2}')", UserId, "Ted", "Turner");

There are better ways depending on what kind of data access you're using, but this is just to make the point of how to correct the string.

To answer your question regardless of your approach, try:

string insertString = @"INSERT INTO tb_User(ID,f_Name, l_Name) VALUES ("
+ UserID + ",'Ted','Turner')";

The problem is the first argument in VALUES - it simply isn't defined. If this is meant to be the value the user has entered, then you need to add a parameter to the command and use that parameter in the SQL; for example:

cmd.Parameters.AddWithValue("@id", UserID);

An then use

VALUES(@id, ...

in the TSQL.

Also, generally you might want to have the system generate the unique id. A the simplest level this could have an IDENTITY defined (an automatic sequence).

Use a parameterized query:

using (var connection = new SqlConnection(connectionString))
{
    connection.Open();

    using (var insertCommand = new SqlCommand(
        @"INSERT INTO tb_User (ID, f_Name, l_Name)
          VALUES (@ID, 'Ted', 'Turner')", connection))
    {
        insertCommand.Parameters.AddWithValue("@ID", userID);
        insertCommand.ExecuteNonQuery();
    }
}

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