• <tfoot id='f9bVy'></tfoot>
  • <small id='f9bVy'></small><noframes id='f9bVy'>

      <bdo id='f9bVy'></bdo><ul id='f9bVy'></ul>
  • <i id='f9bVy'><tr id='f9bVy'><dt id='f9bVy'><q id='f9bVy'><span id='f9bVy'><b id='f9bVy'><form id='f9bVy'><ins id='f9bVy'></ins><ul id='f9bVy'></ul><sub id='f9bVy'></sub></form><legend id='f9bVy'></legend><bdo id='f9bVy'><pre id='f9bVy'><center id='f9bVy'></center></pre></bdo></b><th id='f9bVy'></th></span></q></dt></tr></i><div id='f9bVy'><tfoot id='f9bVy'></tfoot><dl id='f9bVy'><fieldset id='f9bVy'></fieldset></dl></div>

      1. <legend id='f9bVy'><style id='f9bVy'><dir id='f9bVy'><q id='f9bVy'></q></dir></style></legend>
      2. SQL INSERT - 列名无效

        时间:2023-06-05

        <small id='52jI7'></small><noframes id='52jI7'>

        • <bdo id='52jI7'></bdo><ul id='52jI7'></ul>
              <tbody id='52jI7'></tbody>

          • <tfoot id='52jI7'></tfoot>
              <i id='52jI7'><tr id='52jI7'><dt id='52jI7'><q id='52jI7'><span id='52jI7'><b id='52jI7'><form id='52jI7'><ins id='52jI7'></ins><ul id='52jI7'></ul><sub id='52jI7'></sub></form><legend id='52jI7'></legend><bdo id='52jI7'><pre id='52jI7'><center id='52jI7'></center></pre></bdo></b><th id='52jI7'></th></span></q></dt></tr></i><div id='52jI7'><tfoot id='52jI7'></tfoot><dl id='52jI7'><fieldset id='52jI7'></fieldset></dl></div>

                  <legend id='52jI7'><style id='52jI7'><dir id='52jI7'><q id='52jI7'></q></dir></style></legend>
                  本文介绍了SQL INSERT - 列名无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  正如你们中的一些人可能从我之前的帖子中看到的那样,我是使用 C# 创建网站的新手(尽管我有相当多的将它用于 Windows 窗体应用程序的经验).强大的力量让我远离 PHP,但我一直在我认为的基础知识上失败.

                  As some of you may of seen from my previous post I'm new to using C# to create websites (Although I have a fair bit of experience using it for Windows Forms apps). The powers that be are tempting me away from PHP but I keep failing at what I consider the basics.

                  无论如何,这是我的问题.我正在尝试在 SQL 数据库中创建一个简单的条目.我知道我与数据库的连接很好,因为我可以整天执行 SELECT 查询,但我在使用 Insert 时遇到了问题.

                  Anyway, this is my issue. I am trying to create a simple entry into a SQL database. I know my connection to the DB is fine as I can reel off SELECT queries all day long but I'm having trouble with using Insert.

                  这是我的代码:

                  string filename = "abc123.jpg";
                  SqlConnection link = new SqlConnection(//you dont need to see my data here ;));
                  string sqlcode = "INSERT INTO file_uploads (upload_filename VALUES ("+filename+")";
                  SqlCommand sql = new SqlCommand(sqlcode,link);
                  link.open();
                  sql.ExecuteNonQuery();
                  

                  这会导致 try/catch 返回无效的列名 abc123.jpg".

                  This results in "Invalid column name abc123.jpg" returned from the try/catch.

                  任何帮助将不胜感激.(我希望他们能让我在 PHP 中做到这一点,哈哈!)

                  Any help would be appreciated. (I wish they would let me do this in PHP lol!)

                  谢谢,

                  绊脚石

                  推荐答案

                  你在列名后面缺少一个括号,值代表一个字符串,因此必须用引号引起来:

                  You are missing a parenthesis after the column name and the value represents a string and as such must be enclosed in quotes:

                  string sqlcode = "INSERT INTO file_uploads (upload_filename) " + 
                                   "VALUES ('"+filename+"')";
                  

                  但是,正确的方法是使用参数化查询:

                  However, the correct way would be to use a parameterized query:

                  string filename = "abc123.jpg";
                  SqlConnection link = new SqlConnection(/*you dont need to see my data here ;)*/);
                  string sqlcode = "INSERT INTO file_uploads (upload_filename) VALUES (@filename)";
                  SqlCommand sql = new SqlCommand(sqlcode,link);
                  sql.Parameters.AddWithValue("@filename", filename);
                  link.open();
                  sql.ExecuteNonQuery();
                  

                  这篇关于SQL INSERT - 列名无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C# 表单未将值插入 SQL Server 数据库 下一篇:自动/智能插入“本身"目的

                  相关文章

                  最新文章

                  <i id='UYtc2'><tr id='UYtc2'><dt id='UYtc2'><q id='UYtc2'><span id='UYtc2'><b id='UYtc2'><form id='UYtc2'><ins id='UYtc2'></ins><ul id='UYtc2'></ul><sub id='UYtc2'></sub></form><legend id='UYtc2'></legend><bdo id='UYtc2'><pre id='UYtc2'><center id='UYtc2'></center></pre></bdo></b><th id='UYtc2'></th></span></q></dt></tr></i><div id='UYtc2'><tfoot id='UYtc2'></tfoot><dl id='UYtc2'><fieldset id='UYtc2'></fieldset></dl></div>

                    <bdo id='UYtc2'></bdo><ul id='UYtc2'></ul>
                      <legend id='UYtc2'><style id='UYtc2'><dir id='UYtc2'><q id='UYtc2'></q></dir></style></legend>
                      <tfoot id='UYtc2'></tfoot>

                    1. <small id='UYtc2'></small><noframes id='UYtc2'>