Change Database-Name/Server at runtime - sql

I am using Visual Studios build-in DataSource functions, to work with my application and its database. Now I am facing one little problem; How do I change the database-server in the final project?
Obviously the end-users server name will not be the same as mine.
Also how can I change it at runtime? My application has functions to find the database-server itself, so it needs to be able to change the database server at runtime (Only at applcations start) .
Update 1:
Right now I am Changing my TableAdapter.Connection.ConnectionString with .Replace("My Local Server Name", "New Server Name") to change the Server. But I don't think that's the way it's supposed to be done.

If you want to change the connection string after deployment then you edit the config file by hand or you can do so in code if the current user is an administrator.
If you want to change the connection string for a table adapter at run time to something other than what's in the config file then you do indeed need to set the Connection.ConnectionString property. The most advisable way to do that is to use a connection string builder. For an Access database, that might look like this:
Dim builder As New OleDbConnectionStringBuilder(myTableAdapter.Connection.ConnectionString)
builder.DataSource = dataSourceName
myTableAdapter.Connection.ConnectionString = builder.ConnectionString

Related

Invalid value for attachdbfilename

I'm currently trying to distribute a application that relies on some form of database in .NET. I am trying to use SQL Server Express or SQL Server Express LocalDB.
For accessing the database I am using the entity framework.
Now the target is to create a deployable version that does not require any alterations on the SQL server by hand. It needs to install and setup as far as needed to launch the application. All goes well on my development computer. How ever once deploying to another computer the problems begin.
I tried the LocalDB in order to avoid the requirement to setup the database structure by hand on the target machine. How ever upon launching the application its reports a problem with the connection string. The attachdbfilename is not valid.
This filename is set to attachdbfilename=|DataDirectory|\database\Db.mdf
As I said. On my development maschine this works. Both when using the deployed files and when running it from the IDE. Yet the target mashine reports the problem. I installed the SQL Server Express 2012 LocalDB using ClickOnce along with my application. The deployed files contain the mdf file in the proper directory.
The full connection strings are:
<connectionStrings>
<add name="DbContainer" connectionString="metadata=res://*/database.DbModel.csdl|res://*/database.DbModel.ssdl|res://*/database.DbModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\database\Db.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
I fail to see a reason why the attachdbfilename is not valid. The installation of the sql server on the target machine seems to be okay.
I also tried to pulish it with the normal SQL Server 2012 express. How ever I assume if I want to use this, without a attached database file I need to create the required database by hand. And that is not a option.
I read in some questions that this indicates a problem with the create ObjectContext but this one is looking good as well:
Public Partial Class DbContainer
Inherits ObjectContext
Public Sub New()
MyBase.New("name=DbContainer", "DbContainer")
MyBase.ContextOptions.LazyLoadingEnabled = true
OnContextCreated()
End Sub
Public Sub New(ByVal connectionString As String)
MyBase.New(connectionString, "DbContainer")
MyBase.ContextOptions.LazyLoadingEnabled = true
OnContextCreated()
End Sub
Public Sub New(ByVal connection As EntityConnection)
MyBase.New(connection, "DbContainer")
MyBase.ContextOptions.LazyLoadingEnabled = true
OnContextCreated()
End Sub
...
Just like it got generated by the entity framework.
I am running out of ideas on how to solve this problem.
Okay the problem turned out to be the |DataDirectory|
I replace this entry in the connection string by hand using the proper one click data directory I optained using:
Dim dataDir As String
If ApplicationDeployment.IsNetworkDeployed Then
Dim ad = ApplicationDeployment.CurrentDeployment
dataDir = ad.DataDirectory
Else
dataDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
End If
No problem loading the database file anymore. I can't say why the replacement of the |DataDirectory| is not working. For the version on the development computer this is simply replace with a empty string... and this works because the working directory is the parent of the refered database directory.
How ever when using ClickOnce the data files, including the database, is located in a different directory. This causes the connect string to fail. The code above fixes this problem. I can't explain for what reason this is not working out of the box.

Reporting Services multiple report folders, same data source name, but want different DB

