gpt4 book ai didi

c# - 在 Asp.net C# 中使用文件上传将数据从 .csv 导入 SQL Server

转载 作者:太空宇宙 更新时间:2023-11-03 16:03:27 25 4
gpt4 key购买 nike

我有一个 .csv 文件,我通过 FileUpload 控件上传它,我想将该 csv 文件中的全部数据传输到 SQL Server 数据库表中,但我的数据库中存在一些问题代码:

 DataTable tblReadCSV = new DataTable();
tblReadCSV.Columns.Add("EmailId");
string path = System.IO.Path.GetFileName(fupEmails.PostedFile.FileName);
fupEmails.PostedFile.SaveAs(Server.MapPath("~/Contacts/" + path));
path = Server.MapPath("~/Contacts/" + path);
TextFieldParser csvParser = new TextFieldParser(path);
csvParser.Delimiters = new string[] { "," };
csvParser.TrimWhiteSpace = true;
//csvParser.ReadLine();
while (!csvParser.EndOfData)
{
string[] fields = csvParser.ReadFields();
tblReadCSV.Rows.Add(fields.Equals("Email"));
}
string connection = @"Data Source=ANURAG-PC; Initial Catalog=MailServer; Persist Security Info=True; User ID=sa; Password=anurag";
string strSql = "Insert into EmailData(EmailId) Values(@Email)";
SqlConnection con=new SqlConnection(connection);
SqlCommand cmd=new SqlCommand();
cmd.CommandType=CommandType.Text;
cmd.CommandText=strSql;
cmd.Connection=con;
cmd.Parameters.Add("@Email",SqlDbType.NVarChar,250,"Email");
SqlDataAdapter daAdapter=new SqlDataAdapter();
daAdapter.InsertCommand=cmd;
int result=daAdapter.Update(tblReadCSV);
lblError.Text="Send Successfully";

我在倒数第二行遇到错误

int result = daAdapter.Update(tblReadCSV);

错误是

The parameterized query '(@Email nvarchar(250))Insert into EmailData(EmailId) Values(@Ema' expects the parameter '@Email', which was not supplied.

最佳答案

cmd.Parameters.Add("@Email",SqlDbType.NVarChar,250,"EmailId");

您添加到 DataTable 的列的名称是 EmailId,而不是 Email

关于c# - 在 Asp.net C# 中使用文件上传将数据从 .csv 导入 SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20408424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com