我的数据库正在被另一个进程使用,但是什么进程?!? [英] My database is being used by another process, but what process ?!?

查看:112
本文介绍了我的数据库正在被另一个进程使用,但是什么进程?!?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,在处理完我的原始问题的代码后(下面用斜体字表示),我想我(稍微有点)更好地理解它是如何工作的,我认为我有更好的代码。 ..然而,它仍然没有工作&我很困惑!!!



我修改了我的代码以使用 USING 声明,因为我被引导相信我的内容已经读过它会在我完成它们时释放资源,即使它们是程序失败,但它似乎并没有为我这样做...



以下是导致我的问题的时间表:



1)在VS2010内打开项目

2)添加新项目/基于数据/服务的数据库/数据集&然后创建一个表,所以数据库名为MyTestDB,表是MyTestTbl,&字段是MyTestID(Int,Primary Key,IsIdentity True,1,1)& MyTestNum(Int)

3)构建成功

4)在调试模式下运行程序,表格显示,点击插入

5)MyCount = 1,MessageBox说myDB.State = Open,处理数据,MessageBox说myDB.State = Closed,Form重新显示。

6)重复,重复,重复等等...... MyCount进展正如所料,没有问题,退出&程序结束。

7)现在我们遇到了问题......首先,数据库中仍然没有记录!如果我右键单击服务器资源管理器中的MyTestTbl表和显示表数据,它只显示标题& Null,Null。 问题1 :为什么?我是否必须添加另一行代码来提交更新或其他内容?!?

8)如果我现在再次以调试模式运行程序,它将无法运行,因为我有2个错误,类似于我原来的问题,与\bin\debug文件夹中已存在的文件有关...无法将文件< path> \ MyTestDB.mdf复制到bin \Debug \ MyTestDB.mdf 。进程无法访问文件bin\Debug \ MyTestDB.mdf,因为它正被另一个进程使用。(文件MyTestDB_log.ldf的错误消息相同)。问题2 :再次,为什么?!?为什么这些文件被锁定或被阻止?!?



这是我的代码,当然不应该这么难!!! />


OK, after working on my code regarding my original question (below in italics), I think I have a (slightly !) better understanding of how this works and I think I have better code ... however, it is still not working & I am confused !!!

I have amended my code to use the "USING" statement as I am led to believe from what I have read that it will release resources when I have finished with them, even if their is a program failure, but it doesn't seem to be doing that for me ...

Here is the timeline leading to my problem :

1) Open the Project within VS2010
2) Add a new Item / Data / Service Based Database / DataSet & then create a single Table, so the Database is called MyTestDB, the Table is MyTestTbl, & the fields are MyTestID (Int, Primary Key, IsIdentity True, 1,1) & MyTestNum (Int)
3) Build Succeeded
4) Run the Program in Debug mode, Form displays, Click on Insert
5) MyCount = 1, MessageBox says myDB.State = Open, process data, MessageBox says myDB.State =Closed, Form re-displays.
6) Repeat, repeat, repeat, etc. ... MyCount progresses as expected, no problems, Quit & program ends.
7) Now we have a problem ... Firstly, there are still no records in the database !!! If I right-click on the MyTestTbl Table in Server Explorer and "Show Table Data" it just shows the Headings & Null, Null. Question 1 : Why ? Do I have to add another line of code to "commit" the update or something ?!?
8) If I now run the Program in Debug mode again, it will not run because I have 2 Errors, similar to my original problem, something to do with files in the \bin\debug Folder already existing ... "Unable to copy file <path> \MyTestDB.mdf to bin\Debug\MyTestDB.mdf. The process cannot access the file bin\Debug\MyTestDB.mdf because it is being used by another process. (the same error message for the file MyTestDB_log.ldf). Question 2 : Again, why ?!? Why are these files getting locked, or blocked ?!?

