Connection String over network for oledb - connection-string

What can be the problem with following connectionStrings
string constr = #"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=\\otherPCName\SmoeSharedFolder\test.mdb;";
Or
string constr = #"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=\\192.168.14\SmoeSharedFolder\test.mdb;";
No Problem with following connection String, when i access database on that PC otherCP
string constr = #"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=D:\SmoeSharedFolder\test.mdb;";
Also I can open a text file from other PC using this path. This tells the path should be valid
\\otherPCName\SmoeSharedFolder\hi.txt
I have seen connection strings for network so I think they should work but how?

string constr = #"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=\\otherPCName\SmoeSharedFolder\test.mdb;";
string constr = #"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=\\192.168.1.14\SmoeSharedFolder\test.mdb;";
Both strings are valid to access database from a network PC. If your database file exists in a folder to which you have permissions => Exists in a Shared folder. You will be able to write (update/insert) only if you have write permissions as well.
I was getting error specified file cannot be opened. Because I had problem both in using otherPCName and IP 192.168.14. (For otherpcname I was using sampc but it was sam-pc).

Related

Lua: Connect with MSSQL DB

I'm learning about Lua (version 5.3),Is there a way to connect to an mssql database?
I try to do it as follows
print("Connection to the db:")
local dbuser = '....'
local dbpass = '...'
local dbsource = '....'
local dbname = 'xx.xx.xxx.xx'
database.open("Provider= ODBC ; Initial Catalog = dbname; Data Source = dbsource ; User ID = dbuser ; Password = dbpass ")
print("Database opened succesfully.")
print("Connection to the db:")
But didnĀ“t work, execution returns me:
attempt to index a nil value (global 'database')
Thanks,
I try to do it as follows
database.open("Provider= ODBC ; Initial Catalog = dbname; Data Source = dbsource ; User ID = dbuser ; Password = dbpass ")
What made you think you can do that? You get that error because there is no global variable database unless you define one.
Hence you may not index it. Trial and error with random code is not a very good way to get things done in programming.
Also you cannot handle strings like that in Lua. You either need to use string.format or the concatenation operator .. to get your variables into that string.
There's also no printf in Lua unless you define it.
I suggest you learn the basics of Lua befor you get into interfacing databases.
From reading the Lua reference manual it should become obvious that Lua does not know anything about databases.
Either you run Lua embedded into some host application that provides database access through its Lua API or you need to load a library that supports that.
Searching the web for "lua sql" instantly yields
https://keplerproject.github.io/luasql/
and others.

Create a config file containing connection information then reference that connection

I have an Excel sheet which contains VBA code assigned to a button.
Here is part of my VBA code:
Dim ConnectionStr As String
Dim sql As String
ConnectionStr = "Provider=SQLOLEDB.1;Password=ABCDEFG;User ID=xyz;Data Source=MyServer;Initial Catalog=NEW_DB"
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array("OLEDB;" & ConnectionStr), _
Destination:=Worksheets("Hidden").Range("$A$2")).QueryTable
.CommandType = xlCmdSql
.CommandText = "EXECUTE [dbo].[usp_one]"
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.ListObject.DisplayName = "Table_in_Hidden"
.Refresh BackgroundQuery:=False
End With
I don't want to show the connection string in a VBA macro, because someone may "peep" into it and discover our SQL Server password.
It seems one solution would be to create a config file containing the connection information and then to reference the config file in the VBA macro.
Anyway, VBA must finally get the password for the connection. No matter it's in a connection string or a config file.
And the connection is established by Excel via VBA, so the password (or a connection string, a config file, etc.) must be distributed with the excel sheet file.
Therefore, if someone has gotten the excel sheet and can use it normally, I don't think there be an effective way to prevent knowing the connection password.
If you wan't the excel sheet's users to make some changes on the database, you'd better not assign them the corresponding database privilege for the database user that the VBA code uses (if the database system supports user privilege limitation).

Detect what computer/server I'm using in VB

I am writing an application that uses a database, through SQL Server. I'm also using two computers to do it. I want to put in an if statement to execute code for the appropriate computer since my database connection string will be unique to each machine. Each computer will use an identical, but not synchronized database. I just don't want to jump back and forth to change connection strings all the time.
If Computer01 then
use connection-string01
else
use connection-string02
end if
This code will return the name of the computer in VB.NET
Dim ComputerName As String
ComputerName = System.Net.Dns.GetHostName
If ComputerName = Computer01 Then
'Connection String 1
Else
'Connection String 2
End If

How to dynamically change server url withing http connection manager SSIS?

