我正在尝试在上传文件中注入值。我是 C# .net 的新手,我在 Google 上搜索了很多以找到答案,但找不到任何可以为我提供所需帮助的答案。我从数据库中获取值没有问题,但是当我尝试插入它时,它总是返回 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.";
}
}
}