导入outlook联系人
protected void btn_import_Click(object sender, EventArgs e)
{
ReturnValue _result = new ReturnValue();
Transaction trans = new Transaction();
TContact_Source contactsource = new TContact_Source();
lb_importmsg.Text = "";
if (fucontact.HasFile)
{
if (fucontact.PostedFile.ContentType.ToLower() == "application/vnd.ms-excel")
{
string name = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + GlobeInfo.CustomerId + fucontact.FileName;
string allName = Server.MapPath("~/temp/" + name);
try
{
fucontact.PostedFile.SaveAs(allName);
}
catch (Exception ex)
{
if (System.IO.File.Exists(allName))
{
System.IO.File.Delete(allName);
}
FrameWorkV3.BLL.Common.ProcessError(ex.ToString());
return;
}
try
{
OdbcConnection odbc = new OdbcConnection("Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=" + Server.MapPath("~/temp") + ";");
odbc.Open();
string dbName = "[" + name + "]";
string sql = @"SELECT * FROM " + dbName;
OdbcDataAdapter dbCmd = new OdbcDataAdapter(sql, odbc);
DataSet dataSet = new DataSet(dbName);
dbCmd.Fill(dataSet, dbName);
odbc.Close();
if (dataSet.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in dataSet.Tables[0].Rows)
{
TContact contact = new TContact();
contact.LastName = dr["Last Name"].ToString();
contact.FirstName = dr["First Name"].ToString();
contact.Company = dr["Company"].ToString();
contact.Address1 = dr["Business Street"].ToString();
contact.City = dr["Business City"].ToString();
contact.State = dr["Business State"].ToString();
contact.PostalCode = dr["Business Postal Code"].ToString();
contact.Phone1 = dr["Business Phone"].ToString();
if (SMQShare.Utilities.IsDateTime(dr["Birthday"].ToString()))
{
contact.BirthDate = Convert.ToDateTime(dr["Birthday"].ToString());
}
if (dr["Gender"].ToString() == "0" || dr["Gender"].ToString() == "1")
{
contact.Gender = Convert.ToInt32(dr["Gender"].ToString());
}
if (SMQShare.Utilities.IsDateTime(dr["Anniversary"].ToString()))
{
contact.Anniversary = Convert.ToDateTime(dr["Anniversary"].ToString());
}
contact.Email = dr["E-mail Address"].ToString();
contact.Notes = dr["Notes"].ToString();
contact.CreatedOn = DateTime.Now;
contact.CreatedBy = GlobeInfo.CustomerId;
contact.CustomerId = GlobeInfo.CustomerId;
_result = contactsource.getContact_SourceListByTypeCode("OutLook");
if (!_result.Success)
{
FrameWorkV3.BLL.Common.ProcessError(_result);
trans.RollbackTransaction();
return;
}
contactsource = _result.ObjectList[0] as TContact_Source;
contact.ContactSourceId = contactsource.Id;
_result = contact.Save(trans);
if (!_result.Success)
{
FrameWorkV3.BLL.Common.ProcessError(_result);
trans.RollbackTransaction();
return;
}
}
trans.CommitTransaction();
lb_importmsg.Text = StringResourcePage["Import_Success"];
this.ReDataBindInPage(this.Page);
}
}
catch (Exception exception)
{
trans.RollbackTransaction();
if (System.IO.File.Exists(allName))
{
System.IO.File.Delete(allName);
}
FrameWorkV3.BLL.Common.ProcessError(exception.ToString());
return;
}
finally
{
if (System.IO.File.Exists(allName))
{
System.IO.File.Delete(allName);
}
}
}
else
{
lb_importmsg.Text = StringResourcePage["Wrong_Type"];
}
}
else
{
lb_importmsg.Text = StringResourcePage["No_ImportFile"];
}
}