473,789 Members | 3,013 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Server 2005 Schema from C#

JPS
I need to know how to connect to a SQL Server 2005 server, that
contains multiple databases.
I need to be able to read in the collection of Databases and get the
tables in each and the properties of each field in each table. I have
done this in VB6, but I cannot figure out how to do this in C#

Jan 4 '07 #1
5 7174
JPS wrote:
I need to know how to connect to a SQL Server 2005 server, that
contains multiple databases.
I need to be able to read in the collection of Databases and get the
tables in each and the properties of each field in each table. I have
done this in VB6, but I cannot figure out how to do this in C#
The traditional way must be:
sp_databases
sp_tables
sp_columns

Here are an old example using the two first:

using System;
using System.Data;
using System.Data.Sql Client;

class MainClass
{
public static void Main(string[] args)
{
SqlConnection con = new
SqlConnection(" server=ARNEPC2; Integrated Security=SSPI;d atabase=master" );
con.Open();
SqlCommand cmd = new SqlCommand("sp_ databases", con);
cmd.CommandType = CommandType.Sto redProcedure;
SqlDataReader rdr = cmd.ExecuteRead er();
while(rdr.Read( )) {
string dbnam = (string)rdr[0];
Console.WriteLi ne("Database=" + dbnam);
SqlConnection con2 = new
SqlConnection(" server=ARNEPC2; Integrated Security=SSPI;d atabase=" + dbnam);
con2.Open();
SqlCommand cmd2 = new SqlCommand("sp_ tables", con2);
cmd2.CommandTyp e = CommandType.Sto redProcedure;
cmd2.Parameters .Add("@table_ty pe", "'TABLE'");
SqlDataReader rdr2 = cmd2.ExecuteRea der();
while(rdr2.Read ()) {
string tblnam = (string)rdr2[2];
Console.WriteLi ne(dbnam + " " + tblnam);
}
con2.Close();
}
con.Close();
}
}

Alternatively you could use INFORMATION_SCH EMA.

Arne

Jan 5 '07 #2
Look at the GetSchema method on the SqlConnection class.

"Arne Vajhøj" wrote:
JPS wrote:
I need to know how to connect to a SQL Server 2005 server, that
contains multiple databases.
I need to be able to read in the collection of Databases and get the
tables in each and the properties of each field in each table. I have
done this in VB6, but I cannot figure out how to do this in C#

The traditional way must be:
sp_databases
sp_tables
sp_columns

Here are an old example using the two first:

using System;
using System.Data;
using System.Data.Sql Client;

class MainClass
{
public static void Main(string[] args)
{
SqlConnection con = new
SqlConnection(" server=ARNEPC2; Integrated Security=SSPI;d atabase=master" );
con.Open();
SqlCommand cmd = new SqlCommand("sp_ databases", con);
cmd.CommandType = CommandType.Sto redProcedure;
SqlDataReader rdr = cmd.ExecuteRead er();
while(rdr.Read( )) {
string dbnam = (string)rdr[0];
Console.WriteLi ne("Database=" + dbnam);
SqlConnection con2 = new
SqlConnection(" server=ARNEPC2; Integrated Security=SSPI;d atabase=" + dbnam);
con2.Open();
SqlCommand cmd2 = new SqlCommand("sp_ tables", con2);
cmd2.CommandTyp e = CommandType.Sto redProcedure;
cmd2.Parameters .Add("@table_ty pe", "'TABLE'");
SqlDataReader rdr2 = cmd2.ExecuteRea der();
while(rdr2.Read ()) {
string tblnam = (string)rdr2[2];
Console.WriteLi ne(dbnam + " " + tblnam);
}
con2.Close();
}
con.Close();
}
}

Alternatively you could use INFORMATION_SCH EMA.

Arne

Jan 5 '07 #3
JPS
Arne,
What does INFORMATION_SCH EMA. do and what should I replace in this
code?

Thanks,
John

On Jan 4, 9:14 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
JPS wrote:
I need to know how to connect to a SQL Server 2005 server, that
contains multiple databases.
I need to be able to read in the collection of Databases and get the
tables in each and the properties of each field in each table. I have
done this in VB6, but I cannot figure out how to do this in C#The traditional way must be:
sp_databases
sp_tables
sp_columns

Here are an old example using the two first:

using System;
using System.Data;
using System.Data.Sql Client;

