Changing database name for existing Asp.net MVC3 project - database

I am working on Asp.net MVC 3 project, I need change the database name for the project I tried changing the database name in connection string & What happens is that when I pull the data it pulls from new DB where as when I try to insert data in some table it tries to insert in the old db. I am sure from where it is getting reference to the old DB name. Please HELP.

There must be a different connection string for the inserts. You should make a search in the entier solution after the database name or parts of the connection string to find that connection string and modify it too.

Related

Schema error when running update-database command to create identity tables for npgsql data provider

I am manually creating the identity tables for a new ASP.NET 6 project following this tutorial. Such tutorial is for MS SQL Server and I am using PostgreSQL so I made the appropriate modifications. Although the process is straightforward, I have a problem when reaching the update-database step where I get the following error: "Couldn´t set schema (Parameter ´schema')". My connection string is as follows: "host=localhost; database=testdb001; schema=testdb001; port=5433; user id=some-user; password=some-password;". I found that such error disappears and the identity tables are successfully created if I remove the schema parameter in the connection string but tables are created in the public schema that PostgreSQL automatically includes when a new DB is created. However, I do not want this to happen because I want to use another schema name. I visited connection strings website for PostgreSQL (https://www.connectionstrings.com/postgresql/) and I see a link for Npgsql, and all the examples here do not contain the schema parameter. This is the first time that I use npgsql. Is there a way to create the identity tables in a specific schema name?
Maybe a workaround is to specify a schema name in the search_path parameter in postgresql.conf file but this would lead to add a name every time I define a new schema. I think that the schema name in the connection string is a great choice but I wonder why this is not accepted for npgsql.
Respectfully,
Jorge Maldonado
I found the solution.
For npgsql you must use search path as a connection string parameter instead of schema. So a connection string is as follows
"host=localhost; database=testdb001; search path=testdb001; port=5433; user id=some-user; password=some-password;"
and not
"host=localhost; database=testdb001; schema=testdb001; port=5433; user id=some-user; password=some-password;"
Regards,
Jorge Maldonado

How to access LocalDB without SqlServer from a winforms app c#?

Scenario:
I have programmatically created a Database on Master using an appropriate connection string and added tables to it. But when I tried to access it to create a stored procedure I cannot as it says, it does not have access to perform any action on DB.
I used this query with C# and tried to create the stored procedure.
//Connection String ("Server=localhost;Integrated security=SSPI;database=master") => For Createing Database
//Connection String ("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\MyAppDB.mdf;Integrated Security=True") => For trying to create stored procedure on already created Local DB
string AccountEmail = "CREATE PROCEDURE [MyAppDB].[dbo].[usp_AccountEmail]( #Email varchar(max)) AS SELECT Username FROM [dbo].[User] WHERE Email = #Email";
SqlCommand mySP = new SqlCommand(AccountEmail, sqlcon);
using [MyAppDB].[dbo].[usp_AccountEmail] resulted in Exception of specifying the database name. But when I tried the same like [dbo].[usp_AccountEmail] it executed and created a stored procedure on master but that's not what I want, I want it to be created on localDB.
The Problem:
I want to add a stored procedure to the already created database and I want to use the DB without installing any SQL Packages by the client. Just clean install of my app and run which connects to the local DB which is created programmatically by my app. I am struggling with attaching the DB with the installer and if I even do the connection string from the app cannot get the proper path of the attached DB which the client chooses (not definite).
Common Question:
is there any common approach to attach local DB with the set of files to the installer so that the client doesn't need to install any other packages?
The Goal:
All I want is to create an installer that has DB and AppData which does not require any additional packages to be installed and the app uses the DB to store and retrieve the data plus the database already has tables and stored procedures.
Please Help me with this Guys. Thanks in advance.

Should we delete physical database for the deleted clients in SAAS application

I am working on a project which is in .net core and hosted on azure and being used as SAAS application by different clients all over the world having separate databases.
Now I got a requirement which says we should delete the databases of those clients which are no longer in master db and deleted by admin.
I am in doubt that should we really delete the database for the clients which would be getting deleted?
Also if yes is it feasible to delete the database through application or we should use some utility to do that?
Also I am afraid if server will allow application to delete the database?
Can any one please suggest me on this?
Following tech stack i am using:
.net core (backend) with EF core
MSSQL (database)
Theres a few different ways.. Essentially it works the same as any other database, so you can execute T-SQL to do it.. or use the Azure SDK.. Here is some examples of different approaches: https://learn.microsoft.com/en-us/sql/t-sql/statements/drop-database-transact-sql?view=sql-server-2017
You can execute raw SQL statements in EF like so:
using(var context = new SampleContext())
{
var commandText = "INSERT Categories (CategoryName) VALUES (#CategoryName)";
var name = new SqlParameter("#CategoryName", "Test");
context.Database.ExecuteSqlCommand(commandText, name);
}
So, using that format, you could add your DROP Database command in? Taken from: https://www.learnentityframeworkcore.com/raw-sql - under the Database.ExecuteCommand section

Access won't connect to a different SQL Server database

I have an Access application that writes to a SQL Server database. I would now like it to write to a different SQL Server database instead.
Made sure the new database if properly configured with dbowner permissions for my user credentials
Wrote a test record using that user credentials
In the Control Panel, set ODBC source to the right database. Tested connection successfully
In Access, Database tools > Visual Basic section, I changed the connection string and substituted the login credentials with the new userid and password.
But when I input values in the form, it STILL saves to the old database.
I did notice that in the Linked Table Manager, the linked tables still point to the older database. But when I select all and click OK, the message is 'All selected tables have been successfully refreshed'.
What do I need to do to point the tables to the new database?
Edit
I did a 'Find' for the old database name and edited the connection string on all the forms where-ever that old database name existed and replaced it with my new database name.
Example:
.ConnectionString = "Provider=SQLNCLI11;Data Source=(old datasource name);Initial Catalog=(old database name);User ID=(old userID);Password=(old password)"
changed to
.ConnectionString = "Provider=SQLNCLI11;Data Source=(new datasource name);Initial Catalog=(new database name);User ID=(new userID);Password=(new password)"
It sounds like you need to delete your old linked tables and create new ones. If you hover over the linked table it should show the connection string.
If you are worried about losing your old linked table, you could rename it to something else.

Setting up a Entity Framework Code First Database on SQL Server 2008

I successfully created my first Entity Framework Code first project using SQL Server CE. The database tables were created automatically and all was well.
I then changed my connection string to point to a SQL Server database I'd created and expected it to autogenerate the tables. It didn't and instead complained about the tables not existing.
After some reading I found that I needed to add an initializer to my App_Start method.
I tried:
// Method 1
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EFContext>());
But this didn't work at first because it couldn't find an EdmMetaData table to discover if the model had changed.
So I tried adding:
// Method 2
Database.SetInitializer(new CreateDatabaseIfNotExists<EFContext>());
But this didn't work because I'd already created the database, and when I removed the database it still didn't work because the user I was logging in as didn't have create database privileges.
I finally tried:
// Method 3
Database.SetInitializer(new DropCreateDatabaseAlways<EFContext>());
Which worked, but I don't really want to drop the db every time. So once it ran and added the EdmMetaData table I replaced it with method 1 and that now works fine.
Although this method works, it doesn't feel as elegant as when I was using SQL Server CE and I've not found someone suggesting that you use this method.
So, am I missing something and is there an easier, more elegant way to do this?
Method 1 and 2 expects that database doesn't exist yet. They don't work if you create database manually. If you need EF to create tables in existing database you must create custom database initializer.

Resources