C# 应用程序中的 SQL 查询

发布于 2024-10-21 07:22:12 字数 737 浏览 2 评论 0原文

我需要创建以下查询

CREATE TABLE [dbo].[soft] (
[img_pk] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) NULL ,
[img_data] [image] NULL ,
[img_contenttype] [varchar] (50) NULL, 
[file_data] [image] NULL ,
[file_contenttype] [varchar] (50) NULL 
) 

ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[soft] WITH NOCHECK ADD 
CONSTRAINT [PK_image] PRIMARY KEY NONCLUSTERED 
(
[img_pk]
) ON [PRIMARY]
GO

第一部分没有问题,它的解决方式类似于

string query1 = "CREATE TABLE soft" +
   "(pk int IDENTITY (1, 1) NOT NULL," +
  "name VARCHAR(50) NULL, img_data IMAGE NULL, img_contenttype VARCHAR(50) NULL, file_data IMAGE NULL, file_contenttype VARCHAR(50) NULL)"

但不知道如何使用下一部分。谁能帮助我吗?

I need to create the following query

CREATE TABLE [dbo].[soft] (
[img_pk] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) NULL ,
[img_data] [image] NULL ,
[img_contenttype] [varchar] (50) NULL, 
[file_data] [image] NULL ,
[file_contenttype] [varchar] (50) NULL 
) 

ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[soft] WITH NOCHECK ADD 
CONSTRAINT [PK_image] PRIMARY KEY NONCLUSTERED 
(
[img_pk]
) ON [PRIMARY]
GO

No problem with the first part, it's solved like

string query1 = "CREATE TABLE soft" +
   "(pk int IDENTITY (1, 1) NOT NULL," +
  "name VARCHAR(50) NULL, img_data IMAGE NULL, img_contenttype VARCHAR(50) NULL, file_data IMAGE NULL, file_contenttype VARCHAR(50) NULL)"

But no ideas how to work with the next part. Can anyone help me?

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

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

发布评论

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

评论(3

锦爱 2024-10-28 07:22:12

您可以组合表和主键定义:

CREATE TABLE [dbo].[soft] 
( 
   [img_pk] [int] IDENTITY (1, 1) NOT NULL 
       CONSTRAINT PK_Image PRIMARY KEY CLUSTERED, 
   [name] [varchar] (50) NULL , 
   [img_data] [image] NULL , 
   [img_contenttype] [varchar] (50) NULL,  
   [file_data] [image] NULL , 
   [file_contenttype] [varchar] (50) NULL  
)  
GO

如果这是您的默认文件组,则可以省去额外的“ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]”,这在许多(大多数?)情况下也是如此

此外,我已将 PK 创建为集群,这对于自动增量列来说更常见。

另外

ntext、文本和图像数据类型将
将在未来版本中删除
微软SQL服务器。避免使用
这些数据类型在新开发中
工作,并计划修改应用程序
目前正在使用它们。使用
nvarchar(max)varchar(max)
改为 varbinary(max)

You can combine the table and primary key definitions:

CREATE TABLE [dbo].[soft] 
( 
   [img_pk] [int] IDENTITY (1, 1) NOT NULL 
       CONSTRAINT PK_Image PRIMARY KEY CLUSTERED, 
   [name] [varchar] (50) NULL , 
   [img_data] [image] NULL , 
   [img_contenttype] [varchar] (50) NULL,  
   [file_data] [image] NULL , 
   [file_contenttype] [varchar] (50) NULL  
)  
GO

You can dispense with the extra 'ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] ' if that's your default FileGroup, which it is in many (most?) cases

Also, I have created the PK as Clustered, which is more usual for an auto-inc column.

Also:

ntext, text, and image data types will
be removed in a future version of
Microsoft SQL Server. Avoid using
these data types in new development
work, and plan to modify applications
that currently use them. Use
nvarchar(max), varchar(max), and
varbinary(max) instead.

戏剧牡丹亭 2024-10-28 07:22:12

您的意思是只是将查询加载到字符串中?您可以使用 @"" 多行字符串语法:(

string query2 = @"
ALTER TABLE [dbo].[soft] WITH NOCHECK ADD 
CONSTRAINT [PK_image] PRIMARY KEY NONCLUSTERED 
(
[img_pk]
) ON [PRIMARY]
";

顺便说一句,“ON PRIMARY .. GO”是第一个查询的一部分。)

要执行此操作,您将像往常一样使用类似的内容

var command2 = dbConnection.CreateCommand();
command2.CommandText = query2;
command2.ExecuteNonQuery();

You mean just load the query into a string? You can use the @"" multi-line string syntax:

string query2 = @"
ALTER TABLE [dbo].[soft] WITH NOCHECK ADD 
CONSTRAINT [PK_image] PRIMARY KEY NONCLUSTERED 
(
[img_pk]
) ON [PRIMARY]
";

(The 'ON PRIMARY .. GO' is part of the first query, by the way.)

To execute this you'd use something like

var command2 = dbConnection.CreateCommand();
command2.CommandText = query2;
command2.ExecuteNonQuery();

as usual.

我最亲爱的 2024-10-28 07:22:12

使用单行查询时遇到任何问题吗?

string query = "ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO ALTER TABLE [dbo].[soft] WITH NOCHECK ADD CONSTRAINT [PK_image] PRIMARY KEY NONCLUSTERED ( [img_pk] ) ON [PRIMARY] GO";

Any problems you face when you use single line queries ??

string query = "ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO ALTER TABLE [dbo].[soft] WITH NOCHECK ADD CONSTRAINT [PK_image] PRIMARY KEY NONCLUSTERED ( [img_pk] ) ON [PRIMARY] GO";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文