string[] content = System.IO.File.ReadAllLines(e.FullPath);
foreach (string line in content)
{
string[] arr;
arr = line.Split(','); //splitting the line which was read by the stream reader object
DataRow row = table.NewRow();
row["L_DateTime"] = DateTime.Parse(arr[0].Substring(0, 4) + "-" + arr[0].Substring(4, 2) + "-" + arr[0].Substring(6));
row["L_Direction"] = arr[1];
row["L_CardID"] = arr[2];
row["L_GateID"] = Convert.ToInt32(arr[3]);
table.Rows.Add(row);
i2++;
}
//Connect to DB
string conString = ConfigurationManager.ConnectionStrings["Data Source=HEIZ-PC\\SQLEXPRESSR2;Initial Catalog=attandent;User ID=sa;Password=pwd"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
try
{
//Execute the command to make a temp table
SqlCommand cmd = new SqlCommand(tmpTable, con);
cmd.ExecuteNonQuery();
//BulkCopy the data in the DataTable to the temp table
using (SqlBulkCopy bulk = new SqlBulkCopy(con))
{
bulk.DestinationTableName = "#templogDetail";
bulk.WriteToServer(table);
}
//Now use the merge command to upsert from the temp table to the production table
string mergeSql = "merge into logDetail as Target using #templogDetail as Source on" +
"Target.L_DateTime=Source.L_DateTime, Target.L_Direction=Source.L_Direction," +
"Target.L_CardID=Source.L_CardID, when matched then update set Target.L_GateID=Source.L_GateID," +
"when not matched then insert (L_DateTime,L_Direction,L_CardID,L_GateID)" +
"values (Source.L_DateTime,Source.L_Direction,Source.L_CardID,Source.L_GateID);";
cmd.CommandText = mergeSql;
cmd.ExecuteNonQuery();
//Clean up the temp table
cmd.CommandText = "drop table #templogDetail";
cmd.ExecuteNonQuery();
}
hi...greeting coder...
im totally noob in c#....has error with this :
Object reference not set to an instance of an object the error stated at the this line.
string conString = ConfigurationManager.ConnectionStrings["Data Source=HEIZ-PC\\SQLEXPRESSR2;Initial Catalog=attandent;User ID=sa;Password=pwd"].ConnectionString; what is wrong with my connection string?? any advice would be my pleasure...thanks in advance..