Click here to Skip to main content
16,016,500 members
Home / Discussions / Database
   

Database

 
GeneralRe: Joining fields from several tables onto one SSRS report Pin
Mycroft Holmes13-Mar-12 0:37
professionalMycroft Holmes13-Mar-12 0:37 
QuestionSQL Enable Identity Insert Pin
Zeyad Jalil12-Mar-12 3:32
professionalZeyad Jalil12-Mar-12 3:32 
AnswerRe: SQL Enable Identity Insert Pin
Shameel13-Mar-12 1:34
professionalShameel13-Mar-12 1:34 
Question2013 - Lost Connection to mysql during query Pin
Jassim Rahma11-Mar-12 5:19
Jassim Rahma11-Mar-12 5:19 
AnswerRe: 2013 - Lost Connection to mysql during query Pin
Richard Andrew x6411-Mar-12 8:11
professionalRichard Andrew x6411-Mar-12 8:11 
QuestionDatabase table creation error Pin
solomon20110-Mar-12 10:00
solomon20110-Mar-12 10:00 
AnswerRe: Database table creation error Pin
Luc Pattyn10-Mar-12 10:37
sitebuilderLuc Pattyn10-Mar-12 10:37 
GeneralRe: Database table creation error Pin
solomon20110-Mar-12 17:17
solomon20110-Mar-12 17:17 
I am using SQL Server 2008 R2 with C# running VS2010

The Code that gives the Error is this:

StringBuilder sb = new StringBuilder();

try
{
sb.AppendLine(string.Format("Starting to create {0} with login to {1}", this.tbDBName.Text, this.tbBTSAppUser.Text));
sb.AppendLine("Connecting to DB Server");
this.tbProgress.Text = sb.ToString();
this.tbProgress.ScrollToCaret();
//Connect to the local, default instance of SQL Server.
string srvname = this.cbServers.SelectedItem as string;
Server srv;
if (srvname == null)
{
srv = new Server();
sb.AppendLine("Connected to local SQL server");
}
else
{
srv = new Server(srvname);
sb.AppendLine(string.Format("Connected to {0}", srvname));
}

this.tbProgress.Text = sb.ToString();
this.tbProgress.ScrollToCaret();
//Define a Database object variable by supplying the server and the database name arguments in the constructor.
Database db = srv.Databases[this.tbDBName.Text.Trim()];
if (db != null)
{
if (MessageBox.Show(string.Format("The '{0}' already exists do you want to drop it?", this.tbDBName.Text), "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
db.Drop();
}
else
{
if (MessageBox.Show(string.Format("Create the Tables and Stored Procedures for BT Error Manager on '{0}'?", this.tbDBName.Text), "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
sb.AppendLine("Creating the Tables and Stored Procedures.");
this.tbProgress.Text = sb.ToString();


db.ExecuteNonQuery(dbstring);

string ConnectionString = "Integrated Security=SSPI;" +

sb.AppendLine(string.Format("Created the Tables and Stored Procedures for BT Error Manager on '{0}'", this.tbDBName.Text));
this.tbProgress.Text = sb.ToString();
this.tbProgress.ScrollToCaret();

}
sb.AppendLine("Proceed or select another database");
this.tbProgress.Text = sb.ToString();
this.tbProgress.ScrollToCaret();
return;
}
}
sb.AppendLine("Creating the database.....");
db = new Database(srv, this.tbDBName.Text);
this.tbProgress.Text = sb.ToString();
this.tbProgress.ScrollToCaret();
//Create the database on the instance of SQL Server.
db.Create();
sb.AppendLine("Created the database.");
sb.AppendLine("Creating the Tables and Stored Procedures.");
this.tbProgress.Text = sb.ToString();
this.tbProgress.ScrollToCaret();
//'Reference the database and display the date when it was created.
db.ExecuteNonQuery(dbstring);
sb.AppendLine("Success!");
this.tbProgress.Text = sb.ToString();
this.tbProgress.ScrollToCaret();

}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
sb.AppendLine("Failuer:" + ex.Message);
this.tbProgress.Text = sb.ToString();
this.tbProgress.ScrollToCaret();

}

sb.AppendLine("Proceed or select another database");
this.tbProgress.Text = sb.ToString();
this.tbProgress.ScrollToCaret();

}


dbstring is read from a text file it's content is:

-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 03/11/2012 05:12:44
-- Generated from EDMX file: C:\Users\SOLO\Desktop\Second\Anoda Code Project\CreateDB\CreateDB\Model1.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [mydb];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------


-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------


