string ConnStr = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + Server.MapPath("Data/ProgramList.xls") + ";Extended Properties=Excel 8.0";//从Excel中读数据
OleDbConnection myconnection = new OleDbConnection(ConnStr);
try
{
string sql = "select * from [programlist$] where air_sno is not null";//[programlist$] [Sheet1$
myconnection.Open();
OleDbDataAdapter myadapter = new OleDbDataAdapter(sql, myconnection);
myadapter.Fill(mytable_data);
myadapter.SelectCommand.CommandText = "select distinct air_sno from [programlist$]";
myadapter.Fill(mytable_air);
myconnection.Close();
}
catch (Exception a)
{
myconnection.Close();
}
#endregion
#region 汇入数据库中
SqlConnection connection = new SqlConnection(ConStr);
connection.Open();
SqlCommand mycommand = new SqlCommand();
mycommand.Connection = connection;
SqlTransaction st = connection.BeginTransaction();
mycommand.Transaction = st;
try
{
//删除过期节目表
mycommand.CommandText = "delete from tab_programlist where Programlist_time<@time and programlist_sno is not null";
mycommand.Parameters.Add("@time", SqlDbType.DateTime, 9).Value = DateTime.Now.Date.AddDays(-3);
mycommand.ExecuteNonQuery();
//删除用户过期录影
mycommand.CommandText = "delete from tab_programlist_user where programlist_sno in "
+" (select programlist_sno from tab_programlist_user where programlist_sno not in (select programlist_sno from tab_programlist ))";
mycommand.ExecuteNonQuery();
for (int i = 0; i < mytable_data.Rows.Count; i++)
{
mycommand.Parameters.Clear();
string sql = "if not exists(select air_sno from tab_programlist where programlist_time=@time and air_sno=@air) insert into tab_ProgramList(air_sno,programlist_name,programlist_time) values(@air,@content,@time)";
mycommand.CommandText = sql;
mycommand.Parameters.Add("@air", SqlDbType.Decimal, 9).Value = Convert.ToDecimal(mytable_data.Rows[i]["air_sno"]);
mycommand.Parameters.Add("@content", SqlDbType.NVarChar, 255).Value = mytable_data.Rows[i]["name"].ToString();
string time = mytable_data.Rows[i]["time"].ToString();//regextime.Replace(mytable_data.Rows[i]["time"].ToString(), "${year}-${month}-${day} ${minute}:${second}");
mycommand.Parameters.Add("@time", SqlDbType.SmallDateTime).Value = DateTime.Parse(time);
int index=mycommand.ExecuteNonQuery();
if (index > 0)
{
sql = "select @@identity from tab_programlist";
mycommand.Parameters.Clear();
mycommand.CommandText = sql;
decimal sno = decimal.Parse(mycommand.ExecuteScalar().ToString());
insert into Aim_Tab
SELECT * FROM OpenRowSet('Microsoft.Jet.OLEDB.4.0','Excel 8.0;HDR=no;DataBase=C:/Excel_Example_Aim.xls;',PlanSheetName$)
UNION
SELECT * 。。。。。