Invalid column name sql error

后端 未结 11 1217
梦毁少年i
梦毁少年i 2020-11-30 03:16

I am trying to enter data into my database, but it is giving me the following error:

Invalid column name

Here\'s my code

<
11条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 03:57

    Always try to use parametrized sql query to keep safe from malicious occurrence, so you could rearrange you code as below:

    Also make sure that your table has column name matches to Name, PhoneNo ,Address.

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand cmd = new SqlCommand("INSERT INTO Data (Name, PhoneNo, Address) VALUES (@Name, @PhoneNo, @Address)");
        cmd.CommandType = CommandType.Text;
        cmd.Connection = connection;
        cmd.Parameters.AddWithValue("@Name", txtName.Text);
        cmd.Parameters.AddWithValue("@PhoneNo", txtPhone.Text);
        cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
        connection.Open();
        cmd.ExecuteNonQuery();
    }
    

提交回复
热议问题