Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Data.SqlClient.SqlException:列名无效(在命令行.ExecuteOnQuery();)处出错)_C#_Sql_Sql Server_Connection - Fatal编程技术网

C# System.Data.SqlClient.SqlException:列名无效(在命令行.ExecuteOnQuery();)处出错)

C# System.Data.SqlClient.SqlException:列名无效(在命令行.ExecuteOnQuery();)处出错),c#,sql,sql-server,connection,C#,Sql,Sql Server,Connection,所以我一直在犯这个错误。我不确定问题是否在SQL server中。此代码用于将数据插入数据库 发送帮助 试试看 string queryReg = "INSERT INTO uporabnik2(uporabnisko_ime, geslo, email) " + "VALUES('" + RegistracijaUporabnisko + "', '" + RegistracijaGeslo + "', '" + RegistracijaMail +

所以我一直在犯这个错误。我不确定问题是否在SQL server中。此代码用于将数据插入数据库

发送帮助

试试看

string queryReg = "INSERT INTO uporabnik2(uporabnisko_ime, geslo, email) " +
                     "VALUES('" + RegistracijaUporabnisko + "', '" + RegistracijaGeslo + "', '" + RegistracijaMail + "')";

通常,您只需要将字符串类型数据的值括起来,但是为了更安全起见,请始终将值括在insert语句中的单引号中。参数(如下)修复了这一问题和一系列其他问题,包括SQL注入、i18n/l10n等。也有可能您只是键入了一个列名,在这种情况下,我们不能帮你,因为我们不知道真名

string queryReg = "INSERT INTO uporabnik2(uporabnisko_ime, geslo, email) " +
     "VALUES(@uporabnisko_ime, @geslo, @email)";

using (SqlCommand command = new SqlCommand(queryReg, con))
{
    con.Open();
    command.Parameters.AddWithValue("@uporabnisko_ime", RegistracijaUporabnisko_txt.Text);
    command.Parameters.AddWithValue("@geslo", RegistracijaGeslo_txt.Text);
    command.Parameters.AddWithValue("@email", RegistracijaMail_txt.Text);
    con.Close();
}
我也不厌其烦地推荐像Dapper这样的工具:

con.Execute("INSERT INTO uporabnik2(uporabnisko_ime, geslo, email) " +
     "VALUES(@uporabnisko_ime, @geslo, @email)", new {
    uporabnisko_ime = RegistracijaUporabnisko_txt.Text,
    geslo = RegistracijaGeslo_txt.Text,
    email = RegistracijaMail_txt.Text });

它执行所有操作,包括(如有必要)连接打开/关闭、命令构造、参数打包等。

让我们看看您的代码:

      string queryReg = "INSERT INTO uporabnik2(uporabnisko_ime, geslo, email) " +
             "VALUES(" + RegistracijaUporabnisko + ", " + RegistracijaGeslo + ", " + RegistracijaMail + ")";

        using (SqlCommand command = new SqlCommand(queryReg, con))
        {
            con.Open();
            command.ExecuteNonQuery();
            con.Close();
        }
好的,那么如果
registicijauporabnisko
的值为Rok,
registicijageslo
的值为Rok,
registicijamail
的值为RokRok@home.. 你的绳子现在看起来像什么?嗯

 string queryReg = "INSERT INTO uporabnik2(uporabnisko_ime, geslo, email) VALUES(Rok,Rok,Rok@home)";
然后它将寻找的Rok是一个字段,而不是一个值。因此它表示无效列

那么,如果你用一种普遍采用的

  string queryReg = "INSERT INTO uporabnik2(uporabnisko_ime, geslo, email) VALUES(@RegistracijaUporabnisko ,@RegistracijaGeslo ,@email)";

    using (SqlCommand command = new SqlCommand(queryReg, con))
    {
        command.Parameters.AddWithValue("@RegistracijaUporabnisko ", RegistracijaUporabnisko );
        command.Parameters.AddWithValue("@RegistracijaGeslo ", RegistracijaGeslo );
        command.Parameters.AddWithValue("@email", email);
        con.Open();
        command.ExecuteNonQuery();
        con.Close();
    }

现在发生了什么?好的,幕后的文本是用引号括起来的,日期是以适当的格式发送的,数字等等。。替你处理。它可以保护您免受注射,因此如果我输入了
"; 放下表格uporabnik2
您不会发现自己丢失了表格等。

错误消息是否足够清楚?它表示列名
geslo
无效/未在表
uporabnik2
中找到,请交叉检查您的表名/列名,并将错误添加为文本无图像。请包括表字段列表,因为错误本身似乎非常清楚和明确。。“rok”是您的变量值之一吗?您还应该仔细阅读,以帮助避免SQL注入vulnerabilities@sujithkarivelil没有人喜欢有一堆文本的图片,也不是所有的图片站点都可以让我们去寻找“然而,为了更安全的一面,总是在插入语句中的单个引号中包含值”——哦,天哪,不是;这就是你被黑客攻击的方式。答案永远是参数。没有有趣的异常(不管怎样,在例程代码中)。非常感谢!这对我很有用,我学到了很多!非常感谢你!这对我很有用,我学到了很多@RokŠekoranja Take a了解当帖子回答您的问题时,您如何通过单击答案附近的复选标记来接受答案。
  string queryReg = "INSERT INTO uporabnik2(uporabnisko_ime, geslo, email) VALUES(@RegistracijaUporabnisko ,@RegistracijaGeslo ,@email)";

    using (SqlCommand command = new SqlCommand(queryReg, con))
    {
        command.Parameters.AddWithValue("@RegistracijaUporabnisko ", RegistracijaUporabnisko );
        command.Parameters.AddWithValue("@RegistracijaGeslo ", RegistracijaGeslo );
        command.Parameters.AddWithValue("@email", email);
        con.Open();
        command.ExecuteNonQuery();
        con.Close();
    }