Re-linking Tables in Access Database - sql-server

First of all - my VB skills are almost non-existent so any suggestion/ideas should be a little more elaborate if possible. Here is the problem I am facing -
We have linked database tables and queries in Ms Access 2010. Now a copy of the database has been created on a new SQL server. So I want to re-link the tables so that access points to the tables on the new SQL server. Right now, all the tables are corresponding to the database on the old server. Here is what I have tried -
Open XYD_Outreach_Dev.accdb on the network (this is the dev version of access that we use)
Form a new ODBC connection which creates a new_sql_server.dsn file corresponding to the new SQL server (2012)
Go to External Data -> ODBC Database -> Link to the data source by creating......
Link to a table say - tblAbriContacts
This creates a new linked table dbotblAbriContacts
I rename it to tblAbriContacts at which point Access asks me -> table already exists. Do you want to replace it? I click yes and the table gets replaced
I hit the save button, close the application and restart it. The tblAbriContacts is pointing to the old database again
What could be the issue? Please note that I have to do this for all the database, lookup and junction tables.
Do I have to change some connection string which the access points to as soon as it is launched?
If I try the Linked table manager, nothing happens, it says "All selected tables were successfully refreshed". But nothing happens. I guess that's because the tables haven't been moved from the old database. A new copy of everything has been created on the new server.
Please help me out with this. My work is stuck because of this reason.

Can you please check if there is a module or other vba file created and file's on_load event has some code which to assign the connection string for all objects (tables and SPs) and refreshes it every time you restart the application.
If you see the connection string there, you might need to replace it with the new one.

Related

How to create database using Database Project in Visual Studio if doesn't exists?

I have a Database project for my personal project and I am trying to deploy my code to my DEV server. I frequently delete and re-create my DEV Server. Right now, DEV Server is newly created with SQL Server. Every time I want to deploy my code I have to manually create Database Project and then publish database project. I want to automate creation of Database with database project deployment.
Right now, I have a script that creates database, but I have to execute it manually. And this is working perfectly but I want to automate this step as well.
Is this even possible? If yes, then how? Please explain step by step. Also what will we mention for Initial Catalog in connection string?
Edit:
I tried to create Database by using
CREATE DATABASE LocalDbTest
in Pre-Deployment Script. But it didn't work. It is creating Database, but then tables are not getting created tables under it. Since I used master database as default database, it is creating table under master. It is not letting me select LocalDbTest database as default because it is not yet created, so I have to select Master as my default database. I tried to Change Database by:
USE LocalDbTest
GO
I used it just after creating Database but this didn't work because when generating script it is changing back to default database. This part is coming automatically when generating script.
USE [$(DatabaseName)];
GO
Also Visual Studio is not letting me add database name in front of table name like:
CREATE TABLE [LocalDbTest].[dbo].[TestTable]
I am getting error:
When you create an object of this type in a database project, the object's name must contain no more than two parts.
If you have a script ready for database creation, you can use the Pre-build event to call SQLCMD and run your script.
Edit:
If you have trouble pointing to a database that does not exist, you may have to manually edit the publish profile (ex. dev.publish.xml) and set the TargetDatabaseName element explicitly. You can also set CreateNewDatabase element to True if you want to be recreated every time it gets published.
Answer:
You can use a publish profile and hardcode the target database in it.

Two SQL Server instances with the same name?

I'm not sure how I ended up with this situation: I seem to have two SQL Server instances with the same name, in one machine.
Screenshot here
When I connect through SQL Management Studio I have access to the database of my application, but it is empty, tables have no rows.
However when my application connects through the next connection string it has all the rows it had inserted, I mean, the application works fine.
Data Source=DESKTOP-D5BH4BP;Initial Catalog=AppDB;User ID=AppUsr;Password=pass;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
So I guess each instance points to its own files.
The question is: How can I get rid of the empty instance??
You cannot have two instances with the same name on SQL Server, it is not possible at all.
The drop down you have shared in your screenshot is only showing the server names you have connected to in past using SSMS, you can type anything there but whether that server exists or not is something different.
To get rid of these names from the drop down list, just select the name and press Delete key.
Click on < Browse for more...> and you'll see the single instance
If you want to remove the duplicate entry from the drop down, just mouse hover on the entry and hit delete on the keyboard.

missing the database

I have a problem with my database which is base on access. when i try to use insert command, all records send to mydatabase.mdb in root folder, but when I want to read from database, records come from another database which is in bin folder.
it cause a big problem because every time I should copy mydatabase.mdb from root folder to bin folder !!!
how I can solve this problem?
P.S.
I use the same connection string in both insert and select command.
public OleDbConnection con = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=F:\\fanoos\\new work\\C# regester_markaze tebi\\regester\\regester\\bin\\Debug\\Db_reg.mdb");
It sounds like your database has linked tables which are pointing to the "other database which is in the bin folder". Open the database you are connecting to in Access and use the linked table manager to see where the linked tables are pointing. In Access 2010+ (and maybe 2007+) the linked table manager is located on the External Data ribbon tab.

How can I use a SQL Scripts in a Database Project with the System.Data.SQLite data provider?

