When I use BCP data into database, the trigger is not fired. What should I do to make the trigger useful? Can I merely update the data without changing values?
try
{
//get all data
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
strExcel = string.Format("select * from [{0}$] where xh<>''", sheetName);
OleDbDataAdapter oda = new OleDbDataAdapter(strExcel, strConn);
oda.Fill(ds, sheetName);
//BCP data
using (SqlBulkCopy bcp = new SqlBulkCopy(connectionString))
{
bcp.SqlRowsCopied += new SqlRowsCopiedEventHandler(bcp_SqlRowsCopied);
bcp.BatchSize = 100;//the speed of import
bcp.NotifyAfter = 100;
bcp.DestinationTableName = sheetName;//target table
bcp.WriteToServer(ds.Tables[0]);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
The trigger is following.
create trigger getAge
on tablename
for insert ,update
as
if update(birthday)
update tablename set age=DateDiff(year,birthday,getdate())
go
Check this MSDN article: http://msdn.microsoft.com/en-us/library/ms187640.aspx
Using bcp, triggers are disabled by default. To enable it, use the following argument:
-h "FIRE_TRIGGERS"
When working with the SqlBulkCopy
class provided by .NET, use the SqlBulkCopyOptions
enum to enable trigger.