简体   繁体   English

如何通过命令提示符将整数插入数据库

[英]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" 我正在尝试使用以下代码在C#中将整数插入数据库中,但是每次运行编译器时,都会通知我我的整数不是有效的列“无效的列名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. 其中idforenamesurname是适当的变量。 Notice I am also using using blocks, this ensures that my objects are cleaned up after it has completed. 注意,我也在使用using块,这可以确保对象完成后被清理。

it is because the 'UserID' within your insertString : ..."VALUES (UserID"... is invalid. 这是因为insertString中的'UserID':...“ VALUES(UserID” ...)无效。

you need to pass a value for the UserID such as: ..."VALUES ('myUserIDGoesHere'"... 您需要为UserID传递一个值,例如:...“ 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"); 字符串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. 问题是VALUES的第一个参数-只是没有定义。 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; 如果这是用户输入的值,则需要在命令中添加一个参数 ,然后在SQL中使用该参数。 for example: 例如:

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

An then use 然后使用

VALUES(@id, ...

in the TSQL. 在TSQL中。

Also, generally you might want to have the system generate the unique id. 另外, 通常您可能希望系统生成唯一的ID。 A the simplest level this could have an IDENTITY defined (an automatic sequence). 最简单的级别可能是定义了IDENTITY (自动序列)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM