Access Database Connection String - sql

I have built an application that connects to an access database on one of my servers at work. I can easily use this on my computer but when I install on another I obviously have an issue specifying the connection to the database location on the server. How do I allow the user to specify the database connection location for use on multiple work stations? Any help is greatly appreciated!

Do not hard-code the filename of the DB in your code, rather store it in a configuration file. E.g. an INI-file or an XML-file. See How to store and retrieve custom information from an application configuration file by using Visual Basic .NET or Visual Basic 2005.
You can save the setting in the App.Config File like this.
Const connectionKey As String = "connection"
Dim config As System.Configuration.Configuration
config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
config.AppSettings(connectionKey) = openFileDialog.FileName
config.Save(ConfigurationSaveMode.Modified)
And you can read it again with
Dim value As String = ConfigurationManager.AppSettings(connectionKey)
(Not tested. Got it from here: Read/Write App.Config File with .NET 2.0.)

Related

What other ways to put connection string in vb web form via asp.net not to include the Drive C or D my db is ms access 2007

Below is my connection string code in vb
the reason is that when I deploy this to web hosting there is no Drive D: or C: in website
anyone can help is really much appreciated, this is my first time to make a web program.
Thank you very much in advance.
Dim constr As String = "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=D:\Desktop\Web1\Database\SignUp.accdb"
Dim con As OleDbConnection = New OleDbConnection(constr)
Dim cmd As OleDbCommand = New OleDbCommand()
You almost certainly shouldn't be using a Access database in the first place. If your site is hosted then they would also provide database facilities, which probably means MySQL or maybe SQL Server. You ought to use one of those.
That said, if you're using Web Forms then you should be able to add an App_Data folder in the Solution Explorer and place the data file there, then specify the folder path using "|DataDirectory|", like this.
Dim constr As String = "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\SignUp.accdb"
EDIT: It just occurred to me that you're almost certainly not going to be able to use an ACCDB file regardless. If you were using an MDB file then you could use the Jet OLE DB provider, which is built into Windows. As you're using an ACCDB file, you need to use the ACE OLE DB provider, which is not part of Windows. That means that your host would have to either install Microsoft Office on their web server or else explicitly install the ACE provider, which Microsoft specify is not intended for servers. It can be installed but I very much doubt that any web host is going to do so.

How to set the connection string on another computer for access database

I created very simple winform with access database, i have 3 pcs i choose one of them to host the database (shared), and i created a .udl (data link properties) on the other two computers and i configured the path for the shared database, the test connection was successful, i opened the udl file and i have the connection string Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\PCName\c$\temp\Database1.accdb;Persist Security Info=False My question, where will i put the connection string? do i need to have the program solution just to put it?
You can define the connection string in app.config

Able to write file but not read from a network location

I have following network location
Dim myfolder As String = "\\10.0.0.90\myfolder\"
I am able to create a new file in this folder using following code:
File.Create (myfolder)
But when I try to read contents of this folder using code below I get error.
Code
Dim orderedFiles = New System.IO.DirectoryInfo(myfolder).GetFiles()
Error
The system detected a possible attempt to compromise security. Please
ensure that you can contact the server that authenticated you.
File writing is being done by ASP.Net page while reading is done from Windows Service. Could this be the issue?
Windows Service was running as "Local System". I right click on it, went into properties and changed the "Log on as" to some user account and now it can access network folder.

Should I hardcode the path for the database while deploying?

I created a software using Visual Basic 2010 and SSMS 2012 and I wish to deploy it. The question here is how should I go about doing it? I know I can create an executable file .exe which is already a good thing about VS2010 and I also think that installing SSMS-2012 as well before installing the main setup. Also, the script of the database would be generated and then run on the client's computer which enables the database to be attached. However, the question here is that should I hard-code the directory of the database files (.mdf) in Visual Basic
Currently, my connection string is such:
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
When SSMS 2012 gets installed on any computer, the server (which is based on the computer's name) which could differ in all computers. So will this work? Or are there any other options
P.S i'm a beginner, so please go easy on me :)
if your problem only lies on the computer's name on a different pc, then try to concatenate this on your myServerAddress variable. Use
My.Computer.Name
to get the target computer's name.
This is also a good link I guess, it is an Access database though but the same concept is used (the use of DataDirectory).
I'd not suggest to hardcode the connection string as this ties your application to a very specific deployment scenario. What if a customer wants to use his or her existing SQL Server instance? What if the customer creates a named instance for the SQL Server Express installation (e.g. SERVERNAME\SQLEXPRESS). Sooner or later you'd have to change your code to reflect the different situations and you'd do the same thing that you can do already now.
In .NET, you can store a connection string in an application configuration file. It is named as the Exe file but has a .config extension (e.g. MyExeName.exe.config). In Visual Studio, all you need to do is to add a file to your project ("Application Configuration File", app.config) if it doesn't exist yet. Upon build, it will be renamed to the Exe-name plus .config. See this link for detailed information.
For your scenario, you'd have a configuration file like this. It references the local computer by using "." as the server name. So you don't have to change it after deployment if this is your default scenario:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyConnection"
connectionString="Data Source=.;Initial Catalog=myDatabase;Integrated Security=SSPI"/>
</connectionStrings>
</configuration>
In your code, you can access the connection string using the ConfigurationManager class (you might need to add a reference to the System.Configuration assembly). You can use the acquired connection string when you create the connection:
Dim connStr = ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString
Using conn = new SqlConnection(connStr)
conn.Open()
' Use the connection
End Using
To summarize: you'd add a default configuration file with a connection string that works for any server name to your deployment. If the customer wants to have the database on a different instance or another server, you simply change the connection string - but you don't have to if the customer is fine with the default configuration. As you see, it is not much more effort than to hard code it, but it will make your deployments more flexible.
I work for a large company delivering enterprise solutions and the way we handle this is very easy. All you need is a small configuration utility (could even be your installer wizard) where you request a user credentials (which presumably have enough access right to the SQL Server to create/alter databases and objects), and for the server name.
All you need to do after that is to establish a connection to the server, create the database and run the table and/or views creation scripts.
I see you mentioned attaching an MDF file but I cannot see a reason why you would like to do that. If you have system data you still can insert it using the same script or an additional one.

What does this connection string mean?

data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true
Specifically what does
AttachDBFilename=|DataDirectory|\aspnetdb.md
mean?
It means that the connection will open aspnetdb.mdb on published app data dir.
This avoid you to read dir inside your web app once published.
Visual Studio does it automatically for you at runtime.
|DataDirectory| (enclosed in pipe symbols) is a substitution string that indicates the path to the database. It eliminates the need to hard-code the full path which leads to several problems as the full path to the database could be serialized in different places. DataDirectory also makes it easy to share a project and also to deploy an application.
For example, instead of having the following connection string:
"Data Source= c:\program files\MyApp\Mydb.sdf"
Using DataDirectory, you can have the following connection string:
“Data Source = |DataDirectory|\Mydb.sdf”
To set the DataDirectory property, call the AppDomain.SetData method. If you do not set the DataDirectory property, the following default rules will be applied to access the database folder:
• For applications that are put in a folder on the user's computer, the database folder uses the application folder.
• For applications that are running under ClickOnce, the database folder uses the specific data folder that is created.
*i forgot to add the link so here ya go ->
http://social.msdn.microsoft.com/Forums/en-US/sqlce/thread/dc31ea59-5718-49b6-9f1f-7039da425296/
*