I'm trying to import an MDB file, read this file and write to SQL. I am already able to import the file and read, I just can not even get this data and write to SQL.
If you notice the code I even managed to show the data in a List.
Here is my code below:
protected void Btn_Importar(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
if (FileUpload1.PostedFile.ContentType == "application/msaccess")
{
string filename = Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/Upload/") + filename);
Label1.Text = "File uploaded successfully!";
//ReadMdb();
Insert();
}
}
}
public void Insert()
{
string strFile = Server.MapPath("~/Upload/teste.mdb");
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFile;
var myDataTable = new DataTable();
using (var connection = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;" + "data source=" + strFile))
{
connection.Open();
var query = "SELECT * FROM BOLETO";
var command = new OleDbCommand(query, connection);
var reader = command.ExecuteReader();
Conexaocs con = new Conexaocs();
con.Conexao();
SqlCommand sqlComm = new SqlCommand("INSERT INTO BOLETO (CODIGO,NF_CONTA,TEXTO) VALUES (@CODIGO, @NF_CONTA, @TEXTO)", conn);
//if (reader.HasRows)
//{
// while (reader.Read())
// {
// //ListBox1.Items.Add(reader.GetInt32(0).ToString() + " - " + reader.GetString(1));
// }
for (int i = 0; i < reader.FieldCount; i++)
{
sqlComm.Parameters.AddWithValue("@CODIGO", reader[i]);
sqlComm.Parameters.AddWithValue("@NF_CONTA", reader[i]);
sqlComm.Parameters.AddWithValue("@TEXTO", reader[i]);
sqlComm.ExecuteNonQuery();
}
connection.Close();
}
}
I just put these columns to test.