SQL Server - Its possible to generate script (.sql) include Schema & Data From C# or VB.NET

 
Vista:
sin imagen de perfil

Its possible to generate script (.sql) include Schema & Data From C# or VB.NET

Publicado por anonymous (5 intervenciones) el 24/07/2015 03:43:36
Its possible to generate script (.sql) include Schema & Data From C# or VB.NET?
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 86
Ha disminuido su posición en 2 puestos en SQL Server (en relación al último mes)
Gráfica de SQL Server

Its possible to generate script (.sql) include Schema & Data From C# or VB.NET

Publicado por Rafael (110 intervenciones) el 24/07/2015 09:31:50
Si estas en un foro en español por que no preguntas en español???

En fin

Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Its possible to generate script (.sql) include Schema & Data From C# or VB.NET

Publicado por anonymous (5 intervenciones) el 24/07/2015 17:08:05
Disculpa es mi segunda pregunta ..

Esta es mi Pregunta

Es posible generar un script desde C # o VB.NET que contenga los datos y el esquema de la base datos.


Gracias de ante mano y gracias rafael por la aclaracion.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Isaias
Val: 3.250
Oro
Ha mantenido su posición en SQL Server (en relación al último mes)
Gráfica de SQL Server

Its possible to generate script (.sql) include Schema & Data From C# or VB.NET

Publicado por Isaias (4558 intervenciones) el 24/07/2015 18:29:10
Tal vez con un código en SMO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Sdk.Sfc;
...
// Connect to the local, default instance of SQL Server. 
Server srv = new Server();
 
// Reference the database.  
Database db = srv.Databases["YOURDBHERE"];
 
Scripter scrp = new Scripter(srv);
scrp.Options.ScriptDrops = false;
scrp.Options.WithDependencies = true;
scrp.Options.Indexes = true;   // To include indexes
scrp.Options.DriAllConstraints = true;   // to include referential constraints in the script
scrp.Options.Triggers = true;
scrp.Options.FullTextIndexes = true;
scrp.Options.NoCollation = false;
scrp.Options.Bindings = true;
scrp.Options.IncludeIfNotExists = false;
scrp.Options.ScriptBatchTerminator = true;
scrp.Options.ExtendedProperties = true;
 
scrp.PrefetchObjects = true; // some sources suggest this may speed things up
 
var urns = new List<Urn>();
 
// Iterate through the tables in database and script each one   
foreach (Table tb in db.Tables)
{
    // check if the table is not a system table
    if (tb.IsSystemObject == false)
    {
        urns.Add(tb.Urn);
    }
}
 
// Iterate through the views in database and script each one. Display the script.   
foreach (View view in db.Views)
{
    // check if the view is not a system object
    if (view.IsSystemObject == false)
    {
        urns.Add(view.Urn);
    }
}
 
// Iterate through the stored procedures in database and script each one. Display the script.   
foreach (StoredProcedure sp in db.StoredProcedures)
{
    // check if the procedure is not a system object
    if (sp.IsSystemObject == false)
    {
        urns.Add(sp.Urn);
    }
}
 
StringBuilder builder = new StringBuilder();
System.Collections.Specialized.StringCollection sc = scrp.Script(urns.ToArray());
foreach (string st in sc)
{
    // It seems each string is a sensible batch, and putting GO after it makes it work in tools like SSMS.
    // Wrapping each string in an 'exec' statement would work better if using SqlCommand to run the script.
    builder.AppendLine(st);
    builder.AppendLine("GO");
}
 
return builder.ToString();

Para saber un poco mas:

https://msdn.microsoft.com/en-us/library/ms162189.aspx

https://msdn.microsoft.com/en-us/library/ms162169.aspx
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar