473,836 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Example code for database configuration(M SDE)

Does anyone know where I can find example code for VB.NET that I can use
to create an application that attaches and detaches databases to MSDE using
osql scipts.
(I think SQL-DMO is typically used)

Thanks in advance
Jul 20 '05 #1
10 5249
Hi

The following shows example of attaching a database.
http://support.microsoft.com/?id=224071

The following shows the parameters to the osql utility.
http://msdn.microsoft.com/library/de..._osql_1wxl.asp

John

"David" <da***@orbitcom s.com> wrote in message
news:lu******** ***********@new s-server.bigpond. net.au...
Does anyone know where I can find example code for VB.NET that I can use
to create an application that attaches and detaches databases to MSDE using osql scipts.
(I think SQL-DMO is typically used)

Thanks in advance

Jul 20 '05 #2
To add to John's response, another method is to execute sp_attach_db
directly from your application rather than shelling out to OSQL. Example
below.
Public Shared Sub AttachDb( _
ByVal ConnectionStrin g As String, _
ByVal DatabaseName As String, _
ByVal FileList As String())

Try
Dim connection As New SqlConnection(C onnectionString )
connection.Open ()
Dim command As New SqlCommand("sp_ attach_db", connection)
command.Command Type = CommandType.Sto redProcedure

command.Paramet ers.Add( _
New SqlParameter("@ dbname", DatabaseName))
Dim fileNumber As Integer
Dim fileName As String
For fileNumber = 1 To FileList.Length
command.Paramet ers.Add( _
New SqlParameter(St ring.Format("@f ilename{0}",
fileNumber.ToSt ring), _
FileList(fileNu mber - 1)))
Next fileNumber

command.Execute NonQuery()
connection.Clos e()

Catch ex As SqlException
Throw New ApplicationExce ption("Attach database failed: " + _
ex.ToString())
End Try

End Sub

--
Hope this helps.

Dan Guzman
SQL Server MVP

"David" <da***@orbitcom s.com> wrote in message
news:lu******** ***********@new s-server.bigpond. net.au...
Does anyone know where I can find example code for VB.NET that I can use
to create an application that attaches and detaches databases to MSDE using osql scipts.
(I think SQL-DMO is typically used)

Thanks in advance

Jul 20 '05 #3
Dan,

Thanks for the reply. I notice before you attach, you connect using a
database name.
Is this one of the system databases (Master,TempDb. ...) ?, as at this point
we have not attached the db of interest ?

Thanks
"Dan Guzman" <da*******@nosp am-earthlink.net> wrote in message
news:qD******** *******@newsrea d1.news.pas.ear thlink.net...
To add to John's response, another method is to execute sp_attach_db
directly from your application rather than shelling out to OSQL. Example
below.
Public Shared Sub AttachDb( _
ByVal ConnectionStrin g As String, _
ByVal DatabaseName As String, _
ByVal FileList As String())

Try
Dim connection As New SqlConnection(C onnectionString )
connection.Open ()
Dim command As New SqlCommand("sp_ attach_db", connection)
command.Command Type = CommandType.Sto redProcedure

command.Paramet ers.Add( _
New SqlParameter("@ dbname", DatabaseName))
Dim fileNumber As Integer
Dim fileName As String
For fileNumber = 1 To FileList.Length
command.Paramet ers.Add( _
New SqlParameter(St ring.Format("@f ilename{0}",
fileNumber.ToSt ring), _
FileList(fileNu mber - 1)))
Next fileNumber

command.Execute NonQuery()
connection.Clos e()

Catch ex As SqlException
Throw New ApplicationExce ption("Attach database failed: " + _
ex.ToString())
End Try

End Sub

--
Hope this helps.

Dan Guzman
SQL Server MVP

"David" <da***@orbitcom s.com> wrote in message
news:lu******** ***********@new s-server.bigpond. net.au...
Does anyone know where I can find example code for VB.NET that I can use
to create an application that attaches and detaches databases to MSDE

using
osql scipts.
(I think SQL-DMO is typically used)

Thanks in advance


