Retrieve Database Schema - c#

I want to know, how can I retrieve Sql-Database schema informations such as Tables, Columns and Their relation between Tables and etc. I know it's possible if I execute different particular queries on Master Database and the targeted Database. But Is there any efficient way to retrieve schema of database ?
Thanks in advance.

Take a look at this project on codeplex:
http://dbschemareader.codeplex.com/releases/view/71696

You can obtain the database metadata via GetSchema method of Connection class.

try to follow those articles.
http://support.microsoft.com/kb/309681/en
http://support.microsoft.com/kb/310107/en
Regards

Use the GetSchema method of the SqlConnection class:
DataTable t = _conn.GetSchema("Tables");
For more info read the MSDN article Retrieving Database Schema Information (ADO.NET)..

GetSchema and Schema Collections
The Connection classes in each of the .NET Framework managed providers implement a GetSchema method which is used to retrieve schema information about the database that is currently connected, and the schema information returned from the GetSchema method comes in the form of a DataTable. The GetSchema method is an overloaded method that provides optional parameters for specifying the schema collection to return, and restricting the amount of information returned.
http://msdn.microsoft.com/en-us/library/ms254934(v=vs.110).aspx

Related

Read database schema using NHibernate in c#

I want to give user a choice to select database in our application where number of options will be available like sqlserver,MySql,Sqlite etc.When user selects the type of database,we will show window which will ask user to input the parameters required for DB connection.This will let us connect to user's DB.But Now we want to show all tables and columns from each table to user to select particular tables and columns from it.Is it possible to implement this behavior using NHibernate?Or do I need to use ADO.NET for it?
I have searched web to check if it is possible using NHibernate but I just got one answer here..
Using nHibernate to retrieve Database Schema
I am not able to understand if this answer will solve my problem.
Thanks in advance
Many database servers implement a schema called INFORMATION. This is part of the ANSI Standard for RDBMSs.
It contains data about the schema, tables, and views in the database. I don't know how consistent the INFORMATION structures are across different implementations (e.g. MS SQL, MySQL, etc), or how many of them actually implement it, but that would be the place to start.

Sql Server class in C# for all request?

I'm looking for a class for Sql Server. I need to make insert, update, delete, select (retrieve many rows and columns) and execute Stored Procedure.
I didn't find a sample of this sort of class and i didn't want to reinvente the wheel.
Somebody can give it to me?
You sound like you may be looking for a ORM (Object Relational Mapper). There are a great number available, some built right it to the .NET framework itself. Look at the various websites and see if you can find one that fits your needs.
There's not a single class that does this, but instead a set of a few classes you need to know:
Sql Server specific:
System.Data.SqlClient.SqlConnection
System.Data.SqlClient.SqlCommand
System.Data.SqlClient.SqlDataReader
System.Data.SqlClient.SqlDataAdapter
System.Data.SqlClient.SqlParameter
Used by all database types
System.Data.DataTable
System.Data.DataSet
System.Data.SqlDbType (enum)
There are others as well, but these are the main ones. Together, these make up the ADO.Net API, and the Sql Server provider for the ADO.Net API.
Additionally, there are a number of Object Relational Mappers that build on top of ADO.Net to try to make this easier. Entity Framework, Linq To Sql, and NHibernate are of a few of the more common options. One common characteristic of ORMs is that they try to free you from even knowing the sql language. If you want to write your own SELECT/INSERT/UPDATE/DELETE queries, which it sounds like you do, you should start at the native ADO.Net level.
To put your data access in one object, you create your own class that makes use of these other types. Don't try to build a new public method that accepts an sql string. Build individual methods for each query you will want to run that include the needed sql as part of the method, and have those methods use these types to change or return data.
You might be interested in this tutorial.
There is builtin functionality (System.Data.SqlClient) to simply access an SQL server.
There is no single class that can do everything you need. Whatever choice you decide you would necessarily need to deal with multiple classes.
Look at it this way – in order to get data from SQL Server you need to typically do following things:
Open connection
Crete SQL query
Execute SQL Query
Accept results
Close connection
Putting all this functionality into a single class would make the class way too complex.
Here is a good reading material for what you need.
Beginners guide to accessing SQL Server through C#

