I'm writing a little app to get data from a CSV file generated everyday by a vendor and insert it into a Table in our SQL Server database. I can get the data fine, and I can easily merge it with another dataset I pull from the existing table (which is exactly the same except for an Identity Column as PK).
When I call SqlDataAdapter.Update, no error is generated, but it makes no changes as well. I have one row present in the Database and can use DataAdapter.Update to delete it just fine. It just won't insert.
Please Help! Code below:
- using (OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\MyPath\\;Extended Properties='text;HDR=Yes;FMT=Delimited'"))
-
{
-
string sql = "SELECT * from MyCSV.csv";
-
OleDbDataAdapter csvRdr = new OleDbDataAdapter(sql, cn);
-
DataSet ds = new DataSet();
-
-
cn.Open();
-
-
try
-
{
-
-
csvRdr.Fill(ds, "Table");
-
cn.Close();
-
using (SqlConnection cnn = new SqlConnection("Data Source=DB;Initial Catalog=XX;User ID=usr;Password=pwd"))
-
{
-
string sql_2 = "Select * from DBTable";
-
SqlDataAdapter da = new SqlDataAdapter(sql_2, cnn);
-
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
-
DataSet ds2 = new DataSet();
-
da.TableMappings.Add("DBTable", "Table");
-
Console.WriteLine(sql_2);
-
cnn.Open();
-
da.Fill(ds2, "Table");
-
ds2.Merge(ds.Tables["Table"]);
-
ds2.AcceptChanges();
-
//This works ds2.Tables["Table"].Rows[0].Delete();
-
//Looks Fine Console.Write(ds2.GetXml());
-
da.Update(ds2, "Table");
-
cnn.Close();
-
}
-
}
-
)
Thanks.
-Mike