This is my code, surely it shouldn't be this difficult !!!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MySqlTest
{
    public partial class Form1 : Form
    {
        int myCount;
        string myDBlocation = @"Data Source=MEDESKTOP;AttachDbFilename=|DataDirectory|\MyTestDB.mdf;Integrated Security=True;User Instance=False";

        public Form1()
        {
            InitializeComponent();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            myCount++;
            MessageBox.Show("myCount = " + myCount.ToString());
            //Insert Record Into  SQL File
            myDB_Insert();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //Read Record From SQL File

        }

        private void button4_Click(object sender, EventArgs e)
        {
            //Read All Records From SQL File

        }

        private void button5_Click(object sender, EventArgs e)
        {
            //Delete Record From SQL File
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //Quit
            myDB_Close();
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Close(object sender, EventArgs e)
        {

        }

        void myDB_Insert()
        {
            using (SqlConnection myDB = new SqlConnection(myDBlocation))
            using (SqlCommand mySqlCmd = myDB.CreateCommand())
            {
                myDB.Open();
                MessageBox.Show("State = " + myDB.State);
                mySqlCmd.CommandText = "INSERT INTO MyTestTbl(MyTestNum) VALUES(@MyTestNumValue)";
                mySqlCmd.Parameters.AddWithValue("@MyTestNumValue", myCount);
                mySqlCmd.ExecuteNonQuery();
                myDB.Close();
                MessageBox.Show("State = " + myDB.State);
            }
            return;
        }

        void myDB_Close()
        {
            using (SqlConnection myDB = new SqlConnection(myDBlocation))
            using (SqlCommand mySqlCmd = new SqlCommand())
            {
                myDB.Close();
            }
            return;
        }

    }
}





我写了一个非常小型C#程序,它使用一个非常小的SQL Server数据库,纯粹用于一些学习和测试目的。数据库用于这个新项目,而不是其他任何地方。但是,我在运行程序无法运行的Debugs时遇到问题,因为数据库正被另一个进程使用。



如果我重新启动机器,它会再次运行,然后经过几次测试后我会再次遇到同样的问题。



我发现在互联网上报告了很多类似的问题,但是如何解决这个问题却找不到明确的答案。首先,我如何找出使用我的.mdf&的其他过程。 .ldf文件?然后,我如何发布这些文件&不是为了在一段时间后停止这种情况发生的时间吗?!?



我是VS2010的新手,SQL Server& C#,所以在你给我的任何回复中请说明一点!



这是我的代码,你可以看到,你无法获得更基本的东西,我当然不应该遇到这些不断的问题!






I have written a very small C# program, that uses a very small SQL Server database, purely for some learning & testing purposes. The database is used in this one new project and nowhere else. However, I am getting problems whilst running Debugs where the program will not run, because the database "is being used by another process".

If I reboot my machine, it will work again, and then after a few test runs I will get the same problem again.

I have found many, many similar problems reported all over the Internet, but can find no definitive answer as to how to resolve this problem. Firstly, how do I find out what "other process" is using my .mdf & .ldf files ? Then, how do I get these files released & not held in order to stop this happening time after time after time ?!?

I am new to VS2010, SQL Server & C#, so please be quite descriptive in any replies you give me !!!

This is my code, as you can see, you couldn't get anything much more basic, I certainly shouldn't be running into these constant problems !!!


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;

    namespace MySqlTest
    {
        public partial class Form1 : Form
        {
            SqlConnection myDB = new SqlConnection(@"Data Source=MEDESKTOP;AttachDbFilename=|DataDirectory|\SqlTestDB.mdf;Initial Catalog=MySqlDB;Integrated Security=True");
            SqlDataAdapter myDA = new SqlDataAdapter();
            SqlCommand mySqlCmd = new SqlCommand();

            string mySQLcmd;
            int myCount;

            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("myDB state = " + myDB.State.ToString());
                //Open SQL File
                myDB.Open();
                MessageBox.Show("myDB state = " + myDB.State.ToString());
            }

            private void button2_Click(object sender, EventArgs e)
            {
                myCount++;
                MessageBox.Show("myCount = " + myCount.ToString());
                //Insert Record Into  SQL File
                mySqlCmd.Connection = myDB;
                mySqlCmd.CommandText = "INSERT INTO Parent(ParentName) Values(myCount)";
                myDA = new SqlDataAdapter(mySqlCmd);
                mySqlCmd.ExecuteNonQuery();
            }

            private void button3_Click(object sender, EventArgs e)
            {
                //Read Record From SQL File

            }

            private void button4_Click(object sender, EventArgs e)
            {
                //Read All Records From SQL File

            }

            private void button5_Click(object sender, EventArgs e)
            {
                //Delete Record From DQL File
            }

            private void button6_Click(object sender, EventArgs e)
            {
                MessageBox.Show("myDB state = " + myDB.State.ToString());
                //Close SQL File
                myDB.Close();
                MessageBox.Show("myDB state = " + myDB.State.ToString());
            }

            private void button7_Click(object sender, EventArgs e)
            {
                //Quit
                this.Close();
            }
        }
    }
[/code]

推荐答案

其他过程几乎肯定是你的程序,其他程序将使用它?你为什么要使用Access风格的数据库文件?如果你必须使用平面文件,我推荐SQLite,它允许多个连接。无论如何,这个 [ ^ ] shell扩展程序将允许您找出锁定文件的人,并将其释放。
The other process is almost certainly your program, what other program would be using it ? Why are you using Access style database files at all ? If you must use flat files, I recommend SQLite, which allows multiple connections. In any case, this[^] shell extension will allow you to find out who is locking your file, and release it.


尝试杀死SQL Server上的进程



请看下面这个链接



http://www.kodyaz.com/articles/kill-all-processes-of-a-database.aspx [ ^ ]




接受或投票,如果这对你有帮助

谢谢
try to kill the process on SQL Server

see this link below

http://www.kodyaz.com/articles/kill-all-processes-of-a-database.aspx[^]


Accept or vote if this will help you
thanks


我不得不进入服务并找到MSSQLSERVER,将Start选项更改为Manual,然后物理停止它...然后才能删除bin \debug文件夹中的文件!我将开始选项改回自动并启动了服务,最后,它再次全部工作!现在我必须找出它为什么会发生并阻止它再次发生......
I had to go into Services & find MSSQLSERVER, change the Start option to Manual, then physically Stop it ... than and only then, was I able to delete the files in the bin\debug folder !!! I altered the Start option back to Automatic & started the Service, and at last, it is all working again !!! Now I have to find out why it is happening and to prevent it from happening again ...


这篇关于我的数据库正在被另一个进程使用,但是什么进程?!?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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