class MainClass
{
public static void Main(string[] args)
{
SqlConnection con = new
SqlConnection(" server=ARNEPC2; Integrated Security=SSPI;d atabase=master" );
con.Open();
SqlCommand cmd = new SqlCommand("sp_ databases", con);
cmd.CommandType = CommandType.Sto redProcedure;
SqlDataReader rdr = cmd.ExecuteRead er();
while(rdr.Read( )) {
string dbnam = (string)rdr[0];
Console.WriteLi ne("Database=" + dbnam);
SqlConnection con2 = new
SqlConnection(" server=ARNEPC2; Integrated Security=SSPI;d atabase=" +dbnam);
con2.Open();
SqlCommand cmd2 = new SqlCommand("sp_ tables", con2);
cmd2.CommandTyp e = CommandType.Sto redProcedure;
cmd2.Parameters .Add("@table_ty pe", "'TABLE'");
SqlDataReader rdr2 = cmd2.ExecuteRea der();
while(rdr2.Read ()) {
string tblnam = (string)rdr2[2];
Console.WriteLi ne(dbnam + " " + tblnam);
}
con2.Close();
}
con.Close();
}

}Alternatively you could use INFORMATION_SCH EMA.

Arne
Jan 5 '07 #4
JPS wrote:
What does INFORMATION_SCH EMA. do and what should I replace in this
code?
SELECT * FROM INFORMATION_SCH EMA.TABLES
SELECT * FROM INFORMATION_SCH EMA.COLUMNS

vil get tables and columns - I can not find one for databases.

You can call them just like any other queries.

Arne
Jan 6 '07 #5
>I need to know how to connect to a SQL Server 2005 server, that
>contains multiple databases.
I need to be able to read in the collection of Databases and get the
tables in each and the properties of each field in each table. I have
done this in VB6, but I cannot figure out how to do this in C#
In VS 2005 (.NET 2.0), you can use the SMO (SQL Management Objects) to
deal with this. They allow you to connect to a given SQL server and
explore all its databases, all the database objects inside that
database, and a lot more.

Add a reference to the "Microsoft.SqlS erver.Smo" assembly to your
project, and then add a "using Microsoft.SqlSe rver.Management .Smo"
clause to your file, and you're ready to go!

I'm sure you'll find plenty of info and tutorials and code samples on
sites like www.codeproject.com and others about how to use SMO in
detail - go explore!

Marc
Jan 8 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
990
by: David Lozzi | last post by:
Howdy, My company is setting up a new dedicated hosting server and we're debating using the new SQL 2005 verse the MSDE. I'm not familiar with the new version, so I have one big question:: Outside of the connection string, will any syntax in my existing ASP.NET script/stored procs have to change to support SQL 2005? Is the migration process painless? I've searched MSDN but really can't find what I'm looking for.
2
6969
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
6
2721
by: Amber | last post by:
do the following steps: 1:Use Manage Studio login the server with Integrated security. 2:Create a dabase named testdb; 3:Create a SQL Server login named amber ,and set it to be dbowner of testdb; 4:Create a SQL Server login named guxiaobo ,set it's default databse to
1
1665
by: System Audit | last post by:
I am working with an old version of SQL Server (6.5), but the same mechanism may exist in later versions. If there were any changes made to the database schema, would this be recorded somewhere within the database? Would there be some way of determining if there had been any schema changes within the past year? I am presuming all schema changes would be mediated by the SQL command:
0
5385
by: TechWitch | last post by:
I've seen a number of posts from frustrated folks about this topic, with not much offered as a solution. I actually managed to get this to work today, so I thought I'd post for the benefit of the community. My team went through a number of puzzling errors in getting this work, like the CLI0150E (Driver not capable) error, and a variety of DB2OLEDB errors. None which were really indicative of the problem. Here's how I got a read/write...
8
2735
by: send.me.all.email | last post by:
Hi experts, which approaches would you suggest for: - Reading a database schema (tables, fields, relationships) from SQL Server 2005? - Visualizing the DB schema? For developing a DB tool the schema should be read from the server and then be visualized, e.g. like the SQL Server BI Development Studio
16
4924
by: Omar Abid | last post by:
Hi every body, Im using VB 2005 to create a program that open SQL Data base The problem that i want to detect the tables of a database so how can i know a data base tables instantly Thank you omar.abid@hotmail.com omar.abid2006@gmail.com Omar abid
11
3176
by: Dimitri Furman | last post by:
SQL Server 2005 SP2 (build 3054) Consider the following scenario: - A complex multi-statement table valued function is created. Let's call it dbo.tfFunc(@Param1, @Param2) - A SELECT statement is executed, that calls the above function twice, each time with a different set of parameters. In pseudocode: SELECT <column list>
1
3009
by: Erwin Moller | last post by:
Hi Group, (I am just starting with SQL Server 2005.) On SQL Server 7 I used often the nice relation schema, where I used to draw out the whole database, especially the Foreign Keys constraints. I found these relational schemas very handy to study an old database I build a few years earlier that needs some updating. I tried to find such an utility in SQL Server 2005, but cannot find it.
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10199
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.