I have an .csv file which I upload it through a FileUpload
control and I want to transfer the whole data from that csv file into a SQL Server database table, but I have some problems in my code:
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";
and I'm getting error in the second last line
int result = daAdapter.Update(tblReadCSV);
and the error is
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
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.