简体   繁体   English

如何在Visual Studio中运行非持久查询

[英]How to run non-persistent queries in Visual Studio

When using the Query Design feature in Visual Studio, any queries that I run on a SQL Database or Microsoft Access Database while testing are persistent. 在Visual Studio中使用查询设计功能时,我在测试期间在SQL数据库或Microsoft Access数据库上运行的所有查询都是持久性的。 Meaning they actually change the data in the table(s). 这意味着它们实际上会更改表中的数据。 Is there a way to make the queries non-persistent while testing them until a program is run? 在测试程序运行之前,有没有办法使查询持久化? Using C# as a programming language, and .NET as a framework if it matters. 使用C#作为编程语言,使用.NET作为框架(如果重要)。 Also need to know the process for doing this with either an MS Access or SQL database. 还需要了解使用MS Access或SQL数据库执行此操作的过程。

You can do transactions in C# similar to how you use them in SQL. 您可以在C#中执行事务,类似于在SQL中使用事务的方式。 Here is an example: 这是一个例子:

connection.Open();

SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;

// Start a local transaction.
transaction = connection.BeginTransaction("SampleTransaction");

//Execute query here
Query details

//check if test environment
bool testEnvironment = SomeConfigFile.property("testEnvironment");
if (!testEnvironment) {
   transaction.Commit();
} else {
   transaction.Rollback();
}

Here is the documentation on transactions in C#: https://msdn.microsoft.com/en-us/library/86773566%28v=vs.110%29.aspx 这是有关C#中交易的文档: https : //msdn.microsoft.com/zh-cn/library/86773566%28v=vs.110%29.aspx

It should be possible for VS to create you a local copy of the SQL data you're working on while you're testing. 在测试过程中,VS应该可以为您创建正在处理的SQL数据的本地副本。 This is held in the bin folder. 它保存在bin文件夹中。 Have a look at this: 看看这个:

https://msdn.microsoft.com/en-us/library/ms246989.aspx https://msdn.microsoft.com/zh-CN/library/ms246989.aspx

Once you're finished testing you could simply change it to be pointing to the database you want to alter with your application. 一旦完成测试,您可以简单地将其更改为指向要随应用程序更改的数据库。

I'm not aware of a way to get exactly what you're asking for, but I think there is an approach to get close to the behaviour you want: 我不知道一种确切地获得您所要求的方法,但是我认为有一种方法可以接近您想要的行为:

When using Microsoft SQL Server, creating a table with a leading hash in the name (#tableName) will cause the table to be disposed of when your session ends. 使用Microsoft SQL Server时,如果使用名称(#tableName)开头的哈希表创建表,则会话结束时将丢弃该表。

One way you could take advantage of this to get your desired behaviour is to copy your working table into a temporary table, and work on the temporary table instead of the live table. 您可以利用此方法获得所需行为的一种方法是将工作表复制到临时表中,然后在临时表而不是活动表上工作。

To do so, use something like the following: 为此,请使用如下所示的内容:

SELECT * INTO #tempTable FROM liveTable

This will create a complete copy of your liveTable, with all of the same columns and rows. 这将创建liveTable的完整副本,其中包含所有相同的列和行。 Once you are finished, the table will be automatically dropped and no permanent changes will have been made. 完成后,该表将自动删除,并且不会进行永久更改。

This can also useful for a series of queries which you execute on the same subset of a large data set. 这对于在大型数据集的同一子集上执行的一系列查询也很有用。 Selecting the subset of data into a smaller temporary table can make subsequent queries much faster than if you had to select from the full data set repeatedly. 将数据子集选择到较小的临时表中,可以使后续查询的速度比必须从完整数据集中重复选择的查询快得多。

Just keep in mind that as soon as your connection closes, all the data goes with it. 请记住,连接一旦关闭,所有数据都会随之流逝。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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