急求!!如题下面是源码,请各位大神解惑
protected void Button3_Click(object sender, EventArgs e) //按“更新”按钮事件
{
string connString = "Data Source=GY-PC;Initial Catalog=Database1;Integrated Security=True";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
string sqlStr = "create table ";
sqlStr += "TempExcel" + "( ";
sqlStr += "barcode numeric(18) primary key,";
sqlStr += "unitPrice numeric(18,3),";
sqlStr += "storeNumber numeric(18),";
sqlStr += "productName nchar(100)";
sqlStr += ")";
SqlCommand cmd = new SqlCommand(sqlStr, conn); //命令
cmd.ExecuteNonQuery(); //执行建临时表命令
conn.Close();
int t = 0;
SqlConnection conn2 = new SqlConnection(connString);
conn2.Open();
SqlTransaction trans = conn2.BeginTransaction();//事物对象
try
{
SqlCommand com = new SqlCommand();//数据操作对象
com.Connection = conn2;//指定连接
com.Transaction = trans;//指定事物
string sql = "";
for (int ii = 1; ii < ds.Tables[0].Rows.Count; ii++)
{ //对datatable循环
sql = " INSERT TempExcel ( barcode , unitPrice , storeNumber , productName ) VALUES ( '" + dr[ii]["条 码"].ToString() + "','" + dr[ii]["销售单价"].ToString() + "','" + dr[ii]["库存数量"].ToString() + "','" + dr[ii]["品 名"].ToString() + "')";
//某一行的数据
com.CommandText = sql;
t = com.ExecuteNonQuery();
}
trans.Commit();//如果全部执行完毕.提交
}
catch
{
trans.Rollback();//如果有异常.回滚.
conn2.Close();
}
finally
{
conn2.Close();//关闭连接
}