2
I have a problem when I insert data from excel file into the database because a field that I am entering says it is not of the type time
and does not allow inserting , but in the excel file the text that is written there is only hours or type time
.
Here’s a print of the excel field I’m talking about:
Here is the excel table configuration:
This is the code I use to write the data in the sql table:
string ConecçãoDB = ConfigurationManager.ConnectionStrings["ConecçaoDB"].ConnectionString;
string Table = ConfigurationManager.AppSettings["table"];
string ssqltable = Table;
string ssqlconnectionstring = ConecçãoDB;
string sclearsql = "delete from " + ssqltable;
var connectionString ="Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+filename+";Extended Properties='Excel 8.0;HDR=YES'";
// var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 12.0;IMEX=0;HDR=YES;TypeGuessRows=0;ImportMixedTypes=Text\"";
var sql = "SELECT * FROM [" + comboBox1.Text + "$]";
SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring);
SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn);
sqlconn.Open();
sqlcmd.ExecuteNonQuery();
sqlconn.Close();
MessageBox.Show(sql);
OleDbConnection oledbconn = new OleDbConnection(connectionString);
OleDbCommand oledbcmd = new OleDbCommand(sql, oledbconn);
oledbconn.Open();
using (OleDbDataReader dr = oledbcmd.ExecuteReader())
{
using (SqlBulkCopy bulkcopy = new SqlBulkCopy(ssqlconnectionstring))
{
bulkcopy.DestinationTableName = ssqltable;
bulkcopy.ColumnMappings.Add(comboBox2.Text, "TransDate");
bulkcopy.ColumnMappings.Add(comboBox3.Text, "TransTime");
bulkcopy.ColumnMappings.Add(comboBox4.Text, "CardNo");
bulkcopy.ColumnMappings.Add(comboBox5.Text, "VoucherNo");
bulkcopy.ColumnMappings.Add(comboBox6.Text, "Quantity");
bulkcopy.ColumnMappings.Add(comboBox7.Text, "TransactionValue");
bulkcopy.WriteToServer(dr);
}
}
oledbconn.Close();
button2.Enabled = true;
oledbconn.Close();
This is the mistake you make :
Invalidcastexception: Invalid conversion of 'System.Datetime' into 'System.Timespan'.
Tell me about the contents of: comboBox2 and comboBox3
– Thiago Loureiro
@Thiagoloureiro has the names of the excel fields
– Pedro Azevedo
The problem is the following, the TIME column in sql, if I’m not mistaken, is actually a timespan (in milliseconds). What I would do is create a varchar column, and store the value. Or an int column, which stores the time value without formatting.
– Grupo CDS Informática
@Pedroazevedo puts the error stacktrace please
– Rovann Linhalis