2013-02-12 90 views
0

我正在尝试在上传文件中注入值。我是C#.net的新手,我搜索了很多东西以找到答案,但无法找到任何能够提供我所需帮助的东西。我没有从数据库获取值的问题,但是当我尝试插入它时,总是返回false。我不知道还有什么要做。插入查询总是返回false

我在每一行nr都使用了断点,但我真的需要这个帮助。

正如我所说的从数据库获取值没有问题,所以我连接,但插入总是返回false。也许这个问题对于你的大人来说更清晰:)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Data.SqlClient; 
using System.Xml; 
using System.Xml.Linq; 
using System.IO; 

public partial class Extra : System.Web.UI.Page 
{ 

    public int count = 0; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     Label1.Text = "Hello choose a file to upload"; 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 

     if (FileUpload1.HasFile) 
     { 
      try 
      { 
       if (FileUpload1.PostedFile.ContentLength > 2000000) 
       { 
        Label1.Text = "File is to big"; 
       } 
       else 
       { 
        FileUpload1.SaveAs("C:\\Uploads\\" + FileUpload1.FileName); 
        Label1.Text = "File name: " + FileUpload1.PostedFile.FileName + "<br />" + 
         FileUpload1.PostedFile.ContentLength + "kb<br />" + 
         FileUpload1.PostedFile.ContentType; 

        try 
        { 
         //open connection 
         SqlConnection con = new SqlConnection("Data Source='localhost';Initial Catalog=webDB;Trusted_Connection=true;Integrated Security=SSPI"); 
         //con.Database = "";Data Source=JOHANNES-TOSH;Initial Catalog=webDB;Integrated Security=True 
         con.Open(); 

         SqlCommand cmd = new SqlCommand(); 
         cmd.Parameters.Add("@name", SqlDbType.NVarChar, 150); 
         cmd.Parameters["@name"].Value = FileUpload1.PostedFile.FileName; 
         cmd.Parameters.Add("@type", SqlDbType.NVarChar, 150); 
         cmd.Parameters["@type"].Value = FileUpload1.PostedFile.ContentType; 
         cmd.Parameters.Add("@size", SqlDbType.Int, 11); 
         cmd.Parameters["@size"].Value = FileUpload1.PostedFile.ContentLength; 
         cmd.CommandText = "INSERT INTO [CORE.uploads] (name, type, size) VALUES (@name, @type, @size)"; 
         //cmd.CommandType = CommandType.TableDirect; 
         cmd.Connection = con; 
         cmd.ExecuteNonQuery(); 

         con.Close(); 
        } 
        catch (SqlException err) { 
         Response.Write("<br />"); 
         Response.Write(err.Message.ToString()); 
         Response.Write(err.LineNumber); 
        } 
       } 

      } 
      catch (Exception ex) 
      { 
       Label1.Text = ex.Message.ToString(); 
      } 
     } 
     else 
     { 
      Label1.Text = "You have not specified a file."; 
     } 
    } 

} 
+6

哪条线返回false? – 2013-02-12 19:39:22

+0

什么是这个布尔val1 =“东西...”;回复于(VAL1);在那里做?这总是错误的。 – 2013-02-12 19:40:48

+1

您的代码存在许多问题:您的SqlConnection,SqlCommand,SqlDataReader等不在“使用”块中。另外,你使用'err.Message'而不是'err.ToString()',而'err.Message'已经是一个字符串了,所以不需要'.ToString()'。 – 2013-02-12 19:50:09

回答

1

你并不需要为这种情况指定参数类型的大小。试试这种方式。 如果这不起作用,请检查您的表名称和列名以确保它是相同的。另外,请检查您的其他代码。 希望得到这个帮助。

SqlCommand cmd = new SqlCommand(); 
cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = FileUpload1.PostedFile.FileName; 
cmd.Parameters.Add("@type", SqlDbType.NVarChar).Value = FileUpload1.PostedFile.ContentType; 
cmd.Parameters.Add("@size", SqlDbType.Int).Value = FileUpload1.PostedFile.ContentLength; 
cmd.CommandText = "INSERT INTO [CORE].[uploads] (name, type, size) VALUES (@name, @type, @size)"; 
cmd.Connection = con; 
cmd.ExecuteNonQuery(); 

编辑:此外,请确保您的连接字符串是好的。如果用数据库连接打开窗口(忘记该窗口的名称),则可以在数据库连接的属性中找到它(