[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

Using SMO, how to find column by name

moondaddy

3/21/2007 4:04:00 AM

In DMO I could return a column object like this:

PKCol = tbl.Columns.Item(strPKName);

However SMO doesn't have an Item object and I cant find anything that would
work like the line above. Im I going to have to iterate through all the
columns looking for a matching name to the string strPKName?

Thanks.

--
moondaddy@noemail.noemail


5 Answers

Razvan Socol

3/21/2007 6:03:00 AM

0

There is no need for the Item property: just use
tbl.Columns("ColumnName"), for example:

Dim s As New Microsoft.SqlServer.Management.Smo.Server(".\SQLEXPRESS")
Dim t As Microsoft.SqlServer.Management.Smo.Table
Dim c As Microsoft.SqlServer.Management.Smo.Column
t = s.Databases("AdventureWorks").Tables("Contact", "Person")
c = t.Columns("EmailAddress")
MsgBox(c.DataType.ToString)

Razvan

stcheng

3/21/2007 9:33:00 AM

0

Hi Moondaddy,

As Razvan has suggested, in SQL 2005 SMO, you can directly reference Column
collections from a Table instance(in a given database instance). You can
use column's name to reference it. Here are the SQL BOL reference about it:

#How to: Create, Alter, and Remove a Table in Visual Basic .NET
http://msdn2.microsoft.com/it-it/library/ms1...

#TableViewBase.Columns Property
http://msdn2.microsoft.com/en-us/library/microsoft.sqlserver.mana....
tableviewbase.columns.aspx

and below is some test code snippet in C#:

============================
private void btnTestDataObjects_Click(object sender, EventArgs e)
{
Microsoft.SqlServer.Management.Smo.Server server = new
Server(@".\SQLEXPRESS");
Database testdb = server.Databases["ASPNETTestDB"];

Table tb1 = testdb.Tables[0];

Column c_id = tb1.Columns["id"];
Column c_year = tb1.Columns["year"];

string msg = string.Empty;

foreach (Column col in tb1.Columns)
{
msg += "\r\n" + col.Name + "--" + col.DataType;
if (col.InPrimaryKey == true)
{
msg += "----PrimaryKey:";
}
}

MessageBox.Show(msg);
}
=============================


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default....
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/de....

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.





stcheng

3/23/2007 1:18:00 PM

0

Hi Moondaddy,

Have you got any further ideas on this issue? If there is anything else we
can help, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

Scientology is a cult

4/3/2008 11:18:00 PM

0

"John" <john@junk.com> wrote in news:ft3qc4$kbg$1@news-
01.bur.connect.com.au:

> "Scientology is a cult" <Your@ema.yil> wrote in message
> news:Xns9A75CD26BDD63GenyooineLRH@66.150.105.47...
>> "JAFAW" <anon@anon.net> wrote in
>> news:KEaJj.8761$Re3.6142@newsfe4-gui.ntli.net:
>>
>>> In 1958, Hubbard published Have You Lived Before? - a book which
>>> chronicled the experiences of scientologists in past lives.
>>>
>>> These included memories of being "deceived into a love affair with a
>>> robot decked out as a beautiful red-haired girl",
>>
>> I didn't know Elmer Fudd was a Scientologist!
>
> Be vewwy quiet, it's robot season.

KIWW DA WOBOT,
KIWW DA WOBOT!
KIWW DA WOBOT!!
KIWW DA WOBOT!!!!

--
Posted via a free Usenet account from http://www.te...

John

4/3/2008 11:48:00 PM

0


"Scientology is a cult" <Your@ema.yil> wrote in message
news:Xns9A75CD26BDD63GenyooineLRH@66.150.105.47...
> "JAFAW" <anon@anon.net> wrote in
> news:KEaJj.8761$Re3.6142@newsfe4-gui.ntli.net:
>
>> In 1958, Hubbard published Have You Lived Before? - a book which
>> chronicled the experiences of scientologists in past lives.
>>
>> These included memories of being "deceived into a love affair with a
>> robot decked out as a beautiful red-haired girl",
>
> I didn't know Elmer Fudd was a Scientologist!

Be vewwy quiet, it's robot season.