Showing posts with label basic. Show all posts
Showing posts with label basic. Show all posts

Monday, March 12, 2012

Programming databases

I need to create a database with one table and its column names.
I got this script from the query analyzer templates as a database basic template:
IF EXISTS (SELECT *
FROM master..sysdatabases
WHERE name = N'<database_name, sysname, test_db>')
DROP DATABASE <database_name, sysname, test_db>
GO

CREATE DATABASE <database_name, sysname, test_db>
GO

I am not sure what it all means, what I need to do it name the database a certain name each time. and then create one table with 6 column names.

can someone give me guidance please...the script is just trying to make sure there is no other db with that name and drop the db one if there is one..

hth|||Not really, heres what I got...


Declare @.name Char(10)
Set @.name = 'test'
CREATE DATABASE [@.name] ON (NAME = N'' & @.name & '_Data', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL\data\' & @.name & '_Data.MDF' , SIZE = 2, FILEGROWTH = 10%) LOG ON (NAME = N'' & @.name & '_Log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL\data\' & @.name & '_Log.LDF' , SIZE = 1, FILEGROWTH = 10%)
COLLATE SQL_Latin1_General_CP1_CI_AS
GO
exec sp_dboption N'@.name', N'autoclose', N'false'
GO

exec sp_dboption N'@.name', N'bulkcopy', N'false'
GO
and so on...

Getting the variable '@.name' to go where I want it is where my problem occurs|||its more like

CREATE DATABASE+ @.name+ ON (NAME = N'' ......

hth|||exec sp_dboption N'hol', N'read only', N'false'
GO

What I need to do is pass, from the web page, a variable called @.name
So where it shows N'hol', the variable passed from the web form textbox whould go where 'hol' is.
N'' + @.name + '', does not work. what is the proper sytax for this?

E|||I suspect you don't understand what the 'N' bit means? (sorry if I'm wrong) You could split the batch up and use proper params. Although I'm a tad concerned that a web users is allowed to issue such commands, but hey I'm sure you've taken care of the security aspect.|||absolutely, what I am trying to do is automate as much as possible. this will be a hidden command in which the user can maintain an image database just by naming it where the form pulls the name from a textbox. Administration only should have access to this sort of command, so its all good with security.
SO tell me more about this spliting the batch up and the use of proper param names you speak of.. No, I am not sure of what the 'N' bit means either. Can you help?|||Params are good because you can literally use
exec spbla @.myParam1, @.myParam2
and you won't have to worry about all the horrible string concat problems.

Lookup SQLParameter.
Plus if your command type is stored procedure you can call spblaa directly!

I think you'll have to split up the batch cause normally client exec'd code doesn't like the "GO"s, although I've not actually tried it with the ADO.net.

I'll whisper the following, you don't really need the 'N' bit. Its just saying that the text is going to be unicode. You rarely have to worry about that, certainly not for params (i.e you don't prefix a param with 'N'). I didn't say that ok ;)

Programming + MSDE 2000

Hi.
I want to be able to programmatically access MSDE
databases. I would rather not use Visual Basic. My
preference would be to either access MSDE databases using
ODBC, or to use a DLL, so that I am more flexible in my
choice of programming tools. If all else fails, then I
would have to use some API (I do not use .NET, the latest
version of Visual C++ I have is 6.0, and I would rather not
have to upgrade). What are my options? Where do I get the
API, if one is necessary?
TIA,
Joseph
From classic VB, ADO is probably your best choice. ADO is a COM object, and
it comes with MDAC (I believe), which you already have on your machine
assuming you have a decently recent OS (W2K or higher). There are some ADO
newsgroups I believe, btw.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
"Joseph" <msde2000@.techscribe.com> wrote in message
news:18a301c4110f$ddb27be0$7d02280a@.phx.gbl...
> Hi.
> I want to be able to programmatically access MSDE
> databases. I would rather not use Visual Basic. My
> preference would be to either access MSDE databases using
> ODBC, or to use a DLL, so that I am more flexible in my
> choice of programming tools. If all else fails, then I
> would have to use some API (I do not use .NET, the latest
> version of Visual C++ I have is 6.0, and I would rather not
> have to upgrade). What are my options? Where do I get the
> API, if one is necessary?
> TIA,
> Joseph
|||"Joseph" <msde2000@.techscribe.com> wrote in message
news:18a301c4110f$ddb27be0$7d02280a@.phx.gbl...
> Hi.
> I want to be able to programmatically access MSDE
> databases. I would rather not use Visual Basic. My
> preference would be to either access MSDE databases using
> ODBC, or to use a DLL, so that I am more flexible in my
> choice of programming tools. If all else fails, then I
> would have to use some API (I do not use .NET, the latest
> version of Visual C++ I have is 6.0, and I would rather not
> have to upgrade). What are my options? Where do I get the
> API, if one is necessary?
> TIA,
> Joseph
As you mention C++ and VB 6, I'll assume you're doing Windows apps?
There are MFC classes that support database access, since you say you'd
prefer not to use VB.
CDatabase, CRecordset, etc are what you want.
Peter [MVP Academic]
|||MSDE 2000 supports the same API set as the database engine in the other
versions of SQL Server 2000:
ADO.NET using the SQLClient namespace.
ADO and OLE DB using the SQLOLEDB provider
ODBC using the SQL Server ODBC Driver.
For more information, see this topic in the latest update to the SQL Server
2000 Books Online:
http://msdn.microsoft.com/library/?u...asp?frame=true
You can also download the latest version of the Books Online from this
location:
http://www.microsoft.com/sql/techinf...2000/books.asp
Alan Brewer [MSFT]
Lead Programming Writer
SQL Server Documentation Team
This posting is provided "AS IS" with no warranties, and confers no rights

Friday, March 9, 2012

Programmatically Deleting an Instance

I'm trying to just get a basic NS app up and running. While I can get through the samples just fine, it seems there are no samples that programmatically manage Instances/Applications, etc. I've googled around and not found anything either. Essentially it looks to me that everyone uses the IDF/ADF files. The SDK has some very sparse documentation on it, so I've started there and tried to get things working.

I have code to create the instance/application programmatically, but when I want to drop the whole thing and create it again, upon calling instance.Create() I get an exception (actually this is what the innerException says) saying:

Notification Services failed to read the NSVersionInfo table.

I’m following the example in the SDK (doing Disable/UnregisterLocal/Drop). Interestingly when I do the Disable/Unregister/Drop in SQL Management Studio it works fine. It's also interesting to note that when I ignore the exception it comes up once more, but after that the instance and associated databases seem to be created.

Here’s the short version of my code (getFreshInstance() is the entry method):


private void configureInstance(nmo.Instance instance)
{
prototypeApplication = new nmo.Application(instance, applicationName);

AddDeliveryChannels(instance);
configureApp(prototypeApplication);
instance.Applications.Add(prototypeApplication);
}

private void configureApp(nmo.Application app)
{
AddGenerator(app);
AddDistributor(app);
}

private void checkAndDrop(string name)
{
if (notificationServices.Instances.Contains(name))
{
nmo.Instance instance = new nmo.Instance(notificationServices, name);
instance.Refresh();

instance.Disable();
instance.UnregisterLocal();
instance.Drop();
}
}

private nmo.Instance getFreshInstance()
{
nmo.Instance instance = new nmo.Instance(notificationServices, instanceName);

checkAndDrop(instanceName);

configureInstance(instance);

instance.Create();
instance.RegisterLocal();
instance.Enable();

return instance;
}

Anyone know what I'm doing wrong?

Ok, seems you've asked a question not many folks in the wild know the answer to :-).

I've tracked down what is probably the answer care of Shyam Pather:

He’s creating a new in-memory object to reflect his instance, like this:

Nmo.Instance instance = new Nmo.Instance(…, “InstanceName”);

Instead, he should do something like:

Smo.Server s = new Smo.Server(…);

Nmo.Instance instance = s.NotificationServices.Instances[“InstanceName”];

That will get him an instance object that’s already populated with the metadata from the database and actually represents the existing instance.

The FlightNMO sample that comes with SQL Server 2005 should also provide you a working example of how to make the API calls.

Hope that helps,

-Lukasz


This posting is provided "AS IS" with no warranties, and confers no rights.

|||

I just gave that snippet a try and unfortunately, I ended up with the same error when I attempt to enable. The need for this functionality in my project has been removed by a change in design, so I'm no longer really worried about this, but I would still be curious to know the cause of this or hear confirmation of someone else running into this.

Now that you mention it, I vaguely remember seeing the FlightNMO sample when I first looked at NS, but had since forgotten it. Thanks! I'm sure that'll prove quite useful later. As far as this particular functionality though, I'm afraid FlightNMO doesn't ever delete the instance, only create.

Thanks for your help,

-Francis