数据库正在被另一个进程使用...但是什么进程? [英] Database is being used by another process ... but what process?
问题描述
我写了一个很小的C#程序,它使用很小的SQL Server数据库,纯粹是为了一些学习和学习.测试目的.在这个新项目中使用了数据库,而在其他任何地方都没有使用.但是,在运行无法运行程序的Debug时遇到问题,因为数据库正在被另一个进程使用".
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.
我在Internet上发现了许多很多类似的问题,但是找不到如何解决此问题的明确答案.首先,如何找出使用我的.mdf&.ldf文件?然后,我如何释放这些文件?不是为了阻止这种情况一次又一次地举行?!?
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 ?!?
我是VS2010,SQL Server和VS的新手.C#,所以在您给我的任何回复中都请描述性强!!!
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 so many problems !!!
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();
}
}
}
推荐答案
最可能的选择:
- 程序的先前(崩溃)实例
- Visual Studio(打开表设计器或类似工具)
您可以在Server Explorer中查看1)和TaskManager,以及2).您的数据库应显示一个小红叉,表示已关闭".
You can check 1) with TaskManager and 2) by looking in Server Explorer. Your db should show a small red cross meaning 'closed'.
并且您应该重写代码以尽快关闭连接.使用try/finally或 using(){}
块.
And you should rewrite your code to close connections ASAP. Use try/finally or using(){ }
blocks.
这篇关于数据库正在被另一个进程使用...但是什么进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!