I've got a project where I'm attempting to use SQLite via System.Data.SQLite. In my attempts to keep the database under version-control, I went ahead and created a Database Project in my VS2008. Sounds fine, right?
I created my first table create script and tried to run it using right-click->Run on the script and I get this error message:
This operation is not supported for the provider or data source you are using.
Does anyone know if there's an automatic way to use scripts that are part of database project against SQLite databases referenced by the databases, using the provider supplied by the System.Data.SQLite install?
I've tried every variation I can think of in an attempt to get the script to run using the default Run or Run On... commands. Here's the script in it's most verbose and probably incorrect form:
USE Characters
GO
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'U' AND name = 'Skills')
BEGIN
DROP Table Skills
END
GO
CREATE TABLE Skills
(
SkillID INTEGER PRIMARY KEY AUTOINCREMENT,
SkillName TEXT,
Description TEXT
)
GO
Please note, this is my first attempt at using a Database, and also the first time I've ever touched SQLite. In my attempts to get it to run, I've stripped any and everything out except for the CREATE TABLE command.
UPDATE: Ok, so as Robert Harvey points out below, this looks like an SQL Server stored procedure. I went into the Server Explorer and used my connection (from the Database project) to get do what he suggested regarding creating a table. I can generate SQL from to create the table and it comes out like thus:
CREATE TABLE [Skills] (
[SkillID] integer PRIMARY KEY NOT NULL,
[SkillName] text NOT NULL,
[Description] text NOT NULL
);
I can easily copy this and add it to the project (or add it to another project that handles the rest of my data-access), but is there anyway to automate this on build? I suppose, since SQLite is a single-file in this case that I could also keep the built database under version-control as well.
Thoughts? Best practices for this instance?
UPDATE: I'm thinking that, since I plan on using Fluent NHibernate, I may just use it's auto-persistence model to keep my database up-to-snuff and effectively in source control. Thoughts? Pitfalls? I think I'll have to keep initial population inserts in source-control separately, but it should work.
I built my database using an SQLite SQL script and then fed that into the sqlite3.exe console program like this.
c:\sqlite3.exe mydatabase.db < FileContainingSQLiteSQLCommands
John
Well, your script looks like a SQL Server stored procedure. SQLite most likely doesn't support this, because
It doesn't support stored procedures, and
It doesn't understand SQL Server T-SQL
SQL is actually a pseudo-standard. It differs between vendors and sometimes even between different versions of a product within the same vendor.
That said, I don't see any reason why you can't run any (SQLite compatible) SQL statement against the SQLite database by opening up connection and command objects, just like you would with SQL Server.
Since, however, you are new to databases and SQLite, here is how you should start. I assume you already have SQLite installed
Create a new Windows Application in Visual Studio 2008. The database application will be of no use to you.
Open the Server Explorer by pulling down the View menu and selecting Server Explorer.
Create a new connection by right-clicking on the Data Connections node in Server Explorer and clicking on Add New Connection...
Click the Change button
Select the SQLite provider
Give your database a file name.
Click OK.
A new Data Connection should appear in the Server Explorer. You can create your first table by right-clicking on the Tables node and selecting Add New Table.

How Do I Copy and Overwrite a Database in SQL Server 2005 with SSIS?

I HAVE to be missing something really simple here!
I have a database in my development environment called Project.
I have a database in my test environment called Project_UAT.
I've created an SSIS package that successfully copied my database from Project to Project_UAT. I'm pretty sure this eliminates most permission and configuration issues.
Now, I want to re-create the package and this time allow it to overwrite the destination, which is Project_UAT. This is simply because from time to time I want to click a button in the Microsoft SQL Management Studio that pushes the new database schema, data, users, and everything, out to my testing environment. I WANT to overwrite the data.
So I create the package just like I did before, but this time I specify the already-existing database name as the "Destination database" and I select the radio button called "Drop any database on the destination server with the same name, then continue with the database transfer, overwriting existing database files."
I click Next, and what does it tell me?
"Database name already exists at destination"
Well, I KNOW! I just told you I want to overwrite it!
How do I make this work?
Not sure if I am missing the point but why do you not use a task to drop/delete the existing database prior to your deployment step?
Perhaps you could qualify the SSIS Component Tasks you are using within your SSIS package.
Cheers, John
You can add an Execute SQL Task into the Control Flow to drop the database. Just set the SQLStatement property to
DROP DATABASE Project_UAT
After this step is executed the new copy of the Project_UAT database won't have to overwrite the old one.
I had this problem because I deleted the database before hand. The database is not in the destination folder, but SQL Server 2008 still thinks it is there. Refresh didn't work. And SQL Server wouldn't honor the selection of "Drop any database on the destination server..." It just complained that the database already existed.
Guys this is a common sense solution. A lot of complexity for nothing.
Backup the destination database you want to copy to and delete the destination database.
Open the copy database wizard and follow the steps.
Use the detach and attach method.
When you get to the configure destination database use the option if destination database exists select drop database on destination server with same name.
Now it will continue to the next screen.
But this only works if you delete the destination database first before starting the wizard.
I may have missed something but this worked for me.

Resources