Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
vodkasoda
Starting Member
15 Posts |
Posted - 2012-02-08 : 09:25:27
|
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 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(); } } } |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-02-08 : 09:47:52
|
| The connection string you are using is useful if you want to read data directly from an MDF file that is NOT ATTACHED TO A SERVER. If you have an instance of SQL Server installed on your computer, and if you are able to see this database from the SQL Server Management Studio's object explorer, then the database is attached to the server. If that is true, use a different connection string that indicates the server name etc. Typical connection strings used are available here: http://connectionstrings.com/sql-server-2008 |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2012-02-08 : 10:01:00
|
Is your database set to SINGLE USER mode perhaps?SELECT user_access_desc FROM sys.databases WHERE [name] = 'MyDatabaseName' or you are not closing a connection, or not retrieving all the data from a dataset before attempting to reuse the connection. |
 |
|
|
vodkasoda
Starting Member
15 Posts |
Posted - 2012-02-08 : 10:42:14
|
I am not able to create a New Query in VS2010 to try this, I get the message "Unable to open the physical file ..... Operating system error 32: "32(failed to retrieve text for this error. Reason:15105). An attempt to attach an auto-named database for file ..... failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.However, in SQL Server Management Studio, I *can* run the query & I get the result "MULTI_USER".quote: Originally posted by Kristen Is your database set to SINGLE USER mode perhaps?SELECT user_access_desc FROM sys.databases WHERE [name] = 'MyDatabaseName' or you are not closing a connection, or not retrieving all the data from a dataset before attempting to reuse the connection.
|
 |
|
|
vodkasoda
Starting Member
15 Posts |
Posted - 2012-02-08 : 10:47:28
|
sunitabeck, I have SQL Server installed on this PC, but the person I am writing this application for will probably not have, so that is something that I have to look into, but I didn't think I would need to worry about that until I have finished my coding ?!?Could this be causing the problem, or are you just giving me some advice towards best practice ?!?quote: Originally posted by sunitabeck The connection string you are using is useful if you want to read data directly from an MDF file that is NOT ATTACHED TO A SERVER. If you have an instance of SQL Server installed on your computer, and if you are able to see this database from the SQL Server Management Studio's object explorer, then the database is attached to the server. If that is true, use a different connection string that indicates the server name etc. Typical connection strings used are available here: http://connectionstrings.com/sql-server-2008
|
 |
|
|
vodkasoda
Starting Member
15 Posts |
Posted - 2012-02-08 : 13:44:18
|
| FYI ... 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 that were causing the problem !!! 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 ... |
 |
|
|
|
|
|
|
|