..如何正确清理Excel互操作对象 [英] ..How to properly clean up Excel interop objects

查看:74
本文介绍了..如何正确清理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.


这篇关于..如何正确清理Excel互操作对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