TThanks, for reading, I'll try to explain my issue in a detailed format as the question I'm asking is a bit high-level for my experience-level.
I'm using VS2005 and SQL Server 2005 with Reporting Services. All of my reports are built in VS2005. The reports are deployed to folders named "Amort" or :Amort_Test" on the Report server depending on the configuration I choose when I deply (Production delpoys to "Amort", Test deploys to "Amort_Test").
In Reporting Services Report manager, I have a data source setup call AMORT (and that is the datasource in my VS2005 reports). The datasource is of type Microsfot SQL Server and the connection string is "Data Source=uslibsql310;Initial Catalog=AMORT_P".
What I'd like to do is have the ability for the reports in the "Amort" folder point to a database called AMORT_P on my server (uslibsql310) while the reports in the folder "Amort_Test" point to the database called AMORT_T on the same server (uslibsql310). Obviously my current configuration, where reports in both folders point to the same datasource, says that reports point to the AMORT datasource which currently points to AMORT_P.
My initial thought was that I could create a new datasources, call it AMORT_Test and have its connection string be ""Data Source=uslibsql310;Initial Catalog=AMORT_P". However, every time I'd deploy my reports, I'd have to change the datasource in VS2005 to read AMORT_Test instead of AMort and then deply, which would be abit of a hassle.
Can anyone think of a more user-friendly solution to this? I'm one who normally finds the quickest solution and goes with it, but in this case I think there must be a way to set this up so that the reports in one folder know to pick one DB and the reports in another folder know to pick a different DB, but my current setup doesn't allow that. I'm not sure where to start in trying to figure this out as I'm a bit of an RS novice.
You're almost there, I think. If I understood correctly, here's your current setup:
One shared datasource
Reports all use that shared datasource for datasets
Two configurations: test and production, each with its own target folder
What you can do now is set OverwriteDataSources to False. Manual labor is required to set the connection string for deployed reports only:
For initial deployment of reports
When you want/choose to change the connection for deployed reports
This manual labor can be either:
Changing the connection string, temporarily enable OverwriteDataSources, and re-deploy
Going to the report manager web frontend to change the connection string
However, your default setup would be to deploy reports to both configurations, without having to worry about connecting test reports to production databases and vice versa.

PowerBuilder 12.5 tutorials

I am new to PowerBuilder Classic 12.5. I have created ado.net database profile. How can I now connect to it and create a login window to read username and password, validate and let the user access to the program.
Please help with a sample code and where do I put the codes?
Thanks.
Making the database connection was one of the things that took me the longest to learn, not because it's difficult, but because you do it once and don't have to look at it again.
In your code, you'll need to set the properties of the SQLCA global transaction object. If you have the connection defined in your database painter, then you can right-click on it, choose "Properties", and see what the syntax needs to be from the "Preview" tab.
This syntax will (probably) include your security credentials for the connection. You'll need to build your own userid/password prompt and parse the results into the connection string. I don't have ADO.NET installed, but here's a sample of an ODBC connection... straight copy and paste:
// Profile CINEMA3
SQLCA.DBMS = "ODBC"
SQLCA.AutoCommit = False
SQLCA.DBParm = "ConnectString='DSN=cinema;UID=UserName;PWD=SuperSecurePassword'"

Access a accdb file (MS Access 2007) from resources

probably this is a simple question but because i have never used resources like that i can not think how should i do it.
I am writing a very simple program that connects to a accdb file (Microsoft Access 2007 file) and returns some results in a datagridview. Everything is fine. Now because we have to deploy this program in many computers i publish it so everyone we want can install it and have updates. What i wanted to do is to make the database file part of the program which i did it by adding it to the resources. My problem is that i do not know what connection string to enter in order to access it in my resources.
My previous connectionstring before i deploy it was this
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\bl.accdb;Persist Security Info=True
What should i change in the data source in order to access the same file in my resources? Or am i wrong and this is not possible?
Thanks
You can do it. All you need to do is point the datasource to the location of the resources direcory, which by default is adjacent to the application itself. You could determine this at runtime by using:
Application.StartupPath & "\Resources\DatabaseName.accdb"

How can I fix LINQ dependency issues with a VB.NET deployment?

I have a program that contains .dbml files. I created this file with Server Explorer.
I want to execute this program on other computers but I seem to be having issues with dependencies because I use LINQ.
How can I fix this?
Can you be more specific? What errors are you encountering? Did you use SQL Passwords or Network authentication? Do your users have network accounts added to SQL? Is this a permission based error?
Just one other possibility: Your users should have .NET 3.51 installed on their systems or they won't get the best possible Linq support. They might not have the Linq assemblies at all.
Yeah, we need more info, but I'm guessing you're having problems with the ConnectionString? as in, there's a connection string in your DBML designer file, and it doesn't match your production DB connection string?
If this is the case, then perhaps write a class that handles all instantiations of your DataContext in your code.
Then, within that class, whenever a new datacontext is created, you override the DBML connectionstring with your current connectionstring, probably from your web.config. this ensures your LINQ stuff is always connected to the correct DB during runtime.
Perhaps something like this (the property names might differ):
Public Shared Function GetNewContext() As YourDataContext
Dim dContext As YourDataContext
dContext = New YourDataContext()
dContext.Connection.ConnectionString = MyConnectionStringFromTheWebConfig()
Return dContext
End Function