Showing posts with label smo. Show all posts
Showing posts with label smo. Show all posts

Tuesday, March 27, 2012

Adding a new VarBinaryMax column to an existing table using SMO.

Consider the following code.

Table applicationSettingsTable = database.Tables["application_settings"];

Column logoColumn = new Column(applicationSettingsTable, "logo", DataType.VarBinaryMax);

applicationSettingsTable.Columns.Add(logoColumn);

applicationSettingsTable.Alter();

I don't understand why - but when I execute this code I end up with a column that has the data type varbinary(1) in the database rather than varbinary(MAX). I can't see anything in the msdn documentation that tells me I need to do something special for varbinary(max) columns, but maybe I do?

Any help much appreciated.

This appears to work tho...

Column logoColumn = new Column(applicationSettingsTable, "logo", DataType.VarBinary(-1));

Adding a loggin to a server

In DMO the server object had an add method on the Logins collection. Does anyone know how this works in SMO the logins collection does not have an add.

I have spent hours on this and just cannot figure out how to create a new login on a server using SMO. I guess I could allways script the login but there must ba a way to do this in SMO.

Thanks

You can create a login as follows:

Server srv = new Server("MyServer"); //Yukon

srv.ConnectionContext.LoginSecure = true;

srv.ConnectionContext.Connect();

srv.DefaultTextMode = false;

string lgName = "MyNewLogin";

if (srv.Logins[lgName] == null)

{

Login lg = new Login(srv, lgName);

lg.LoginType = LoginType.SqlLogin;

lg.Create("MyPassword!#");

}

Peter

Tuesday, March 20, 2012

Add/Drop LinkedServers using SMO

Anyone used SMO in SQL Server 2005 to add or drop Linked Server. I couldn't
find any sample or information on that.
ThanksHello,
I've never done this, but you probably need to use the LinkedServer
class in the Microsoft.SqlServer.Management.Smo namespace. Set the
desired properties of the LinkedServer, then use the Create method. For
more informations, see:
http://msdn2.microsoft.com/microsof...rs
.aspx
http://msdn2.microsoft.com/ms162171(en-US,SQL.90).aspx
Razvan|||Thanks for the response.
I solved the problem. If anyone interested heres the code for
Adding/Dropping Linked Servers using SMO. I use this in a Class.
public bool AddLinkServer(string SourceServer, string
DestinationServer,string UID,string PWD)
{
try
{
LinkedServer DestSrv;
ServerConnection SrvConn = new ServerConnection();
SrvConn.ServerInstance = SourceServer;
if ((UID == "") && (PWD == ""))
{
SrvConn.LoginSecure = true;
}
else
{
SrvConn.LoginSecure = false;
SrvConn.Login = UID;
SrvConn.Password = PWD;
}
Server SQLServer = new Server(SrvConn);
DestSrv = new LinkedServer(SQLServer, DestinationServer);
// Add Remote user/password for linking.
LinkedServerLogin LnkLogin = new LinkedServerLogin(DestSrv, UID);
if (!SrvConn.LoginSecure)
{
LnkLogin.RemoteUser = UID;
LnkLogin.SetRemotePassword(PWD);
LnkLogin.Create();
}
DestSrv.Create();
SrvConn.Disconnect();
return true;
}
catch
{
return false;
}
}
// drop link serverss...
public bool DropLinkServer(string SourceServer,string DropLinkServerName)
{
try
{
ServerConnection SrvConn = new ServerConnection();
SrvConn.ServerInstance = SourceServer;
SrvConn.LoginSecure = true;
Server SQLServer = new Server(SrvConn);
LinkedServerCollection LnkServerList = SQLServer.LinkedServers;
foreach (LinkedServer Lnk in LnkServerList)
{
if (Lnk.Name == DropLinkServerName)
{
Lnk.Drop(true);
break;
}
}
SrvConn.Disconnect();
return true;
}
catch
{
return false;
}
}
// Load all linked servers...
public bool LoadLinkServers(string SourceServer, ArrayList LinkServerList)
{
try
{
ServerConnection SrvConn = new ServerConnection();
SrvConn.ServerInstance = SourceServer;
SrvConn.LoginSecure = true;
Server SQLServer = new Server(SrvConn);
LinkedServerCollection LnkSrvList = SQLServer.LinkedServers;
foreach (LinkedServer Lnk in LnkSrvList)
{
LinkServerList.Add(Lnk.Name);
}
SrvConn.Disconnect();
return true;
}
catch
{
return false;
}
}
"Razvan Socol" <rsocol@.gmail.com> wrote in message
news:1138559727.598122.180660@.g44g2000cwa.googlegroups.com...
> Hello,
> I've never done this, but you probably need to use the LinkedServer
> class in the Microsoft.SqlServer.Management.Smo namespace. Set the
> desired properties of the LinkedServer, then use the Create method. For
> more informations, see:
> http://msdn2.microsoft.com/microsof...
rs.aspx
> http://msdn2.microsoft.com/ms162171(en-US,SQL.90).aspx
> Razvan
>