【问题标题】:Writing data to SQL database from ASP.NET textbox从 ASP.NET 文本框将数据写入 SQL 数据库
【发布时间】:2016-09-01 22:56:17
【问题描述】:

写入 SQL 数据库时,我收到的是“System.Web.UI.WebControls.TextBox”,而不是实际数据本身。

upload.aspx.cs 文件(包含查询):

 string query = "INSERT INTO reports (birdname, location, details, image, spotteddata, uploaddata, typeofbird) VALUES ('"+birdnametext+"', 'mygarden', 'some details about how long you waited', ' " + img + "', '10th March 2014','" + dateNow + "', '2')";

upload.aspx(包含文本框):

<header> Upload </header>
<p> Please fill out the form below to put your item up for sale</p>
<p>  
<span>Name of Bird:
<asp:TextBox ID="birdnametext" runat="server"></asp:TextBox> </span>
<br/>
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Image ID="Image1" runat="server" />
<br />

【问题讨论】:

标签: c# mysql asp.net sql-server database


【解决方案1】:

它们可能是你做错的事情:

  1. 您正在尝试将 TextBox 本身传递给数据库,您需要将其传递给 Text。这意味着...'"+ birdnametext + "' ... 应该是...'"+ birdnametext.Text + "' ...
  2. 您正在通过文本查询为injection 敞开大门,请改用参数化查询。

你可以像下面这样构建命令:

string query = "INSERT INTO reports(birdname, location) VALUES(@birdname, @location);
SqlCommand cmd = new SqlCommand("query,con);
cmd.Parameters.Add("@birdname", SqlDbType.VarChar).Value = birdnametext.Text;
cmd.Parameters.Add("@location", SqlDbType.VarChar).Value = "mygarden";
// similarly you can add the rest of columns and parameters 
cmd.ExecuteNonQuery();

【讨论】:

    【解决方案2】:

    您需要使用 TextBox 的 Text 属性来访问其内容:

    ... + birdnametext.Text + ...
    

    参数化,而不是串联

    此外,在构建查询时,您不希望使用字符串连接,因为这会使您容易受到 SQL 注入和语法错误等问题的影响。更好的方法是使用参数化,如下所示:

    using(var connection = new SqlConnection("{your-connection-string}"))
    {
         // Notice the use of parameters
         var query = "INSERT INTO reports (birdname, location, details, image, spotteddata, uploaddata, typeofbird) VALUES (@birdname, @location', @details, ' @uploadData, @someDate, @now, @x)";
         using(var command = new SqlCommand(query, connection))
         {
              connection.Open();
              // Read the bytes of your image here and store in a byte[]
              var imageData = File.ReadAllBytes(Image1.ImageUrl);
              // Add your parameters
              command.Parameters.AddWithValue("@birdName",birdnametext.Text);
              command.Parameters.AddWithValue("@location","mygarden");
              command.Parameters.AddWithValue("@details","some details about how long you waited");
              command.Parameters.AddWithValue("@uploadData",imageData);
              command.Parameters.AddWithValue("@someDate","10th March 2014");
              command.Parameters.AddWithValue("@now",DateTime.Now);        
              command.Parameters.AddWithValue("@x",2);  
              // Execute your query
              command.ExecuteNonQuery();
         }
    }
    

    【讨论】:

      【解决方案3】:

      在您的 sql 语句中将birdnametext 更改为birdnametext.text

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-20
        • 1970-01-01
        • 1970-01-01
        • 2014-11-21
        • 1970-01-01
        • 2013-12-28
        • 2021-06-15
        • 1970-01-01
        相关资源
        最近更新 更多