Jul 20 '05 #4
David (da***@orbitcom s.com) writes:
Thanks for the reply. I notice before you attach, you connect using a
database name.
Dan is using a connection string, but he did not specify one. If
there is no "Intial Catalog" or "Database" in the connection string,
you will run the command from your default database. Since you can
access system procedures from any database, it does not matter, with
one qualification: if your default database is the one you are about
to attach, or some one other non-accessible database, then the
connection string must inlucde a database.
Is this one of the system databases (Master,TempDb. ...) ?, as at this
point we have not attached the db of interest ?


After having attached the database, you could issue a USE command to
make that your current database. Or simply re-connect with a different
connection string.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #5
> Thanks for the reply. I notice before you attach, you connect using a
database name.
Is this one of the system databases (Master,TempDb. ...) ?, as at this point we have not attached the db of interest ?
The DatabaseName argument is used only as the sp_attach_db @dbname
parameter. You can specify the desired database context via the connection
string, which I didn't include in my code example.. Here is an example of
calling the method:

Dim databaseName As String = "MyDatabase "
Dim fileList(2) As String
fileList(0) = "C:\temp\MyData base.mdf"
fileList(1) = "C:\temp\MyData base_Log.ldf"
DbUtility.Attac hDb( _
"Data Source=MyServer ;" + _
"Initial Catalog=master; " + _
"Integrated Security=SSPI", _
databaseName, _
fileList)

--
Hope this helps.

Dan Guzman
SQL Server MVP

"David" <da***@orbitcom s.com> wrote in message
news:gw******** **********@news-server.bigpond. net.au... Dan,

Thanks for the reply. I notice before you attach, you connect using a
database name.
Is this one of the system databases (Master,TempDb. ...) ?, as at this point we have not attached the db of interest ?

Thanks

Jul 20 '05 #6
Thanks for the help,

I tried using SQLDBO and although it worked on the development machine,
when I ran it on the target I got "File or assembly name
iinterop.SQLDMO .DLL, or one of its dependencies not found" (Even though I
set the SQLDMO refernce in VB.net and coppied SQLDMO.DLL and SQLDMO.RLL
files to app directory with setup solution.

I'll try the method you suggest as it is not dependant on the interop.SQLDMO
"Dan Guzman" <da*******@nosp am-earthlink.net> wrote in message
news:hk******** *********@newsr ead1.news.pas.e arthlink.net...
Thanks for the reply. I notice before you attach, you connect using a
database name.
Is this one of the system databases (Master,TempDb. ...) ?, as at this point
we have not attached the db of interest ?


The DatabaseName argument is used only as the sp_attach_db @dbname
parameter. You can specify the desired database context via the

connection string, which I didn't include in my code example.. Here is an example of
calling the method:

Dim databaseName As String = "MyDatabase "
Dim fileList(2) As String
fileList(0) = "C:\temp\MyData base.mdf"
fileList(1) = "C:\temp\MyData base_Log.ldf"
DbUtility.Attac hDb( _
"Data Source=MyServer ;" + _
"Initial Catalog=master; " + _
"Integrated Security=SSPI", _
databaseName, _
fileList)

--
Hope this helps.

Dan Guzman
SQL Server MVP

"David" <da***@orbitcom s.com> wrote in message
news:gw******** **********@news-server.bigpond. net.au...
Dan,

Thanks for the reply. I notice before you attach, you connect using a
database name.
Is this one of the system databases (Master,TempDb. ...) ?, as at this

point
we have not attached the db of interest ?

Thanks


Jul 20 '05 #7
Dan,

Can I use a similar method as shown in your code to List the available
servers and databases so that I do not require DBO at all ? This will enable
me to send the users updates of the programs that may have additional
databases added. The user can select a server if they already have a non
MSDE server installed. Also, the code can scan the server db files list to
ensure it only attaches files not already attached.

"Dan Guzman" <da*******@nosp am-earthlink.net> wrote in message
news:hk******** *********@newsr ead1.news.pas.e arthlink.net...
Thanks for the reply. I notice before you attach, you connect using a
database name.
Is this one of the system databases (Master,TempDb. ...) ?, as at this point
we have not attached the db of interest ?


The DatabaseName argument is used only as the sp_attach_db @dbname
parameter. You can specify the desired database context via the

