【发布时间】:2016-03-22 11:57:40
【问题描述】:
我在 win 形式的 OpenFileDialog 中遇到了这个问题..
private void btnAllegato_Click(object sender, EventArgs e)
{
try
{
using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
{
string path = string.Empty;
openFileDialog1.Title = "Seleziona richiestaIT (PDF)..";
openFileDialog1.Filter = ("PDF (.pdf)|*.pdf");
openFileDialog1.FilterIndex = 1;
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//salva l'intero path
path = openFileDialog1.FileName;
//nome file + estensione
string temp = openFileDialog1.SafeFileName;
//elimina l'estensione del file con IgnoreCase -> case Unsensitive
temp = Regex.Replace(temp, ".pdf", " ", RegexOptions.IgnoreCase);
//datatime + replace
string timenow = System.DateTime.Now.ToString();
//replace data da gg//mm/aaaa ss:mm:hh -----> ad gg-mm-aaaa_ss-mm-hh
timenow = timenow.Replace(":", "-").Replace("/", "-");//.Replace(" ", " ");
//effettua una copia dal path origine alla cartella nel NAS
_url = @"\\192.168.5.7\dati\SGI\GESTIONE IT\RichiesteIT\" + temp + timenow + ".pdf";
this.Cursor = Cursors.WaitCursor;
System.IO.File.Copy(path, _url);
this.Cursor = Cursors.Default;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
第一次运行...一切正常... 但是第二次点击 btn ...进程进入循环.. OpenFileDialog 是打开的,但完全是白色的...
我认为这是处置资源的问题..但我不知道如何解决它。
... ... @EDIT
经过几次尝试......我意识到问题出在我点击按钮后Inserisci>>。 第一次运行良好,但是当我在 btnInserisci 之后第二次点击 ... btnAllegato 时,我有循环过程。
private void btnInserisci_Click(object sender, EventArgs e)
{
try
{
if ((_IDRichiedente != -1) && (_data != string.Empty) && (_url != string.Empty))
{
//messageBox
MessageBox.Show(_url);
QueryAssist qa = new QueryAssist();
string query = "INSERT INTO RICHIESTA_IT(ID_Risorsa, descrizione_richiesta, modulo_pdf, data_richiesta) VALUES('" + _IDRichiedente + "', '" + txtBreveDescrizione.Text + "', '" + _url + "', '" + _data + "');";
MessageBox.Show(query);
qa.runQuery(query);
// qa.runQuery("INSERT INTO RICHIESTA_IT (ID_Risorsa, data_richiesta) VALUES ('" + _IDRichiedente + "','" + _data + "');");
}
else
{
MessageBox.Show("Selezionare il richiedente,data o allegato!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
其中private int _IDRichiedente = -1;
private string _data = String.Empty;
private string _url = string.Empty; 是类的字段。
QueryAssist 是我创建连接、运行查询和断开连接的个人课程。 代码:
class QueryAssist
{
System.Data.OleDb.OleDbConnection _OleDBconnection;
public QueryAssist()
{
this._OleDBconnection = null;
}
//riferimento di connessione al db
private bool connectionDB()
{
string connection = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\"\\\\192.168.5.7\\dati\\Scambio\\Sviluppo\\Impostazioni temporanea db Censimento\\CensimentoIT.accdb\"";
try
{
_OleDBconnection = new System.Data.OleDb.OleDbConnection(connection);
_OleDBconnection.Open();
return true;
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
return false;
}
}
private void disconnectDB()
{
try
{
_OleDBconnection.Close();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
public System.Data.DataTable runQuery(string query)
{
try
{
if (connectionDB())
{
System.Data.DataTable dataTable = new System.Data.DataTable();
System.Data.OleDb.OleDbCommand sqlQuery = new System.Data.OleDb.OleDbCommand(query, _OleDBconnection);
System.Data.OleDb.OleDbDataAdapter adapter = new OleDbDataAdapter(sqlQuery);
adapter.Fill(dataTable);
disconnectDB();
return dataTable;
}
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
return null;
}
public int countRowsQueryResult(string query)
{
try
{
if (connectionDB())
{
System.Data.DataTable dataTable = new System.Data.DataTable();
System.Data.OleDb.OleDbCommand sqlQuery = new System.Data.OleDb.OleDbCommand(query, _OleDBconnection);
System.Data.OleDb.OleDbDataAdapter adapter = new OleDbDataAdapter(sqlQuery);
adapter.Fill(dataTable);
disconnectDB();
return dataTable.Rows.Count;
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
return -1;
}
}
}
对不起我的英语
【问题讨论】:
-
您的应用程序中有
GDI+或Win32api 代码吗? -
只有windows窗体...
-
您的 program.cs 的 Main 上方是否有 [STAThread] 属性?
-
当您的程序的 Main() 入口点缺少 [STAThread] 属性或您在工作线程上运行此代码时,可能会发生这样的死锁。安装在您的机器上的不可靠的 shell 扩展也可能导致它。
-
[STAThread] 存在 link image ...
标签: c# winforms openfiledialog