Wednesday, March 28, 2012

Problems with SqlExecutionModes.CaptureSql

After setting the Server.ConnectionContext.SqlExecutionModes = SqlExecutionModes.CaptureSql and run an object.Alter() the only thing that shows up in Server.ConnectionContext.CapturedSQL.Text is the Servername and Instance twice.I was wondering if anyone else was having similar problems or what I might be doing wrong.

Thanks

Bradley

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

Server s = new Server(@.".\SQLExpress");
s.ConnectionContext.LoginSecure = true;
s.ConnectionContext.Connect();
s.ConnectionContext.SqlExecutionModes = SqlExecutionModes.CaptureSql;

Table t = s.Databases["DBToolsA"].Tables[0];
Column c = new Column(t, "Giddy", DataType.Bit);
t.Columns.Add(c);
t.Alter();

foreach (string str in s.ConnectionContext.CapturedSql.Text)
{
Console.WriteLine(s);
}
Console.ReadLine();

}
}
}

Okay so the code would work if I changed

foreach(string str in s.ConnectionContext.CapturedSql.Text)
{

Console.WriteLine(s);

}

To:

foreach(string str in s.ConnectionContext.CapturedSql.Text)
{

Console.WriteLine(str);

}

Guess that's what happens when you look at the same problem for to long.

Bradley

No comments:

Post a Comment