..如何正确清理Excel互操作对象
问题描述:
hii
i使用此代码
hii
i used this code
private void btnshow_Click(object sender, EventArgs e)
{
string connStr = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\KBE\\solid piston1.xls';Extended Properties=Excel 8.0;";
OleDbConnection MyConnection;
DataSet ds1;
OleDbDataAdapter MyCommand1;
MyConnection = new OleDbConnection(connStr);
MyCommand1 = new OleDbDataAdapter("select * from [INPUT$]", MyConnection);
ds1 = new System.Data.DataSet();
MyCommand1.Fill(ds1);
DataTable dt1 = new DataTable();
dt1 = ds1.Tables[0];
cbf.Items.Insert(0, dt1.Rows[1]["Value"].ToString());
textBox1.Text = dt1.Rows[2]["Value"].ToString();
textBox2.Text = dt1.Rows[3]["Value"].ToString();
textBox3.Text = dt1.Rows[4]["Value"].ToString();
textBox4.Text = dt1.Rows[5]["Value"].ToString();
comboBox2.Text = dt1.Rows[6]["Value"].ToString();
textBox5.Text = dt1.Rows[7]["Value"].ToString();
textBox6.Text = dt1.Rows[8]["Value"].ToString();
textBox7.Text = dt1.Rows[9]["Value"].ToString();
cbr.Text = dt1.Rows[10]["Value"].ToString();
// textBox46.Text = dt1.Rows[11]["Value"].ToString();
comboBox4.Text = dt1.Rows[12]["Value"].ToString();
textBox8.Text = dt1.Rows[13]["Value"].ToString();
textBox19.Text = dt1.Rows[24]["Value"].ToString();
comboBox5.Text = dt1.Rows[25]["Value"].ToString();
textBox20.Text = dt1.Rows[26]["Value"].ToString();
textBox21.Text = dt1.Rows[27]["Value"].ToString();
textBox22.Text = dt1.Rows[28]["Value"].ToString();
textBox23.Text = dt1.Rows[29]["Value"].ToString();
textBox24.Text = dt1.Rows[30]["Value"].ToString();
textBox17.Text = dt1.Rows[22]["Value"].ToString();
MyConnection.Close();
GC.Collect();
}
每当我点击任务管理器中打开的新excel按钮时,应用程序就会出现问题。
所以请给我建议从任务经理那里清理excel
谢谢
whenever i click button new excel open in the task manager so problem occur in application .
so please give me suggestion to clean the excel from the task manager
thanks
答
,MyConnection);
ds1 = new System.Data。 DataSet();
MyCommand1.Fill(ds1);
DataTable dt1 = new DataTable();
dt1 = ds1.Tables [ 0 ];
cbf.Items.Insert( 0 ,dt1。行[ 1 ] [ Value ] .ToString());
textBox1.Text = dt1.Rows [ 2 ] [ Value]。ToString();
textBox2.Text = dt1.Rows [ 3 ] [ Value]。ToString();
textBox3.Text = dt1.Rows [ 4 ] [ 值跨度>]的ToString();
textBox4.Text = dt1.Rows [ 5 ] [ 值跨度>]的ToString();
comboBox2.Text = dt1.Rows [ 6 ] [ 值跨度>]的ToString();
textBox5.Text = dt1.Rows [ 7 ] [ 值跨度>]的ToString();
textBox6.Text = dt1.Rows [ 8 ] [ 值跨度>]的ToString();
textBox7.Text = dt1.Rows [ 9 ] [ 值跨度>]的ToString();
cbr.Text = dt1.Rows [ 10 ] [ 值跨度>]的ToString();
// textBox46.Text = dt1.Rows [11] [Value]。ToString();
comboBox4.Text = dt1.Rows [ 12 ] [ 值跨度>]的ToString();
textBox8.Text = dt1.Rows [ 13 ] [ 值跨度>]的ToString();
textBox19.Text = dt1.Rows [ 24 ] [ 值跨度>]的ToString();
comboBox5.Text = dt1.Rows [ 25 ] [ 值跨度>]的ToString();
textBox20.Text = dt1.Rows [ 26 ] [ 值跨度>]的ToString();
textBox21.Text = dt1.Rows [ 27 ] [ 值跨度>]的ToString();
textBox22.Text = dt1.Rows [ 28 ] [ 值]。ToString();
textBox23.Text = dt1.Rows [ 29 ] [ 值跨度>]的ToString();
textBox24.Text = dt1.Rows [ 30 ] [ 值跨度>]的ToString();
textBox17.Text = dt1.Rows [ 22 ] [ 值跨度>]的ToString();
MyConnection.Close();
GC.Collect();
}
", MyConnection); ds1 = new System.Data.DataSet(); MyCommand1.Fill(ds1); DataTable dt1 = new DataTable(); dt1 = ds1.Tables[0]; cbf.Items.Insert(0, dt1.Rows[1]["Value"].ToString()); textBox1.Text = dt1.Rows[2]["Value"].ToString(); textBox2.Text = dt1.Rows[3]["Value"].ToString(); textBox3.Text = dt1.Rows[4]["Value"].ToString(); textBox4.Text = dt1.Rows[5]["Value"].ToString(); comboBox2.Text = dt1.Rows[6]["Value"].ToString(); textBox5.Text = dt1.Rows[7]["Value"].ToString(); textBox6.Text = dt1.Rows[8]["Value"].ToString(); textBox7.Text = dt1.Rows[9]["Value"].ToString(); cbr.Text = dt1.Rows[10]["Value"].ToString(); // textBox46.Text = dt1.Rows[11]["Value"].ToString(); comboBox4.Text = dt1.Rows[12]["Value"].ToString(); textBox8.Text = dt1.Rows[13]["Value"].ToString(); textBox19.Text = dt1.Rows[24]["Value"].ToString(); comboBox5.Text = dt1.Rows[25]["Value"].ToString(); textBox20.Text = dt1.Rows[26]["Value"].ToString(); textBox21.Text = dt1.Rows[27]["Value"].ToString(); textBox22.Text = dt1.Rows[28]["Value"].ToString(); textBox23.Text = dt1.Rows[29]["Value"].ToString(); textBox24.Text = dt1.Rows[30]["Value"].ToString(); textBox17.Text = dt1.Rows[22]["Value"].ToString(); MyConnection.Close(); GC.Collect(); }
每当我点击任务管理器中打开的新excel按钮时,应用程序就会出现问题。
所以请给我建议从任务经理那里清理excel
谢谢
whenever i click button new excel open in the task manager so problem occur in application .
so please give me suggestion to clean the excel from the task manager
thanks
在函数的末尾,你这样写。
at the end of the function, you write like this..
MyConnection.Dispose();
MyConnection = null;
MyCommand1.Dispose();
MyCommand1 = null;
OleDbConnection类实现 IDisposable [ ^ ]因此您可以使用使用关键字。这意味着当使用块超出范围时,连接将自行清理。阅读此 [ ^ ]文章,以便对IDisposable接口有一个基本的了解。
The OleDbConnection class implements IDisposable[^] so you can use the using keyword. This means that when the using block goes out of scope, the connection will clean up after itself. Read this[^] article to get a basic understanding of the IDisposable interface.