connection string, which I didn't include in my code example.. Here is an example of
calling the method:

Dim databaseName As String = "MyDatabase "
Dim fileList(2) As String
fileList(0) = "C:\temp\MyData base.mdf"
fileList(1) = "C:\temp\MyData base_Log.ldf"
DbUtility.Attac hDb( _
"Data Source=MyServer ;" + _
"Initial Catalog=master; " + _
"Integrated Security=SSPI", _
databaseName, _
fileList)

--
Hope this helps.

Dan Guzman
SQL Server MVP

"David" <da***@orbitcom s.com> wrote in message
news:gw******** **********@news-server.bigpond. net.au...
Dan,

Thanks for the reply. I notice before you attach, you connect using a
database name.
Is this one of the system databases (Master,TempDb. ...) ?, as at this

point
we have not attached the db of interest ?

Thanks


Jul 20 '05 #8
Much of DMO functionality can be accomplished via directly via SQL. BTW,
many DMO methods execute SQL Server stored procedures internally but
undocumented procs shouldn't be called directly by production applications.

Below is SQL script that will enumerate SQL Servers. Of course, you'll need
to connect to a SQL Server to execute this so this is a catch-22 situation
when you need to list servers before connecting. Also, this is intended to
be run only by sysadmin role members.

CREATE TABLE #SqlServers
(
SqlServerName nvarchar(255)
)

INSERT #SqlServers
EXEC master..xp_cmds hell 'OSQL -L'

SELECT
CASE LTRIM(SqlServer Name)
WHEN '(local)' THEN @@SERVERNAME
ELSE LTRIM(SqlServer Name) END AS SqlServerName
FROM #SqlServers
WHERE SqlServerName <> 'NULL' AND
SqlServerName <> 'Servers:'

DROP TABLE #SqlServers
GO

You can find code examples to list SQL Servers without DMO at the links
below. These are written in C++ but it shouldn't be difficult to use the
same API calls in VB.NET.

ODBC SQLBrowseConnec t: http://www.sqldev.net/misc/ListSQLSvr.htm
LAN Manager NetServerEnum() : http://www.sqldev.net/misc/EnumSQLSvr.htm

One method to list databases is Transact-SQL:

SELECT CATALOG_NAME
FROM INFORMATION_SCH EMA.SCHEMATA
ORDER BY CATALOG_NAME
GO

To list files for the current database:

SELECT filename
FROM sysfiles
GO

--
Hope this helps.

Dan Guzman
SQL Server MVP

"David" <da***@orbitcom s.com> wrote in message
news:se******** ***********@new s-server.bigpond. net.au...
Dan,

Can I use a similar method as shown in your code to List the available
servers and databases so that I do not require DBO at all ? This will enable me to send the users updates of the programs that may have additional
databases added. The user can select a server if they already have a non
MSDE server installed. Also, the code can scan the server db files list to
ensure it only attaches files not already attached.

Jul 20 '05 #9
Dan,

Thanks heaps for your help.

Much appreciated.

Regards

David Huisman
"Dan Guzman" <da*******@nosp am-earthlink.net> wrote in message
news:aL******** *********@newsr ead1.news.pas.e arthlink.net...
Much of DMO functionality can be accomplished via directly via SQL. BTW,
many DMO methods execute SQL Server stored procedures internally but
undocumented procs shouldn't be called directly by production applications.
Below is SQL script that will enumerate SQL Servers. Of course, you'll need to connect to a SQL Server to execute this so this is a catch-22 situation
when you need to list servers before connecting. Also, this is intended to be run only by sysadmin role members.

CREATE TABLE #SqlServers
(
SqlServerName nvarchar(255)
)

INSERT #SqlServers
EXEC master..xp_cmds hell 'OSQL -L'

SELECT
CASE LTRIM(SqlServer Name)
WHEN '(local)' THEN @@SERVERNAME
ELSE LTRIM(SqlServer Name) END AS SqlServerName
FROM #SqlServers
WHERE SqlServerName <> 'NULL' AND
SqlServerName <> 'Servers:'

DROP TABLE #SqlServers
GO

You can find code examples to list SQL Servers without DMO at the links
below. These are written in C++ but it shouldn't be difficult to use the
same API calls in VB.NET.