How to read SQL server schema info in .net?

How can I know in C# the list of Tables in database.
The list of columns each tables has with complete specification like column one is Id and it has data type of int(50), etc
Use the GetSchema method of the SqlConnection class:
DataTable t = _conn.GetSchema("Tables");
For more info read the MSDN article Retrieving Database Schema Information (ADO.NET). Note that this will get you the results without having to write or directly pass/execute any SQL.
Use the information schem views
http://msdn.microsoft.com/en-us/library/ms186778.aspx
Thy are the "standardized way to look at that and contrary to the sys tables quite guaranteed not to change (while the sys.* tables are an implementation detail).
We have previously used SQL Server Management objects with good success. SMO allows you to interact with the SQL Server quite easily thorugh C#. You just need to reference some DLLs and use the object model provided by them. For example, to list the tables you can use the foreach to iterate over the Database.Tables -property.
Codeproject has an article which goes through the basics: http://www.codeproject.com/KB/database/SMODemo.aspx

Store Relational database as XML

I have to store relational data (text, numbers and pictures) into an XML file. I want to maintain the relationship between data and the tables structure.
How do I go about doing that?
My Windows Mobile application use Windows Communication Foundation (WCF) to get data. Once it gets the data it stores them into an XML file.
The WCF use ADO.NET Entity Framework to data retrieve.
I'm going to use SQL Server 2008.
The data will be use to show it on application. I don't want to use SQL Server CE to store data because it's slower. Maybe, instead of using XML file I can use text plain files to store texts and numbers, and image files to store images.
In short, XML will be a way to store the data instead using SQL Server CE or WCF.
Thank you!
Thanks for the additional detail.
My suggestion is first, try SQL Server CE. It might not actually be slower for what you're doing. The time you save in not reinventing the wheel might be better spent in speeding up other parts of your application.
For the rest, assuming you don't need to store the data for long, nor do much with it, I'd suggest copying the data from the Web Service return type into a DataSet. The DataSet type has a WriteXml method that can save the data, and a ReadXml method that can read it back in.
While it's a little ambiguous what is needed, you can try looking into 'FOR XML EXPLICIT' under SQL Server. This will include relationships. Link: http://www.eggheadcafe.com/articles/20030804.asp
what about a dataset ? define the tables and relations, load the data into the dataset, and save it as XML (WriteXml method)
Define your XML format as an XSD. If you use Visual Studio use add new DataSet and the "custom tool" will automagically create a strong-typed set of classes based on DataSet. You can define tables and relations with constraints. It is easy to do in Visual Studio. If you are Visual Studio averse, you can create the XSD by hand and use xsd.exe to generate your code.
Once you have the strong-typed DataSet filled with data you can serialize it to an XML stream by calling WriteXml.
What do you mean by, "maintain the relationship between data and the tables structure"? Are you talking about more than maintaining the ID of a source database record in an attribute of an XML node? What do you need to do with the XML data once you have it? Will it be edited and have to be re-applied to the database?
More questions than answers from me....sorry.
What SQL Server are you using? Some servers allow you to request an XML response for a query. That could save you a lot of work.
EDIT: Since you have specified that you will use MSSQL 2008, you may be in luck. In MSSQL 2005 and later there is a "FOR XML" keyword which allows you to get results as XML. Give it a try.

create entities for a dynamically created database in SQL Server

I have an application where I create database's dynamically in a SQL Server using Server Management Objects through my application.
I want to use the Entity Framework to access this database when it has been created, is this possible? As I can not generate Entity classes from a database in VS. I do have the structure of the database of course.
So is it possible to create the Entity classes manually and is that a do-able task?
Yes, it's completely possible. You can even manipulate the generated code if you want.
What you might want to take a look is the EDMX XML specification.
In that file you specify the underlying database, views, functions, procedures and the like, as well as the desired objects. Take a look at MSDN in order to have more information.
Paulo is right for EF 1 (+1). For EF 4, I'd suggest using code-only modeling instead.

Categories