-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Students'
CREATE TABLE [dbo].[Students] (
[Id] int IDENTITY(1,1) NOT NULL,
[FirstName] nvarchar(max) NOT NULL,
[LastName] nvarchar(max) NOT NULL,
[RegID] nvarchar(max) NOT NULL
);
GO

-- Creating table 'Courses'
CREATE TABLE [dbo].[Courses] (
[Id] int IDENTITY(1,1) NOT NULL,
[Code] nvarchar(max) NOT NULL,
[Units] nvarchar(max) NOT NULL,
[Title] nvarchar(max) NOT NULL
);
GO

-- Creating table 'StudentRegs'
CREATE TABLE [dbo].[StudentRegs] (
[Id] int IDENTITY(1,1) NOT NULL,
[Student_Id] int NOT NULL,
[Course_Id] int NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Students'
ALTER TABLE [dbo].[Students]
ADD CONSTRAINT [PK_Students]
PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Courses'
ALTER TABLE [dbo].[Courses]
ADD CONSTRAINT [PK_Courses]
PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'StudentRegs'
ALTER TABLE [dbo].[StudentRegs]
ADD CONSTRAINT [PK_StudentRegs]
PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Student_Id] in table 'StudentRegs'
ALTER TABLE [dbo].[StudentRegs]
ADD CONSTRAINT [FK_StudentStudentReg]
FOREIGN KEY ([Student_Id])
REFERENCES [dbo].[Students]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_StudentStudentReg'
CREATE INDEX [IX_FK_StudentStudentReg]
ON [dbo].[StudentRegs]
([Student_Id]);
GO

-- Creating foreign key on [Course_Id] in table 'StudentRegs'
ALTER TABLE [dbo].[StudentRegs]
ADD CONSTRAINT [FK_CourseStudentReg]
FOREIGN KEY ([Course_Id])
REFERENCES [dbo].[Courses]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CourseStudentReg'
CREATE INDEX [IX_FK_CourseStudentReg]
ON [dbo].[StudentRegs]
([Course_Id]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------
GeneralRe: Database table creation error Pin
Richard MacCutchan11-Mar-12 0:00
mveRichard MacCutchan11-Mar-12 0:00 
GeneralRe: Database table creation error Pin
Mycroft Holmes11-Mar-12 2:06
professionalMycroft Holmes11-Mar-12 2:06 
GeneralRe: Database table creation error Pin
Richard MacCutchan11-Mar-12 2:43
mveRichard MacCutchan11-Mar-12 2:43 
GeneralRe: Database table creation error Pin
PIEBALDconsult11-Mar-12 3:40
mvePIEBALDconsult11-Mar-12 3:40 
Questionsql ROLLUP Pin
byka9-Mar-12 10:59
byka9-Mar-12 10:59 
AnswerRe: sql ROLLUP Pin
R. Giskard Reventlov9-Mar-12 11:41
R. Giskard Reventlov9-Mar-12 11:41 
GeneralRe: sql ROLLUP Pin
Mycroft Holmes9-Mar-12 12:27
professionalMycroft Holmes9-Mar-12 12:27 
GeneralRe: sql ROLLUP Pin
R. Giskard Reventlov9-Mar-12 12:29
R. Giskard Reventlov9-Mar-12 12:29 
QuestionTransfer Database Pin
Zeyad Jalil7-Mar-12 20:47
professionalZeyad Jalil7-Mar-12 20:47 
SuggestionRe: Transfer Database Pin
foxyland7-Mar-12 21:58
foxyland7-Mar-12 21:58 
GeneralRe: Transfer Database Pin
Zeyad Jalil7-Mar-12 22:32
professionalZeyad Jalil7-Mar-12 22:32 
GeneralRe: Transfer Database Pin
rana ray10-Mar-12 1:32
rana ray10-Mar-12 1:32 
SuggestionRe: Transfer Database Pin
sherabhs11-Mar-12 18:47
sherabhs11-Mar-12 18:47 
AnswerRe: Transfer Database Pin
Simon_Whale8-Mar-12 1:12
Simon_Whale8-Mar-12 1:12 
QuestionSQL Query enhancement Pin
wonder-FOOL1-Mar-12 7:57
wonder-FOOL1-Mar-12 7:57 
AnswerRe: SQL Query enhancement Pin
Chris Meech1-Mar-12 9:05
Chris Meech1-Mar-12 9:05 
GeneralRe: SQL Query enhancement Pin
wonder-FOOL1-Mar-12 9:19
wonder-FOOL1-Mar-12 9:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.