I am fairly new to SSIS, Visual studio. Thought that might be good to mention in the beginning.
What I wanted to achieve was to download a certain xls file from http://www.ads-slo.org/statistika/ website and store it in a certain folder on my computer. I have achieved that, but the problem is that I know how to do it one file at a time. I did it by opening new connection, going to http connection and in the manager typing the server url: which in my case if lets say we start with January 2016 was this:http://www.ads-slo.org/media/xls/2016/Januar-2016.xls. After doing so I've constructed a script task or more or less copied it from a website that downloads the file given a certain url based on the connection manager.
My problem is that I would like to download all of the files on this site, so starting with January 2007 and ending with January 2016 with a single package and by not changing my connection manager server url settings 100 times.
Is there any way you might help me. I would be forever grateful.
Thank you in advance.
Kind regards, Domen
Here is one very simple example (it can be improved - see comments after code block) of changing a connection string dynamically by using a Script Task. You can also dynamically change connection strings using expressions and the Connection Manager's expressions property. However, since you are using a Script Task to handle the downloads, I have demonstrated it using one.
As you haven't tagged the Script language (VB or C#) you are using, I have written a rough draft in VB.
I have added comments, but stackoverflow syntax highlighting interprets it strangely; apologies.
Public Sub Main()
' Get the HTTP Connection
Dim connObj As Object = Dts.Connections("HTTP Connection Manager").AcquireConnection(Nothing)
Dim connection As New HttpClientConnection(connObj)
' Static list of month names for dynamic connection string (obviously add as many as needed)
Dim monthNames As String() = New String() {"Januar", "February", "March"}
' Nested loop - for each year and month, try to download the Excel file
For Y As Integer = 2007 To 2016 Step 1
For M As Integer = 0 To monthNames.Length - 1 Step 1
' Set the assumed name of the remote file
Dim remoteFileName As String = monthNames(M) + "-" + Y.ToString() + ".xls"
' Change the connection string a.k.a dynamic connection string
connection.ServerURL = "http://www.ads-slo.org/media/xls/" + Y.ToString() + "/" + remoteFileName
' Set where to download the file to
Dim localFileName As String = "C:\Temp\" + remoteFileName
Try
connection.DownloadFile(localFileName, True)
Dim buffer As Byte() = connection.DownloadData()
Dim data As String = Encoding.ASCII.GetString(buffer)
Catch E As Exception
' File may not exist on remote server, delete the blank copy it attempted to create
File.Delete(localFileName)
End Try
Next
Next
Dts.TaskResult = DTSExecResult.Success
End Sub
How can this be improved?
One potential improvement is to parse the remote server for the folders and directory contents (to save having static lists of month names, hardcoded start and end years and building file names) using a HttpWebRequest.
However, there might be an issue with the remote server permissions in allowing such requests to be made so you would have to investigate further and speak with the server administrator.
Testing the above code, it successfully downloaded the Januar-2015 and Januar-2016 Excel files from the website.

Excel data connection to SQL dB error

I crafted a macro in an Excel workbook to extract a subset of data from a SQL database based on user input.
The macro prompts the user for a parameter input and inserts that parameter into a ready-made stored procedure configured into a an Excel data connection - see below for my vba:
Sub RefreshDBQuery()
Dim Val As Integer
Application.ScreenUpdating = False
Worksheets("Adjustable CF").Select
Val = InputBox("Enter valid 4 digit number", , 1907)
Sheets("TestData").Visible = True
Worksheets("TestData").Select
Worksheets("TestData").Range("A1").Select
ActiveCell.Value = Val
With ActiveWorkbook.Connections("MacroExtraction 2Server").OLEDBConnection
.CommandText = "EXEC dbo.prV_FlowExtract '" & Range("A1").Value & "'"
End With
ActiveWorkbook.Connections("MacroExtraction 2Server").Refresh
Sheets("TestData").Visible = False
End Sub
When I run it - it works fine and additionally, since it's modifying an existing data connection ( the one I previously configured), I notice a odc file in a folder called "My Data Sources" under My Documents:
However, when I send this workbook over to a colleague to run the macro and to extract data - the macro is able to run up to a point, and she receives an error:
I ask her to open up the folder "My Data Sources" and I don't see an odc file:
My question is: what am I missing? Or rather what is my colleague missing in order to get her macro to work on her local machine?
I checked with the dB administrator who said that she has the permissions necessary to access the server, so that's why I am picking on the lack of the odc as a cause for my concern. Should I copy my odc file and send it to her to copy into her Data Sources folder? Should I rewrite the macro and re-setup the data connection on her local machine? Anyone with experience to comment would be much appreciated! Thanks!
The macro alone does not contain all the necessary info (server name, for example?). Try running "NewSQLServerConnection.odc" in you colleague's my data sources location, complete the necessary data, make sure the connection name is the same as in your macro, and then the macro should work.
Hope this helps!

Resources