I am attempting to export and import a database schema and the data within. I'm using SMO (Server Management Objects) to do so, but while the export of the schema is working just fine, setting ScriptData to true in the scripting options will produce an exception.
Server server = new Server(new ServerConnection(sqlCon));
Database database = server.Databases["SpectroGrass"];
ScriptingOptions options = new ScriptingOptions();
options.ScriptSchema = true;
options.ScriptData = false;
options.ScriptDrops = false;
string scriptData = String.Join("\r\n", database.Tables[tableName].EnumScript(options).ToList());
Will produce a schema and is working.
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [SpectroGrassAdmin].[Treatment]
(
[TRM_ID] [int] NOT NULL,
[TRM_Name] [varchar](150) COLLATE Latin1_General_CI_AS NULL,
[TRM_Crop] [varchar](50) COLLATE Latin1_General_CI_AS NOT NULL
) ON [PRIMARY]
But using the ScriptData option ...
options.ScriptSchema = false;
options.ScriptData = true;
options.ScriptDrops = false;
string scriptData = String.Join("\r\n", database.Tables[tableName].EnumScript(options).ToList());
will generate an exception.
Login failed for user 'SpectroGrassAdmin'.
I'm not sure why I'm getting this specific error. I can only assume the problem lies somewhere else, because logging into the server and connecting to the database is working fine if I only want the schema exported.
Am I missing some other options to be able to create a script for the table data?