如题:
当一个主键存在时,我使用了Index.Drop();
结果报错如下:
An explicit DROP INDEX is not allowed on index 'dbo.Article.PK_Article'. It is being used for PRIMARY KEY constraint enforcement.
十分感谢!
...全文
30322打赏收藏
使用SMO如何删除主键?
如题: 当一个主键存在时,我使用了Index.Drop(); 结果报错如下: An explicit DROP INDEX is not allowed on index 'dbo.Article.PK_Article'. It is being used for PRIMARY KEY constraint enforcement. 十分感谢!
上面的UIndex是我自己包装的,其实就是原来的Index外包一层
例如:所有以类名U开头均是指加了一下包装,为的是可以扩展属性出来,不要被迷惑:
我重装了SQL,安装了开发版,打了SP1问题依旧。正在下SP2。。。
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
namespace Admiral.UBox.Core
{
public class UIndex : EntiyBase
{
#region propertyies
UIndexedColumnCollection _uindexedColumns;
public UIndexedColumnCollection UIndexedColumns
{
get
{
if (_uindexedColumns == null)
{
_uindexedColumns = new UIndexedColumnCollection(_index.IndexedColumns);
}
return _uindexedColumns;
}
}
Index _index;
public string Name
{
get { return _index.Name; }
set { _index.Name = value; }
}
public bool IgnoreDuplicateKeys
{
get
{
return _index.IgnoreDuplicateKeys;
}
set
{
_index.IgnoreDuplicateKeys = value;
}
}
public IndexKeyType IndexKeyType
{
get
{
return _index.IndexKeyType;
}
set
{
_index.IndexKeyType = value;
}
}
public bool IsClustered
{
get
{
return _index.IsClustered;
}
set
{
_index.IsClustered = value;
}
}
public bool IsDisabled
{
get
{
return _index.IsDisabled;
}
}
public bool IsFullTextKey
{
get
{
return _index.IsFullTextKey;
}
set
{
_index.IsFullTextKey = value;
}
}
public bool IsIndexOnComputed
{
get
{
return _index.IsIndexOnComputed;
}
}
public bool IsIndexOnTable
{
get
{
return _index.IsIndexOnTable;
}
}
public bool IsPartitioned
{
get
{
return _index.IsPartitioned;
}
}
public bool IsSystemNamed
{
get
{
return _index.IsSystemNamed;
}
}
public bool IsSystemObject
{
get
{
return _index.IsSystemObject;
}
}
public bool IsUnique
{
get
{
return _index.IsUnique;
}
set
{
_index.IsUnique = value;
}
}
public bool IsXmlIndex
{
get
{
return _index.IsXmlIndex;
}
}
public int MaximumDegreeOfParallelism
{
get
{
return _index.MaximumDegreeOfParallelism;
}
set
{
_index.MaximumDegreeOfParallelism = value;
}
}
public string DisplayName
{
get
{
return (string)GetExtPropertyValue("DisplayName");
}
set
{
SetExtPropertyValue("DisplayName", value);
}
}
public string Description
{
get
{
return (string)GetExtPropertyValue("Description");
}
set
{
SetExtPropertyValue("Description", value);
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Server s = new Server(".\\sqlexpress");
s.ConnectionContext.SqlExecutionModes = SqlExecutionModes.CaptureSql;
foreach (Table t in s.Databases["test"].Tables)
{
foreach (Index i in t.Indexes)
{
if (i.IndexKeyType == IndexKeyType.DriPrimaryKey)
{
i.Drop();
break;
}
}
}
s.Alter();
foreach (string st in s.ConnectionContext.CapturedSql.Text)
{
richTextBox1.AppendText(st);
}
}
}
}
=====
结果:
USE [test]/****** 对象: Index [PK_user] 脚本日期: 06/07/2007 08:55:54 ******/
ALTER TABLE [dbo].[user] DROP CONSTRAINT [PK_user]
可能是版本问题了,我的结果是这个!
/****** Object: Index [PK_uBoxColumn] Script Date: 06/07/2007 17:33:58 ******/
DROP INDEX [PK_uBoxColumn] ON [dbo].[uBoxColumn] WITH ( ONLINE = OFF )
_____________________________________________________________________________
SQL版本:
Microsoft SQL Server Management Studio 9.00.3042.00
Microsoft Analysis Services Client Tools 2005.090.3042.00
Microsoft Data Access Components (MDAC) 2000.085.1117.00 (xpsp_sp2_rtm.040803-2158)
Microsoft MSXML2.6 3.0 6.0
Microsoft Internet Explorer 6.0.2900.2180
Microsoft .NET Framework2.0.50727.42
Operating System5.1.2600
楼上仁兄,看到你的回答我非常兴奋。
我也是使用这样的方法,可是报错如下:
会停留类似于:i.Drop()上面。
An explicit DROP INDEX is not allowed on index 'dbo.Article.PK_Article'. It is being used for PRIMARY KEY constraint enforcement.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Server s = new Server(".\\sqlexpress");
foreach (Table t in s.Databases["test"].Tables)
{
foreach (Index i in t.Indexes)
{
if (i.IndexKeyType == IndexKeyType.DriPrimaryKey)
{
i.Drop();
break;
}
}
}
s.Alter(); //
}
}
}