English 中文(简体)
C# WinForms申请中的多形式联系问题
原标题:Connection issue in C# WinForms application with multiple forms
  • 时间:2012-01-15 19:58:33
  •  标签:
  • c#
  • winforms

当我提出申请时,尽管我尚未开始申请,但还是装上了装饰/初步写上了所有形式? (I.E. Form.Show)

这就是我如何关闭标志形式的连接:

if (usertype == "UT1") //admin rights
{
    //GET LOGGED USER
    Home_Admin homeAdmin = new Home_Admin();
    homeAdmin.SetUsername(username);

    cString.Close();
    this.Close();

    System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(OpenHomeAdmin));
    t.Start();
}

以及我如何从家庭到Admin s菜单的背书

private void backUpToolStripMenuItem_Click(object sender, EventArgs e)
{
    BackUp BackUpForm = new BackUp();
    BackUpForm.Show();
}

I am trying to create a back up of my database and it works perfectly if I run only the back up form. If I start the app from the very beginning, it says back up failed the database is in use. I already closed the connection from login to the form where I will launch the back up form and even set a

if(conn.State = connectionState.Open)
{
     conn.close();
}

在备份程序之前。 无论怎么说,我只能杀掉与SQ >数据库的所有链接;后又恢复链接?

<>BACK UP CODE

    public void BackupDatabase(String destinationPath)
    {
        SqlConnection cString = new SqlConnection();
        cString.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=D:\MY_THESIS\WORKING FILES\NNIT-RMS.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

        if (cString.State == ConnectionState.Open) 
        {
            cString.Close();
        }

        try
        {
            //MY SERVER
            String userName = "NNIT-Admin";
            String password = "password";
            String serverName = @"RITZEL-PCSQLEXPRESS";

            ServerConnection connection = new ServerConnection(serverName, userName, password);
            Server sqlServer = new Server(connection);

            Backup BackupMgr = new Backup();
            BackupMgr.Devices.AddDevice(destinationPath, DeviceType.File);
            BackupMgr.Database = "NNIT DB";
            BackupMgr.Action = BackupActionType.Database;
            BackupMgr.SqlBackup(sqlServer);

            MessageBox.Show("Back up saved!");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + " " + ex.InnerException);
        }
    }

<>strong>FORM LOAD

private void BackUp_Load(object sender, EventArgs e)
{            
    string date = DateTime.Now.Day.ToString();
    string year = DateTime.Now.Year.ToString();
    string month = DateTime.Now.Month.ToString();

    Filename_txt.Text = "NNIT-RMSDB_" + month + date + year;
}

Error message http://img824.imageshack.us/img824/8541/error1lj.jpg

问题回答

在这一法典中:

SqlConnection cString = new SqlConnection();

cString.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=D:\MY_THESIS\WORKING FILES\NNIT-RMS.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

if (cString.State == ConnectionState.Open) 
{
    cString.Close();
}

您正在直截了new链接,从未开放。 <代码>if 条款never。

Your mistake is assuming that the new connection is the only one - there may be multiple connections (opened in other forms and never properly closed) - some not even from your application (for example, using SQL Server Management Studio - having an open query window to your database will mean there is an open connection).

The best solution for to kill all connections to db is to take it offline, other solutions almost fail

using (SqlConnection sqlcnn = new SqlConnection("Data Source=.\SQLEXPRESS;Integrated Security=True"))
            {
                SqlCommand sqlcmd = new SqlCommand("ALTER DATABASE DB_NAME SET OFFLINE WITH ROLLBACK IMMEDIATE", sqlcnn);
                sqlcnn.Open();
                sqlcmd.ExecuteNonQuery();
                sqlcnn.Close();
            }

在您的支持或恢复守则之前加上上述准则,然后,如果你与你的亲子有公开联系,你的支持或恢复将发挥作用。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签