我有一个包含评论的 .CSV 文件,我将以编程方式将内容从 FileUpload 控件传输到 SQL 数据库。我遇到的问题是我的控件正在工作,但我没有看到我的数据库中填充了任何数据。这是我的代码:
标记
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Button ID="Button1" runat="server"onclick="Button1_Click" Text="Button" />
<asp:Label ID="Label1" runat="server"></asp:Label>
Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data.SqlTypes;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileExt == ".csv")
{
OleDbConnection oconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + FileUpload1 + "; Extended Properties=Excel 8.0");
try
{
OleDbCommand ocmd = new OleDbCommand("SELECT * FROM [AudioPR$]", oconn);
oconn.Open();
OleDbDataReader odr = ocmd.ExecuteReader();
string Device = "";
string Source = "";
string Reviewer = "";
string Datetime = "";
string Links = "";
string Content = "";
string Subject = "";
while (odr.Read())
{
Device = valid(odr, 0);
Source = valid(odr, 1);
Reviewer = valid(odr, 2);
Datetime = valid(odr, 3);
Links = valid(odr, 4);
Content = valid(odr, 5);
Subject = valid(odr, 6);
InsertDataIntoSql(Device, Source, Reviewer, Datetime, Links, Content, Subject);
}
oconn.Close();
}
catch (Exception ee)
{
Label1.Text = ee.Message;
Label1.ForeColor = System.Drawing.Color.Red;
}
finally
{
Label1.Text = "Data Inserted Successfully";
Label1.ForeColor = System.Drawing.Color.Green;
}
}
else
{
Label1.Text = "Only .csv files allowed!";
}
}
else
{
Label1.Text = "You have not specified a file!";
}
}
protected string valid(OleDbDataReader myreader, int stval)
{
object val = myreader[stval];
if (val != DBNull.Value)
{
return val.ToString();
}
else
{
return Convert.ToString(0);
}
}
public void InsertDataIntoSql(string Device, string Source, string Reviewer, string Datetime, string Links, string Content, string Subject)
{
SqlConnection conn = new SqlConnection("Data Source=CI0000000879107\\BENSON; Initial Catalog=PRClips Mail;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO AudioPR(Device, Source, Reviewer, Datetime, Links, Content, Subject) VALUES(@Device, @Source, @Reviewer, @Datetime, @Links, @Content, @Subject)";
cmd.Parameters.Add("@Device", System.Data.SqlDbType.NVarChar).Value = Device;
cmd.Parameters.Add("@Source", System.Data.SqlDbType.NVarChar).Value = Source;
cmd.Parameters.Add("@Reviewer", System.Data.SqlDbType.NVarChar).Value = Reviewer;
cmd.Parameters.Add("@Datetime", System.Data.SqlDbType.Date).Value = Datetime;
cmd.Parameters.Add("@Links", System.Data.SqlDbType.NVarChar).Value = Links;
cmd.Parameters.Add("@Content", System.Data.SqlDbType.NVarChar).Value = Content;
cmd.Parameters.Add("@Subject", System.Data.SqlDbType.NVarChar).Value = Subject;
cmd.CommandType = System.Data.CommandType.Text;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
我意识到,当我可以从 FileUpload 中读取内容时,建立连接有点毫无意义,但是如果有人知道我做错了什么或者可以指导正确的方向,我将不胜感激。谢谢你的时间