ODBC SQLBrowseConnec t: http://www.sqldev.net/misc/ListSQLSvr.htm
LAN Manager NetServerEnum() : http://www.sqldev.net/misc/EnumSQLSvr.htm

One method to list databases is Transact-SQL:

SELECT CATALOG_NAME
FROM INFORMATION_SCH EMA.SCHEMATA
ORDER BY CATALOG_NAME
GO

To list files for the current database:

SELECT filename
FROM sysfiles
GO

--
Hope this helps.

Dan Guzman
SQL Server MVP

"David" <da***@orbitcom s.com> wrote in message
news:se******** ***********@new s-server.bigpond. net.au...
Dan,

Can I use a similar method as shown in your code to List the available
servers and databases so that I do not require DBO at all ? This will

enable
me to send the users updates of the programs that may have additional
databases added. The user can select a server if they already have a non
MSDE server installed. Also, the code can scan the server db files list to ensure it only attaches files not already attached.


Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1961
by: Eric B | last post by:
Hello gurus: Hopefully someone can shed some light on some questions I have. I am tasked to build an application that will schedule and track tasks. I firmly believe in not reinventing the wheel however also feel that customizing is certainly not out of the question. I am looking to build something in ASP (or .NET) that can allow an individual to work "disconnected" from the network while still accessing a database (MSDE?) that will...
1
1892
by: Aliza Klein | last post by:
Hi. A client of mine has a website with a standard "Contact Us" form page with name, address etc. The information is currently sent via email to the client and is then data entered into their Access (2000) system. I am sure there is a better way of doing this.
2
1820
by: David Tandberg-Johansen | last post by:
Hi! I am a new to programmeing in .Net and C#. After some time programming in C# i want to go to the next level of programming. I want to include a database with my applications, but i am a litle confused about something. Must the client have ex: Access installed, or can i accsess the file directly via ADO? If there exist any tutorials about deploying databases, I would
1
1860
by: Paul Aspinall | last post by:
Hi I have a C# Winform app which runs using SQL Server. I want to package up the app, so that it deploys with MSDE. Can anyone offer any pointers or references to help?? Thanks
3
2538
by: Paul Aspinall | last post by:
Hi I want to package my C# winforms app, to be deployed with MSDE, as easily as possible for the end user. I want to create an MSI or Installshield (prefer MSI), to setup my C# app, together with MSDE (if not already installed), and the MSDE application DB. How can I detect if MSDE is already installed?? Has anyone does this?? How??
2
1103
by: Loui Mercieca | last post by:
Hi, I am designing a Point Of Sale system, but facing a problem. By design, the database must be located locally. However, i don't want any user to just open the database and view all the records. Is there a way to prevent any user from opening the db, but still allow my program to access it. So far the solutions i thought of are: 1) Mark the db file as hidden but that would stop only the unskilled users . 2) Encrypt every record,...
0
1165
by: umberto | last post by:
Hello, I have downloades WebMatrix/NET.Framework and it works well. Unfortunatelly I have problem with connecting to MSDE. I've installed the new version (sql2ksp3a) like they say: setup.exe INSTANCENAME=my_inst_name DISABLENETWORKPROTOCOLS=0 SAPWD=my_pass SECURITYMODE=SQL The MSDE works ok (I can connect from a commercial programs - for example
6
1136
by: Jay King | last post by:
Well so far I've been directed to Access (when I asked for a free solution), I'be also been directed to firebird. I then asked how with firebird do I create the initial database, and setup the table. So that I can go back to writting my program and having it handle the add/edit/delete/search functions. No one seems to be able to tell me, or to point me to some kind of walk through, I'm not stupid, but I also don't want to have to read...
4
1556
by: Jesus Carmona | last post by:
Good day everyone; I'm having a program for a PocketPC that was working great, this program connects to a database (MSDE) but when I updated the MSDE to SP3a everything blew-up... There is no way I can connect to my database anymore. The funny part is that if I connect to a full MS-SQL Server the program works just fine. Do I need to change anything in my connection string so I can connect to MSDE SP3a? Any Ideas of how I can solve this...
0
9668
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10840
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10546
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6978
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4448
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.