Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 16
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2005
    Messaggi
    615

    [C# 2005] Si crea il database ma non le tabelle

    (PER ALKA: SCUSAMI! Hai proprio ragione è stata una mia distrazione. Starò più attento!)


    Carissimi,
    sicuramente sto sbagliando io qualcosa.

    Voglio fare in modo che al primo avvio la mia win app in C# 2005 crei il database, se non già presente. Allora ho creato il mio file script di creazione (che funziona, tant'è che eseguendolo da SSMS si crea tutto, database e tutti gli oggetti, tabelle ecc). Se però lo faccio eseguire dal mio software - il cui frammento di codice che lo crea vi posto sotto - sentite cosa accade: si crea il database ma senza oggetti dentro. La cosa stranissima è che 'esplorando' SSMS ho scoperto che gli oggetti si creano nel database Master!!!! Qualcuno di voi sa spiegarmi l'arcanissimo? Mi pare davvero strano. L'unica certezza è che sicuramente avrò commesso io un errore. Grazie a tutti e buna serata,

    Shadow

    CODICE

    //Questa classe fa semplicemente eseguire un generico comando sql

    public static int SqlComEx(string cnSqlComEx, string cmdSqlComEx, CommandType typSqlComEx, SqlParameter[] parSqlComEx)
    {
    int retSqlComEx = 0;
    SqlConnection cnMySqlComEx;
    cnMySqlComEx = new SqlConnection(cnSqlComEx);
    cnMySqlComEx.Open();
    SqlCommand cmdMySqlComEx = new SqlCommand();
    cmdMySqlComEx.Connection = cnMySqlComEx;
    cmdMySqlComEx.CommandText = cmdSqlComEx;
    cmdMySqlComEx.CommandType = typSqlComEx;
    if (parSqlComEx != null)
    {
    cmdMySqlComEx.Parameters.AddRange(parSqlComEx);
    }
    retSqlComEx = cmdMySqlComEx.ExecuteNonQuery();
    if (cnMySqlComEx != null)
    {
    if (cnMySqlComEx.State == ConnectionState.Open)
    {
    cnMySqlComEx.Close();
    }
    }
    return (retSqlComEx);
    }


    // Quest'altra invece legge lo script e lo esegue con la classe di sopra
    // Tenta infatti di connettersi al database DbVeterinario, e se non lo
    // trova provvede alla sua creazione

    public void DatabaseCreator()
    {
    string dbCrCn = "Data Source=.;Initial Catalog=master;Integrated Security=True";
    string dbCrRw = File.ReadAllText("dbVeterinario.sql");
    string [] dbCrCmd = dbCrRw.Split(new string [] {"GO","Go","gO","go"},StringSplitOptions.RemoveEmp tyEntries);
    SqlConnection dbExMyCn;
    dbExMyCn = new SqlConnection("Data Source=.;Initial Catalog=DbVeterinario;Integrated Security=True");
    try
    {
    dbExMyCn.Open();
    }
    catch
    {
    for (int dbCrIndex = 0; dbCrIndex < dbCrCmd.Length; dbCrIndex++)
    {
    SqlComEx(dbCrCn, dbCrCmd[dbCrIndex], CommandType.Text, null);
    }
    }
    }

  2. #2
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,472
    Originariamente inviato da Shadow976
    Allora ho creato il mio file script di creazione (che funziona, tant'è che eseguendolo da SSMS si crea tutto, database e tutti gli oggetti, tabelle ecc). Se però lo faccio eseguire dal mio software - il cui frammento di codice che lo crea vi posto sotto - sentite cosa accade: si crea il database ma senza oggetti dentro. La cosa stranissima è che 'esplorando' SSMS ho scoperto che gli oggetti si creano nel database Master!!!! Qualcuno di voi sa spiegarmi l'arcanissimo?
    Secondo me, molto semplicemente, ciò è dovuto al fatto che ti colleghi al database Master, e non al tuo.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2005
    Messaggi
    615
    Alka, grazie innanzitutto per il tuo aiuto.

    Ma così mi è stato suggerito! Infatti, quando il database ancora non c'è, devo eseguire i comandi di creazione database connettendomi per forza a qualcosa e quindi mi collego al master. Hai qualche suggerimento? Potresti indicarmi qualche modifica al codice secondo te corretta? Grazie per la tua tempestività.

  4. #4
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Due domande:

    1) ci mostri il contenuto dello script

    dbVeterinario.sql


    2) perche' fai questo

    string [] dbCrCmd = dbCrRw.Split(new string [] {"GO","Go","gO","go"},StringSplitOptions.RemoveEmp tyEntries);

    e che risultato hai dopo?

    Ho il sospetto che tu elimini il GO che ti permette di selezionare il database appena creato e, di conseguenza, il database corrente (il master) rimane selezionato e tutto finisce lì ...

  5. #5
    Utente di HTML.it
    Registrato dal
    May 2005
    Messaggi
    615
    Copio sotto il file! Grazie Oregon. Il risultato ottenuto è che non si crea alcun messaggio di errore; si crea correttamente il solo database; però tabelle e viste si creano nel database Master e non in quello Veterinario. Grazie...


    USE [master]
    GO
    /****** Oggetto: Database [DbVeterinario] Data script: 05/22/2007 18:30:04 ******/
    CREATE DATABASE [DbVeterinario] ON PRIMARY
    ( NAME = N'DbVeterinario', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\DATA\DbVeterinario.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
    LOG ON
    ( NAME = N'DbVeterinario_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\DATA\DbVeterinario_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
    GO
    EXEC dbo.sp_dbcmptlevel @dbname=N'DbVeterinario', @new_cmptlevel=90
    GO
    IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
    begin
    EXEC [DbVeterinario].[dbo].[sp_fulltext_database] @action = 'disable'
    end
    GO
    ALTER DATABASE [DbVeterinario] SET ANSI_NULL_DEFAULT OFF
    GO
    ALTER DATABASE [DbVeterinario] SET ANSI_NULLS OFF
    GO
    ALTER DATABASE [DbVeterinario] SET ANSI_PADDING OFF
    GO


    (QUI LO TAGLIO PERCHE' E' TROPPO LUNGO!)



    ALTER DATABASE [DbVeterinario] SET DB_CHAINING OFF
    GO
    USE [DbVeterinario]
    GO
    /****** Oggetto: User [local\Antonio] Data script: 05/22/2007 18:30:04 ******/
    CREATE USER [local\Antonio] FOR LOGIN [local\Antonio] WITH DEFAULT_SCHEMA=[dbo]
    GO
    /****** Oggetto: Table [dbo].[tblVis] Data script: 05/22/2007 18:30:04 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[tblVis](
    [IDVis] [int] IDENTITY(1,1) NOT NULL,
    [IDVisCli] [int] NULL,
    [Data] [datetime] NULL,
    [AnimaleVisitato] [nvarchar](50) NULL,
    [Condizioni] [nvarchar](50) NULL,
    [MotivoVisita] [nvarchar](50) NULL,
    [Esito] [nvarchar](50) NULL,
    [Note] [nvarchar](200) NULL,
    [Immagine1] [image] NULL,
    [Immagine2] [image] NULL,
    [Immagine3] [image] NULL,
    [NumeroFattura] [int] NULL,
    CONSTRAINT [PK_tblVis] PRIMARY KEY CLUSTERED
    (
    [IDVis] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    GO
    /****** Oggetto: Table [dbo].[tblMedAnag] Data script: 05/22/2007 18:30:04 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[tblMedAnag](
    [IDMedicina] [int] IDENTITY(1,1) NOT NULL,
    [Denominazione] [nvarchar](50) NULL,
    CONSTRAINT [PK_tblMedAnag] PRIMARY KEY CLUSTERED
    (
    [IDMedicina] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    /****** Oggetto: Table [dbo].[tblMedMov] Data script: 05/22/2007 18:30:04 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[tblMedMov](
    [IDMov] [int] IDENTITY(1,1) NOT NULL,
    [Data] [datetime] NULL,
    [Medicinale] [nvarchar](50) NULL,
    [TipoMovimento] [nvarchar](50) NULL,
    [Quantità] [int] NULL,
    CONSTRAINT [PK_tblMedMov] PRIMARY KEY CLUSTERED
    (
    [IDMov] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    /****** Oggetto: Table [dbo].[tblPrestazioni] Data script: 05/22/2007 18:30:04 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[tblPrestazioni](
    [IDPre] [int] IDENTITY(1,1) NOT NULL,
    [IDVisita] [int] NULL,
    [Prestazione] [nvarchar](50) NULL,
    CONSTRAINT [PK_tblPrestazioni] PRIMARY KEY CLUSTERED
    (
    [IDPre] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    /****** Oggetto: Table [dbo].[tblCliente] Data script: 05/22/2007 18:30:04 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[tblCliente](
    [IDCli] [int] IDENTITY(1,1) NOT NULL,
    [Cognome] [nvarchar](50) NULL,
    [Nome] [nvarchar](50) NULL,
    [Indirizzo] [nvarchar](50) NULL,
    [CAP] [nvarchar](50) NULL,
    [Città] [nvarchar](50) NULL,
    [Provincia] [nvarchar](50) NULL,
    [CF] [nvarchar](50) NULL,
    [PI] [nvarchar](50) NULL,
    [Telefono] [nvarchar](50) NULL,
    [Fax] [nvarchar](50) NULL,
    [Cellulare] [nvarchar](50) NULL,
    [Email] [nvarchar](50) NULL,
    [Note] [nvarchar](200) NULL,
    CONSTRAINT [PK_tblCliente] PRIMARY KEY CLUSTERED
    (
    [IDCli] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    /****** Oggetto: View [dbo].[visIn] Data script: 05/22/2007 18:30:05 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE VIEW [dbo].[visIn]
    AS
    SELECT Medicinale, TipoMovimento, SUM(Quantità) AS Expr1
    FROM dbo.tblMedMov
    GROUP BY Medicinale, TipoMovimento
    HAVING (TipoMovimento = N'[Entrata]')
    GO
    EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
    Begin DesignProperties =
    Begin PaneConfigurations =
    Begin PaneConfiguration = 0
    NumPanes = 4
    Configuration = "(H (1[40] 4[20] 2[20] 3) )"
    End
    Begin PaneConfiguration = 1
    NumPanes = 3
    Configuration = "(H (1 [50] 4 [25] 3))"
    End


    (QUI LO TAGLIO PERCHE' E' TROPPO LUNGO!)



    NumPanes = 1
    Configuration = "(V (2))"
    End
    ActivePaneConfig = 0
    End
    Begin DiagramPane =
    Begin Origin =
    Top = 0
    Left = 0
    End
    Begin Tables =
    Begin Table = "tblMedMov"
    Begin Extent =
    Top = 6
    Left = 38
    Bottom = 123
    Right = 219
    End
    DisplayFlags = 280
    TopColumn = 1
    End
    End
    End
    Begin SQLPane =
    End
    Begin DataPane =
    Begin ParameterDefaults = ""
    End
    End
    Begin CriteriaPane =
    Begin ColumnWidths = 12
    Column = 1440
    Alias = 900
    Table = 1170
    Output = 720
    Append = 1400
    NewValue = 1170
    SortType = 1350
    SortOrder = 1410
    GroupBy = 1350
    Filter = 1350
    Or = 1350
    Or = 1350
    Or = 1350
    End
    End
    End
    ' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'visIn'
    GO
    EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'visIn'
    GO
    /****** Oggetto: View [dbo].[visOut] Data script: 05/22/2007 18:30:05 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE VIEW [dbo].[visOut]
    AS
    SELECT Medicinale, TipoMovimento, SUM(Quantità) AS Expr1
    FROM dbo.tblMedMov
    GROUP BY Medicinale, TipoMovimento
    HAVING (TipoMovimento = N'[Uscita]')
    GO
    EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
    Begin DesignProperties =
    Begin PaneConfigurations =
    Begin PaneConfiguration = 0
    NumPanes = 4
    Configuration = "(H (1[40] 4[20] 2[20] 3) )"
    End
    Begin PaneConfiguration = 1
    NumPanes = 3
    Configuration = "(H (1 [50] 4 [25] 3))"
    End
    (QUI LO TAGLIO PERCHE' E' TROPPO LUNGO!)


    End
    Begin PaneConfiguration = 10
    NumPanes = 2
    Configuration = "(H (1[66] 2) )"
    End
    Begin PaneConfiguration = 11
    NumPanes = 2
    Configuration = "(H (4 [60] 2))"
    End
    Begin PaneConfiguration = 12
    NumPanes = 1
    Configuration = "(H (1) )"
    End
    Begin PaneConfiguration = 13
    NumPanes = 1
    Configuration = "(V (4))"
    End
    Begin PaneConfiguration = 14
    NumPanes = 1
    Configuration = "(V (2))"
    End
    ActivePaneConfig = 0
    End
    Begin DiagramPane =
    Begin Origin =
    Top = 0
    Left = 0
    End
    Begin Tables =
    Begin Table = "tblMedMov"
    Begin Extent =
    Top = 6
    Left = 38
    Bottom = 123
    Right = 219
    End
    DisplayFlags = 280
    TopColumn = 1
    End
    End
    End
    Begin SQLPane =
    End
    Begin DataPane =
    Begin ParameterDefaults = ""
    End
    End
    Begin CriteriaPane =
    Begin ColumnWidths = 12
    Column = 1440
    Alias = 900
    Table = 1170
    Output = 720
    Append = 1400
    NewValue = 1170
    SortType = 1350
    SortOrder = 1410
    GroupBy = 1350
    Filter = 1350
    Or = 1350
    Or = 1350
    Or = 1350
    End
    End
    End
    ' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'visOut'
    GO
    EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'visOut'
    GO
    /****** Oggetto: View [dbo].[visSum] Data script: 05/22/2007 18:30:05 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE VIEW [dbo].[visSum]
    AS
    SELECT dbo.tblMedAnag.Denominazione, dbo.visIn.Expr1 AS MedIn, dbo.visOut.Expr1 AS MedOut, 'Expr3 = [MedIn] - [MedOut]' AS MedSomma
    FROM dbo.tblMedAnag LEFT OUTER JOIN
    dbo.visIn ON dbo.tblMedAnag.Denominazione = dbo.visIn.Medicinale LEFT OUTER JOIN
    dbo.visOut ON dbo.tblMedAnag.Denominazione = dbo.visOut.Medicinale
    GO
    EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
    Begin DesignProperties =
    Begin PaneConfigurations =
    Begin PaneConfiguration = 0
    NumPanes = 4
    Configuration = "(H (1[54] 4[22] 2[5] 3) )"
    End
    Begin PaneConfiguration = 1
    NumPanes = 3
    Configuration = "(H (1 [50] 4 [25] 3))"


    (QUI LO TAGLIO PERCHE' E' TROPPO LUNGO!)



    Begin PaneConfiguration = 14
    NumPanes = 1
    Configuration = "(V (2))"
    End
    ActivePaneConfig = 0
    End
    Begin DiagramPane =
    Begin Origin =
    Top = 0
    Left = 0
    End
    Begin Tables =
    Begin Table = "tblMedAnag"
    Begin Extent =
    Top = 89
    Left = 22
    Bottom = 176
    Right = 203
    End
    DisplayFlags = 280
    TopColumn = 0
    End
    Begin Table = "visIn"
    Begin Extent =
    Top = 191
    Left = 258
    Bottom = 293
    Right = 439
    End
    DisplayFlags = 280
    TopColumn = 0
    End
    Begin Table = "visOut"
    Begin Extent =
    Top = 39
    Left = 255
    Bottom = 141
    Right = 436
    End
    DisplayFlags = 280
    TopColumn = 0
    End
    End
    End
    Begin SQLPane =
    End
    Begin DataPane =
    Begin ParameterDefaults = ""
    End
    Begin ColumnWidths = 9
    Width = 284
    Width = 1500
    Width = 1500
    Width = 1500
    Width = 1500
    Width = 1500
    Width = 1500
    Width = 1500
    Width = 1500
    End
    End
    Begin CriteriaPane =
    Begin ColumnWidths = 11
    Column = 1440
    Alias = 900
    Table = 1170
    Output = 720
    Append = 1400
    NewValue = 1170
    SortType = 1350
    SortOrder = 1410
    GroupBy = 1350
    Filter = 1350
    Or = 1350
    Or = 1350
    Or = 1350
    End
    End
    End
    ' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'visSum'
    GO
    EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'visSum'
    GO

  6. #6
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,472
    Originariamente inviato da oregon
    Ho il sospetto che tu elimini il GO che ti permette di selezionare il database appena creato e, di conseguenza, il database corrente (il master) rimane selezionato e tutto finisce lì ...
    Più che altro, credo che vengano suddivisi tutti i comandi in base al GO, solo che in questo modo si perdono sia i GO che la selezione del database corrente quando è il momento di aggiungere elementi alla sua struttura, in quanto gli script vengono usati tutti in contesti separati, in questo modo.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  7. #7
    Utente di HTML.it
    Registrato dal
    May 2005
    Messaggi
    615
    Ah! Interessante... Quindi voi come mi suggerireste di correggere il codice C#?

  8. #8
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Sì ... concordo con alka ...

    Ma non puoi inviare il testo dello script sql senza alterarlo e in un solo colpo senza suddividerlo in righe ...?

    Cioe' cosi' ...

    codice:
    public void DatabaseCreator()
    {
       string dbCrCn = "Data Source=.;Initial Catalog=master;Integrated Security=True";
       string dbCrRw = File.ReadAllText("dbVeterinario.sql");
       SqlConnection dbExMyCn;
       dbExMyCn = new SqlConnection("Data Source=.;Initial Catalog=DbVeterinario;Integrated Security=True");
       try
       {
         dbExMyCn.Open();
       }
       catch
       {
         SqlComEx(dbCrCn, dbCrRw, CommandType.Text, null);
      }
    }

  9. #9
    Utente di HTML.it
    Registrato dal
    May 2005
    Messaggi
    615
    come si fa? Mi dice sempre che il messaggio è troppo lungo per essere inviato!

  10. #10
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,472
    Originariamente inviato da Shadow976
    come si fa? Mi dice sempre che il messaggio è troppo lungo per essere inviato!
    Si riferiva probabilmente all'invio del codice al database server...

    Cioè, si chiedevano i motivi per cui spezzi il file con i GO prima di inviarlo al DB.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.