如何维护SQL Server数据库的调试版本? [英] How is the debug version of a SQL Server database maintained ?

查看:138
本文介绍了如何维护SQL Server数据库的调试版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了错误

无法将文件"j:\ users \ gary \ documents \ visual studio 2010 \ Projects \ MyApp01 \ MyApp01 \ MyApp01.mdf"复制到"bin \ Debug \ MyApp01.mdf".该进程无法访问文件' 'bin \ Debug \ MyApp01.mdf",因为它正在被另一个进程使用."



无法删除文件"j:\ users \ gary \ documents \ visual studio 2010 \ Projects \ MyApp01 \ MyApp01 \ bin \ Debug \ MyApp01.mdf".该进程无法访问文件" j:\ users \ gary \ documents \ visual studio 2010 \ Projects \ MyApp01 \ MyApp01 \ bin \ Debug \ MyApp01.mdf",因为它正在被另一个进程使用."

我已经将问题解决了将近一个星期,但仍然无法找出问题所在.

今天,我创建了一个名为MyApp01&的新项目.连接了一个新的相同名称的2列数据库,然后我编辑了属性,将复制到输出目录"状态从总是复制"更改为如果更新则复制".我对此的理解是,存储在MyApp01文件夹中的数据库在首次访问时会在MyApp01 \ bin \ Debug \文件夹中创建该数据库的测试版本,然后仅当数据库布局更改时才被覆盖.那是对的吗 ?如果是这样,为什么我会收到这些似乎暗示系统正在尝试替换数据库的\ bin \ Debug \版本的错误?!?

我是第一次运行该应用程序,看来一切正常,我的数据库更新了三个记录,这些记录显示在Form2中,我成功退出了该记录.但是,在VS2010中,我使用服务器资源管理器显示表数据,这显示数据库为空,因此,我认为这是在查看MyApp01文件夹&中的版本.而不是MyApp01 \ bin \ Debug \文件夹中的版本.因此,我再次运行该程序,这是我收到这些错误的地方.

所以,再次,我对数据库的复制和维护方式的理解正确了,如果是这样的话(或者,如果不是!),为什么我会收到这些错误消息,告诉我系统正在尝试替换\ bin \ Debug \版本数据库的?!?

这是我的代码:

I am getting the Errors

"Unable to copy file "j:\users\gary\documents\visual studio 2010\Projects\MyApp01\MyApp01\MyApp01.mdf" to "bin\Debug\MyApp01.mdf". The process cannot access the file ''bin\Debug\MyApp01.mdf'' because it is being used by another process."

and

"Unable to delete file "j:\users\gary\documents\visual studio 2010\Projects\MyApp01\MyApp01\bin\Debug\MyApp01.mdf". The process cannot access the file ''j:\users\gary\documents\visual studio 2010\Projects\MyApp01\MyApp01\bin\Debug\MyApp01.mdf'' because it is being used by another process."

I have been working on getting this problem fixed for almost a week now, but still cannot fathom out what is wrong.

Today I have created a new Project called MyApp01 & connected a new 2 Column Database of the same name, and I edited the Properties to change the "Copy to Output Directory" status from "Copy Always" to "Copy if Newer". My understanding of this is that the database that is stored in the MyApp01 Folder creates a test version of it in the MyApp01\bin\Debug\ folder the first time it is accessed and then is only overwritten if the Database layout is changed. Is that correct ? If so, why am I getting these errors that seem to insinuate that the system is trying to replace the \bin\Debug\ version of the database ?!?

I ran the App for the first time & it seemed to work just fine, my database was updated with three records, these displayed in Form2 and I Quit out of it successfully. However, back in VS2010 I used the Server Explorer to Show Table Data and this shows the Database as empty, so I believe that insinuates it is looking at the version in the MyApp01 folder & not the version in the MyApp01\bin\Debug\ folder. Therefore I ran the program again and this is where I get these errors.

So, again, is my understanding of the way the database is copied and maintained correct, and if so (or if not !!) why am I getting these errors telling me that the system is trying to replace the \bin\Debug\ version of the database ?!?

This is my code :

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 MyApp01
{
public partial class Form1 : Form
{
    int myCount;

    string myDBlocation = @"Data Source=MEDESKTOP;AttachDbFilename=|DataDirectory|\MyApp01.mdf;Integrated Security=True;User Instance=False";

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        myCount++;
        //Insert Record Into  SQL File
        myDB_Insert();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.Show();
    }

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

    void myDB_Insert()
    {

        using (SqlConnection myDB = new SqlConnection(myDBlocation))
        using (SqlCommand mySqlCmd = myDB.CreateCommand())
        {
            mySqlCmd.CommandText = "INSERT INTO MyAppTbl(MyData) VALUES(@MyValue)";
            mySqlCmd.Parameters.AddWithValue("@MyValue", myCount);
            myDB.Open();
            MessageBox.Show("State = " + myDB.State);
            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;
    }
}
}

推荐答案

就我个人而言,我不会这样做,这会使您面临各种问题.我只是将数据库放在那里,而将Visual Studio保留在其中,我会手动将主副本保留在另一个文件夹中,然后复制到调试文件夹中进行测试
Personally, I would not do it this way, it leaves you open to all sorts of issues. I''d just put the DB there, and leave Visual Studio out of it, I''d manually keep a master copy in another folder and copy to the debug folder for testing


它当然,当您在Project中添加一个New Database并使用Debug例程时,VS2010似乎一触即发.

我现在通过使用在MS SQL Server Management Studio中创建的新数据库解决了此问题,并将其作为现有项(而不是新项)添加到VS2010 C#项目中,并且工作正常……...最后!!!
It certainly seems that VS2010 gets its knickers in a twist when you add a New Database within the Project and then use the Debug routine.

I have resolved this now by using a New Database that I created in MS SQL Server Management Studio and I added it to the VS2010 C# Project as an Existing Item, rather than a New Item, and it works just fine ... phew, at last !!!


这篇关于如何维护SQL Server数据库的调试版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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