アップロードファイルに値を挿入しようとしています。私はC#.netを初めて使用し、答えを見つけるためにたくさんグーグルで検索しましたが、必要なヘルプを提供するものを見つけることができません。データベースから値を取得するのに問題はありませんが、挿入しようとすると常に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.";
}
}
}