简体   繁体   中英

Getting a database error when trying to view data

Alright,

I have a page that interacts with my SQL data connection. it tries to run a stored procedure, but every time I run it, it locks out of the database showing this error:
1

Here's the code the fires of the connection and stored procedure ` FileUpload1.SaveAs(Server.MapPath("images//" + FileUpload1.FileName)); string Image = MapPath("images//" + FileUpload1.FileName);

        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BOOKS.mdf;Integrated Security=True;User Instance=True");

                SqlCommand cmd = new SqlCommand("AddaBook", con);
            cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Title", SqlDbType.NVarChar).Value = txtTitle.Text;
                cmd.Parameters.AddWithValue("@Image", SqlDbType.NVarChar).Value = Image;
                cmd.Parameters.AddWithValue("@Author", SqlDbType.NVarChar).Value = txtAuthor.Text;
                cmd.Parameters.AddWithValue("@PublishDate", SqlDbType.DateTime).Value = txtPublishDate.Text;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                cmd.Connection.Close();

                lblUploadResult.Text = "File uploaded successfully.";
                lblUploadResult.ForeColor = System.Drawing.Color.ForestGreen;

            }` 

Any help is appreciated and always thank you!

Try moving your mdf file out of OneDrive. Sometime when OneDrive is doing an upload, or one of the many other processes it does, it will lock the files - especially from system user.

It is saying that your database is under usage so you can not access it.

Be sure that no other code is accessed in the database without closing the connection .

You may also update the code with the following as some not needed lines are removed.

    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BOOKS.mdf;Integrated Security=True;User Instance=True");

    SqlCommand cmd = new SqlCommand("AddaBook", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@Title", SqlDbType.NVarChar).Value = txtTitle.Text;
    cmd.Parameters.AddWithValue("@Image", SqlDbType.NVarChar).Value = Image;
    cmd.Parameters.AddWithValue("@Author", SqlDbType.NVarChar).Value = txtAuthor.Text;
    cmd.Parameters.AddWithValue("@PublishDate", SqlDbType.DateTime).Value = txtPublishDate.Text;
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();

    lblUploadResult.Text = "File uploaded successfully.";
    lblUploadResult.ForeColor = System.Drawing.Color.ForestGreen;

You already have the file open but just don't know it. For example, if you are trying to open a text file called Test.txt (or conversely a.mdf database file which has already been opened), then you will get this error, which is also why you are likely getting the other errors down the line but all related to this first error.

With databases, things can get a bit tricky, because you can connect to a database and at times get an error and be kicked out of the system without closing the connection or process... at which point a currently running but unknown process may be keeping the connection alive. You can look at all open processes on your system or see if you mistakenly have the connection open and then close it before trying to open it through the application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM