VBScript Error Trying to disable dynamic port in SQL Server - sql-server

I've been trying to automate disabling dynamic ports in a new installation of SQL Server 2012 and setting a static TCP port. I can do this without an issue from the SQL Configuration Manager, but getting a script to do this is giving me more trouble. Luckily, I found someone that was looking to do the exact same thing here: MSDN Forums.
The code I'm using is the following:
Private Function setProperty ( ByVal path, ByVal value )
Set obj = GetObject(path)
errornumber = obj.SetStringValue(value)
If Not errornumber = 0 Then
WScript.Quit(errornumber)
End If
End Function
Set args = WScript.Arguments
If Not args.Count = 1 Then
WScript.Echo "ERROR: Invalid arguments"
WScript.Echo "Usage: cscript " & WScript.ScriptName & " "
WScript.Quit(255)
End If
' set TCP/IP port of SQLServer instance 'SQLSERVER_MATRIX'
setProperty "WINMGMTS:\\.\root\Microsoft\SqlServer\ComputerManagement:ServerNetworkProtocolProperty.InstanceName='SQLSERVER_MATRIX',IPAddressName='IPAll',PropertyName='TcpPort',PropertyType=1,ProtocolName='Tcp'", args(0)
' switch off dynamic ports
setProperty "WINMGMTS:\\.\root\Microsoft\SqlServer\ComputerManagement:ServerNetworkProtocolProperty.InstanceName='SQLSERVER_MATRIX',IPAddressName='IPAll',PropertyName='TcpDynamicPorts',PropertyType=1,ProtocolName='Tcp'", ""
I try running it as suggested on the site with
cscript.exe //nologo set_port_property.vbs
and get the error:
set_port_property.vbs(1,1) (null): 0x8004100E
So that's giving me a namespace error and this is where I'm stuck. It doesn't look like anyone else is having issues with this file as I've found it in multiple places, but I'm trying this on a Windows 10 computer with SQL Server 2012 and everything I found was using 2008 and at most Windows 8. There's a Scriptomatic 2.0 tool that may help, but the link on Microsoft's page is broken so I don't know where to go from here.

The error code as you point out is
WBEM_E_INVALID_NAMESPACE (0x8004100E)
The specified namespace did not exist on the server.
Which is pretty self explanatory, basically the namespace being passed is not recognised for whatever reason, usually it's just incorrectly typed but as you have already mentioned others are using this script without issue.
Couple of suggestions
This likely points to the machine, the first thing I would try is running the script on another machine to see if it can be isolated to this machine alone.
You may also want to test the health of the WMI installation using the in-built tools provided in Windows. The wbemtest.exe tool is a great little tool for testing connection to and query WMI respositories.
Stumbled on the Answer
In the process of answering this question think I may have stumbled on the answer.
Tried suggestion 2. myself to test connecting to
root\microsoft\sqlserver\computermanagement
but failed with the same error using wbemtest.exe but found I could connect to
root\microsoft\sqlserver
After a quick google found the MSDN documentation that describes"How to: Access WMI Provider for Configuration Management using WQL" pointed me in the right direction.
You see the namespaces are different for later versions of SQL Server.
SQL Server 2005
root\Microsoft\SqlServer\ComputerManagement
SQL Server 2008 R2
root\Microsoft\SqlServer\ComputerManagement10
SQL Server 2012, SQL Server 2014, SQL Server 2016#
root\Microsoft\SqlServer\ComputerManagement11
# - Possibly subject to change
After connecting to
root\Microsoft\SqlServer\ComputerManagement11
using wbemtest.exe I no longer received the error and was able to browse classes and instances.
With that in mind changing your namespace in the code should fix the issue.
' set TCP/IP port of SQLServer instance 'SQLSERVER_MATRIX'
setProperty "WINMGMTS:\\.\root\Microsoft\SqlServer\ComputerManagement11:ServerNetworkProtocolProperty.InstanceName='SQLSERVER_MATRIX',IPAddressName='IPAll',PropertyName='TcpPort',PropertyType=1,ProtocolName='Tcp'", args(0)
' switch off dynamic ports
setProperty "WINMGMTS:\\.\root\Microsoft\SqlServer\ComputerManagement11:ServerNetworkProtocolProperty.InstanceName='SQLSERVER_MATRIX',IPAddressName='IPAll',PropertyName='TcpDynamicPorts',PropertyType=1,ProtocolName='Tcp'", ""
In fact at the very bottom of that thread on MSDN someone even hints at this but for SQL Server 2008
Goozak posted in MSDN Forums - silent install with fixed tcp port
Date: Wednesday, March 17, 2010 3:13 PM
"know this is an old thread, but since this is the post I found that helped me solve my problem, I just want to add that for SQL Server 2008 Express, you need to use ComputerManagement10 :ServerNetworkProtocolProperty..."

Related

Database and Dataset data is empty [duplicate]

Apparently, using AttachDbFilename and user instance in your connection string is a bad way to connect to a DB. I'm using SQL server express on my local machine and it all seems to work fine. But what's the proper way to connect to SQL server then?
Thanks for your explanation.
Using User Instance means that SQL Server is creating a special copy of that database file for use by your program. If you have two different programs using that same connection string, they get two entirely different copies of the database. This leads to a lot of confusion, as people will test updating data with their program, then connect to a different copy of their database in Management Studio, and complain that their update isn't working. This sends them through a flawed series of wild goose chase steps trying to troubleshoot the wrong problem.
This article goes into more depth about how to use this feature, but heed the very first note: the User Instance feature has been deprecated. In SQL Server 2012, the preferred alternatives are (in this order, IMHO):
Create or attach your database to a real instance of SQL Server. Your connection string will then just need to specify the instance name, the database name, and credentials. There will be no mixup as Management Studio, Visual Studio and your program(s) will all be connecting to a single copy of the database.
Use a container for local development. Here's a great starter video by Anna Hoffman and Anthony Nocentino, and I have some other resources here, here, and here. If you're on an M1 Mac, you won't be able to use a full-blown SQL Server instance, but you can use Azure SQL Edge if you can get by with most SQL Server functionality (the omissions are enumerated here).
Use SqlLocalDb for local development. I believe I pointed you to this article yesterday: "Getting Started with SQL Server 2012 Express LocalDB."
Use SQL Server Compact. I like this option the least because the functionality and syntax is not the same - so it's not necessarily going to provide you with all the functionality you're ultimately going to want to deploy. Compact Edition is also deprecated, so there's that.
Of course if you are using a version < SQL Server 2012, SqlLocalDb is not an option - so you should be creating a real database and using that consistently. I only mention the Compact option for completeness - I think that can be almost as bad an idea as using AttachDbFileName.
EDIT: I've blogged about this here:
Bad Habits : Using AttachDBFileName
In case someone had the problem.
When attaching the database with a connection string containing AttachDBFile
with SQLEXPRESS, I noticed this connection was exclusive to the ASP.NET application that was using the database. The connection did block the access to all other processes on the file level when made with System.Data.SqlClient as provider.
In order to assure the connection to be shareable with other processes
instead use DataBase to specify the database name in your connection string
Example or connection string :
Data Source=.\SQLEXPRESS;DataBase=PlaCliGen;User ID=XXX;password=ZZZ; Connect Timeout=30
,where PlaCliGen is the name (or logical name) by which SQLEXPRESS server knows the database.
By connecting to the data base with AttachDBFile giving the path to the .mdf file
(namely : replacing DataBase = PlacliGen by AttachDBFile = c:\vs\placligen\app_data\placligen.mdf) the File was connected exclusively and no other process could connect to the database.

CommandTimeout for SQL Connection string

