如何在具有 System.Data.SQLite 数据提供程序的数据库项目中使用 SQL 脚本?

发布于 2024-07-21 19:47:40 字数 1401 浏览 4 评论 0原文

我有一个项目,我尝试通过 System.Data.SQLite 使用 SQLite。 为了让数据库保持版本控制,我在 VS2008 中创建了一个数据库项目。 听起来不错,对吧?

我创建了第一个表创建脚本,并尝试使用右键单击 -> 在脚本上运行来运行它,我收到此错误消息:

您正在使用的提供程序或数据源不支持此操作。

有谁知道是否有一种自动方法可以使用 System.Data.SQLite 安装提供的提供程序,针对数据库引用的 SQLite 数据库使用数据库项目一部分的脚本?

我尝试了我能想到的所有变体,试图让脚本使用默认的 Run 或 Run On... 命令运行。 这是最冗长且可能不正确的形式的脚本:

USE Characters
GO

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'U' AND name = 'Skills')
  BEGIN
    DROP Table Skills
  END
GO

CREATE TABLE Skills
(
   SkillID INTEGER PRIMARY KEY AUTOINCREMENT,
   SkillName TEXT,
   Description TEXT
)
GO

请注意,这是我第一次尝试使用数据库,也是我第一次接触 SQLite。 在我尝试让它运行的过程中,我删除了除 CREATE TABLE 命令之外的所有内容。

更新:好的,正如 Robert Harvey 在下面指出的那样,这看起来像一个 SQL Server 存储过程。 我进入服务器资源管理器并使用我的连接(来自数据库项目)来执行他关于创建表的建议。 我可以生成 SQL 来创建表,结果如下:

CREATE TABLE [Skills] (
    [SkillID] integer PRIMARY KEY NOT NULL,
    [SkillName] text NOT NULL,
    [Description] text NOT NULL
);

我可以轻松复制它并将其添加到项目中(或将其添加到处理其余数据访问的另一个项目中),但是无论如何都有在构建时自动执行此操作? 我想,由于在本例中 SQLite 是一个单文件,因此我也可以将构建的数据库置于版本控制之下。

想法? 此实例的最佳实践?

更新:我想,既然我计划使用 Fluent NHibernate,我可能只使用它的自动持久性模型来保持我的数据库最新并有效地进行源代码控制。 想法? 陷阱? 我想我必须将初始人口插入单独保留在源代码控制中,但它应该有效。

I've got a project where I'm attempting to use SQLite via System.Data.SQLite. In my attempts to keep the database under version-control, I went ahead and created a Database Project in my VS2008. Sounds fine, right?

I created my first table create script and tried to run it using right-click->Run on the script and I get this error message:

This operation is not supported for the provider or data source you are using.

Does anyone know if there's an automatic way to use scripts that are part of database project against SQLite databases referenced by the databases, using the provider supplied by the System.Data.SQLite install?

I've tried every variation I can think of in an attempt to get the script to run using the default Run or Run On... commands. Here's the script in it's most verbose and probably incorrect form:

USE Characters
GO

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'U' AND name = 'Skills')
  BEGIN
    DROP Table Skills
  END
GO

CREATE TABLE Skills
(
   SkillID INTEGER PRIMARY KEY AUTOINCREMENT,
   SkillName TEXT,
   Description TEXT
)
GO

Please note, this is my first attempt at using a Database, and also the first time I've ever touched SQLite. In my attempts to get it to run, I've stripped any and everything out except for the CREATE TABLE command.

UPDATE: Ok, so as Robert Harvey points out below, this looks like an SQL Server stored procedure. I went into the Server Explorer and used my connection (from the Database project) to get do what he suggested regarding creating a table. I can generate SQL from to create the table and it comes out like thus:

CREATE TABLE [Skills] (
    [SkillID] integer PRIMARY KEY NOT NULL,
    [SkillName] text NOT NULL,
    [Description] text NOT NULL
);

I can easily copy this and add it to the project (or add it to another project that handles the rest of my data-access), but is there anyway to automate this on build? I suppose, since SQLite is a single-file in this case that I could also keep the built database under version-control as well.

Thoughts? Best practices for this instance?

UPDATE: I'm thinking that, since I plan on using Fluent NHibernate, I may just use it's auto-persistence model to keep my database up-to-snuff and effectively in source control. Thoughts? Pitfalls? I think I'll have to keep initial population inserts in source-control separately, but it should work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放低过去 2024-07-28 19:47:40

我使用 SQLite SQL 脚本构建了数据库,然后将其输入到 sqlite3.exe 控制台程序中,如下所示。

c:\sqlite3.exe mydatabase.db < 包含 SQLiteSQLCommands 的文件

John

I built my database using an SQLite SQL script and then fed that into the sqlite3.exe console program like this.

c:\sqlite3.exe mydatabase.db < FileContainingSQLiteSQLCommands

John

心病无药医 2024-07-28 19:47:40

嗯,您的脚本看起来像一个 SQL Server 存储过程。 SQLite 很可能不支持这一点,因为

  1. 它不支持存储过程,并且
  2. 它不理解 SQL Server T-SQL

SQL 实际上是一个伪标准。 它在供应商之间有所不同,有时甚至在同一供应商内的产品的不同版本之间也有所不同。

也就是说,我看不出有什么理由不能通过打开连接和命令对象来针对 SQLite 数据库运行任何(SQLite 兼容)SQL 语句,就像使用 SQL Server 一样。

然而,由于您是数据库和 SQLite 的新手,因此您应该如何开始。 我假设您已经安装了 SQLite

  1. 在 Visual Studio 2008 中创建一个新的 Windows 应用程序。数据库应用程序对您没有用处。

  2. 通过下拉视图菜单并选择服务器资源管理器来打开服务器资源管理器。

  3. 通过右键单击服务器资源管理器中的数据连接节点并单击“添加新连接...”来创建新连接

    通过右键单击

  4. 单击“更改”按钮

  5. 选择 SQLite 提供程序

  6. 为您的数据库指定一个文件名。

  7. 单击“确定”。

新的数据连接应出现在服务器资源管理器中。 您可以通过右键单击“表”节点并选择“添加新表”来创建第一个表。

Well, your script looks like a SQL Server stored procedure. SQLite most likely doesn't support this, because

  1. It doesn't support stored procedures, and
  2. It doesn't understand SQL Server T-SQL

SQL is actually a pseudo-standard. It differs between vendors and sometimes even between different versions of a product within the same vendor.

That said, I don't see any reason why you can't run any (SQLite compatible) SQL statement against the SQLite database by opening up connection and command objects, just like you would with SQL Server.

Since, however, you are new to databases and SQLite, here is how you should start. I assume you already have SQLite installed

  1. Create a new Windows Application in Visual Studio 2008. The database application will be of no use to you.

  2. Open the Server Explorer by pulling down the View menu and selecting Server Explorer.

  3. Create a new connection by right-clicking on the Data Connections node in Server Explorer and clicking on Add New Connection...

  4. Click the Change button

  5. Select the SQLite provider

  6. Give your database a file name.

  7. Click OK.

A new Data Connection should appear in the Server Explorer. You can create your first table by right-clicking on the Tables node and selecting Add New Table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文