![](/img/trans.png)
[英]How can I determine whether a column exists in a SQL Server CE table with C#?
[英]How can I know in C# if a column of a table in SQL Server is autonumeric?
我需要在C#中知道SQL Server 2005中表的列是否為自動數字。 我知道,如果我查詢以獲取DataTable並遍歷各列,則可以使用類似
if (table.Columns[i].AutoIncrement) bla bla
問題在於,即使該列是Identity and autoincrement列,AutoIncrement始終為false,除了這種方式,我不知道如何找出來。
不過,我想對Access數據庫也知道這一點。
非常感謝你!!
您需要執行的操作是調用GetSchema
方法以檢索數據庫模式/元數據,而不只是數據庫中的數據,請在此處查看:
您可以使用Microsoft.SqlServer.Management.Smo
命名空間為SQL Server 2005
實現此目的- 單擊此處以獲取更多信息
string conn = string.Format(@"Data Source={0};Initial Catalog={1};User ID={2};Password={3}",
"DATASOURCENAME", "DB", "USERNAME", "PASSWORD");
Microsoft.SqlServer.Management.Smo.Server s = new Microsoft.SqlServer.Management.Smo.Server(
new Microsoft.SqlServer.Management.Common.ServerConnection(
new System.Data.SqlClient.SqlConnection(
conn)));
Microsoft.SqlServer.Management.Smo.Database db =
s.Databases["YOUR_DATA_BASE_NAME"];
Microsoft.SqlServer.Management.Smo.Table tbl =
db.Tables[0];//Or you can get the table by table name
List<Microsoft.SqlServer.Management.Smo.Column> autoIncrementClmns =
new List<Microsoft.SqlServer.Management.Smo.Column>();
foreach (Microsoft.SqlServer.Management.Smo.Column clmn in tbl.Columns)
{
if (clmn.IdentityIncrement > 0)//Check if this column is AutoIncrement
autoIncrementClmns.Add(clmn);
}
SMO
所需的組件是:
在YOUR_SYSTEM_DRIVE_NAME:\\Program Files\\Microsoft SQL Server\\100\\SDK\\Assemblies\\
找到YOUR_SYSTEM_DRIVE_NAME:\\Program Files\\Microsoft SQL Server\\100\\SDK\\Assemblies\\
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.