How do I configure CommandTimeout (not Connect Timeout) from connection string in SQL Server?
I thought it'd be simple to find but apparently not. Below I added CommandTimeout=60. It doesn't seem to break anything, but I have no idea if it's actually working or not (and I can't find doc on this)
Data Source=someplace.com;Initial Catalog=MyDB;CommandTimeout=60;User Id=someID;Password=secret;
It's probably worth pointing out that setting the default command timeout via the connection string is possible in Microsoft.Data.SqlClient since version ~2.1.0. This package is the open source replacement for System.Data.SqlClient and is available from nuget
If you add e.g. ;Command Timeout=300 to your connection string and receive an error Keyword not supported: Command Timeout' you should check that you truly are using Microsoft.Data.SqlClient, and that its version is greater than 2.1; the question was posed before it was possible, but times have changed
Confirm, using the versions of:
Microsoft ActiveX Data Objects 6.1 Library
Microsoft ActiveX Data Objects Recordset 6.0 Library
The CommandTimeout - when set for the Connection object - works also for the ADO Queries following Connection Open.

ADODB Command Parameters Refresh doesn't retrieve parameters

I have an old web application, built with VBScript on an IIS6 Server with a SQL Server 2008 database. It is in the processed of being moved to a new server, on IIS8.
Every queries in the app work with stored procedures, with which we never had a problem. But on the new server, it doesn't seem to work. I found it it's because the Command.Parameters.Refresh doesn't return the parameters properly.
Consider this code:
Set cmd = Server.CreateObject("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandType = 4
cmd.CommandText = v_strSpName
cmd.CommandTimeout = 0
cmd.Parameters.Refresh
For i = LBound(v_arrParameters) To UBound(v_arrParameters)
If m_bReplaceEmptyToNull Then
v_arrParameters(i)(1) = ReplaceEmpty(v_arrParameters(i)(1))
End If
cmd.Parameters(v_arrParameters(i)(0)).value = v_arrParameters(i)(1)
Next
Everything in v_arrParameters exists, but I tried iterating in Parameters.name after the refresh, the parameters are not returned (but they are on the production server).
Also worth noting, the SQL Profiler does receive the query and return the parameters:
exec [Database]..sp_procedure_params_rowset N'get_company',1,N'dbo',NULL
According to this page, it is a known issue, I just want to make sure it doesn't come from this problem, and find a solution or an alternative that doesn't imply a full rewriting of the application.
Also, no I can not update the SQL Server version, switch to VB.NET, there is always the issue that there is a client that won't pay for this problem.
So we managed to make it work this way:
Opening the Applications Pools section in IIS Manager.
Going in the Advanced Settings of the Default App Pool (its name here, maybe not for everyone, I don't know)
Setting Enable 32-bits apps to True
This is on a 64-bit Windows Server 2012 R2, with Microsoft SQL Server 2008 (SP4) - 10.0.6241.0 (X64) .

Why only some users get the error: "Connection is busy with results for another command"

I have a Delphi Application that is connected to a SQL Server db using SDAC component from DevArt, we have 200 installations of the software and only to a customer, with some users, I notice the following error:
"Connection is busy with results for another command" = "La connessione รจ occupata dai risultati di un altro comando".
SQL vers.: SQL Server 2008 R2 Express with filestream full enabled
My application create both db users and SQL account logins:
creating a new user, then there aren't problems
changing user code in my application, it means that another db user and SQL account login is created, I have the error
this problem happens only with some users, not all ones
What I've already tried without luck:
deleted and re-installed database
uninstalled and re-installed SQL Server Instance
checked users/account properties in SQL Server (all ok)
If you need specific infos please tell me
------------NEW INFORMATIONS------------
I checked better all the Instance properties from Studio Management and I've noticed that CPU's are not checked (see image below).
Instead in all the other normal installations of SQL Server, I see filled checkboxes.
Could it be the problem?
I hope this help you to help me...
The "Connection is busy with results for another command" error means that there are at least two queries that use the same connection. This problem can occur if you are using one connection in several threads. To solve the problem in this case, you should have connection (the TMSConnection component) in each thread.
Also, this problem can occur if you set the TCustomMSDataSet.FetchAll property to False. When FetchAll=False, execution of such queries blocks the current session. In order to avoid blocking OLEDB creates additional session that can cause the "Connection is busy with results for another command" error. To solve the problem in this case, you should set the TMSConnection.Options.MultipleActiveResultSets property to True. The MultipleActiveResultSets property enables support for the SQL Server Multiple Active Result Sets (MARS) technology. It allows applications to have more than one pending request per connection, and, in particular, to have more than one active default result set per connection. Please note that the MultipleActiveResultSets property works only when SQL Native Client is used. Therefore, you should also set the TMSConnection.Options.Provider property to prNativeClient.
Just wanted to correct dataol's answer and say that MARS_Connection should be set to "Yes" instead of "True" to enable Multiple Active Result Sets. At least on SQL Server 2012 if you are using a DSN file:
[ODBC]
DRIVER=SQL Server Native Client 11.0
DATABASE=MYDBNAME
WSID=
Trusted_Connection=Yes
SERVER=
MARS_Connection=Yes
To provide Multiple Active Result Set (MARS) support to a SQL connection using the MSSQL driver, you must add a key called Mars_Connection and set its value to True.
#ienax_ridens, I recently encountered the same problem using the same tools (Delphi and Devart-SDAC).
In my case one specific query giving two results sets.
My TMSQuery was
If Condition= 1
begin
Select * from #TempTable1
end else
begin
-- Some more stuff
Insert INTO #TempTable2
--
--
End
Select * from TempTable1 -- here is the problem
so in case of Condition = 1 it was giving two results sets and causing "Connection is busy with results for another command"
I hope this helps you.
Edit: I realized you post is quite old, please share what you did to resolve this error
I had the same problem and solved installing the microsoft odbc driver 11 (msodbcsql) (https://www.microsoft.com/pt-br/download/confirmation.aspx?id=36434).
Check your compatibility mode i just ran into this when we moved from 2008 to 2016 db we had to set it to 2012 compatibility mode.
I had this problem when I found that my runtime DLL was in the environment path and the program folder. It was a runtime issue and nothing with the program.

Smo does not show 2008 SQL Server instances when 2005 instances exist

I am trying to enumerate all SQL Server instances installed on a local machine. I am using SmoApplication.EnumAvailableSqlServers(true). However, only SQL Server Express 2005 instances are shown. Default 2008 instance is not shown at all!
I tried 2 other solutions with SqlServerRegistrations.EnumRegisteredServers() and SqlDataSourceEnumerator.Instance.GetDataSources() but they do not work either.
There is another question regarding this (Can't enumerate SQL Server 2008 Registered Servers with SMO) but it unfortunately has no answer.
Found the solution to your problem here
Solution: Explicitly set the ProviderArchitecture property to the architecture of the target SQL Server.
If you do not explicitly set the ProviderArchitecture property, it will assume that of the running process. If the host process of your application does not match the architecture of the installed version of SQL Server on the target server, the ServerInstances collection will be empty. This is due to the separate x86 and x64 WMI providers and how SQL Server registers instances.
Try
Dim objManagedComputer As New ManagedComputer("target_servername")
objManagedComputer.ConnectionSettings.ProviderArchitecture = ProviderArchitecture.Use64bit
Dim objServerInstance As ServerInstance
For Each objServerInstance In objManagedComputer.ServerInstances
MsgBox(objServerInstance.Name)
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
Here's another alternative you could try using the ManagedComputer Class (Namespace: Microsoft.SqlServer.Management.Smo.Wmi).
ManagedComputer mc = new ManagedComputer();
foreach (ServerInstance si in mc.ServerInstances)
{
Console.WriteLine(si.Name);
}

Resources