Cannot create database file programmatically (SQL Server 2008 R2 Express) - c#

I'm trying to create a database with LINQ to SQL programmatically.
I tried (following this):
MySQLSvrDb db = new MySQLSvrDb(#"c:\mydb.mdf");
if (!db.DatabaseExists())
{
db.CreateDatabase();
}
But I get a SQLException "A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."
However, for unit tests I created a database the following way (making sure I have a empty db for every test):
MySQLSvrDb db = new MySQLSvrDb(#"C:\testdb.mdf");
if (db.DatabaseExists())
{
Console.WriteLine("Deleting old database...");
db.DeleteDatabase();
}
db.CreateDatabase();
This works fine. My problem is I don't see a difference to the first approach. The problem is maybe somehow related to this, but the suggested solutions didn't work.
Any hints?
EDIT
If I just skip the DatabaseExists() step it works, but I need to check, if there's already a Db.

How to: Dynamically Create a Database (LINQ to SQL)
How to create DB in Linq to sql
Did you try deleting the first created DB first? and run the method again?
Why you create Database locally,do you still attached it using sql server?
You can use sql lite or sql server ce DB instead.
Regards!

Related

Is there a way to see a database create by SQLiteAsyncConnection class

I'm using SQLiteAsyncConnection in my project to hold my data locally. I would like to see the data inside its tables like using sql server maybe or some tool like that. I've tried where it was created and to open that .db file with sql server but I didn't managed to find it's location
this is how I create the db
var path = Path.Combine(FileSystem.AppDataDirectory, "AppointmentsDB.db");
db = new SQLiteAsyncConnection(path);
I have tried printing the path or
db.DatabasePath
but it gives me a relative path something like this
/data/data/com.companyname.appointments
and I don't know that it's relative to
Now my question is.. is it possible to open that SQLite database with a "visual" tool like sql server 2019? If now then what would you suggest me to use so I can also see that data from sql server 2019?
You can execute "adb pull" to remove the database file from the emulator and put it on your regular hard drive. Similar to "adb pull /data/data/com.companyname.appointments/databases/xxx.db".
If you want to view directly, you need to root your emulator.

Why am I unable to delete records on local service based database with EF 6?

I have the following code to delete all records from a table. The table is in a local Service-based Database that will hold temporary pricing data on a desktop app. When this code runs, it logs that it connects, deletes, and commits the transaction. I can see that it has 1500 delete logs which matches the row count. But when I go to the table in Visual Studio, all the data is still there like nothing was deleted. If I run this code on my instance of full SQL server, I can delete rows just as expected. Does EF not work with local service based DB's? My work around is to use SQLCommand/SQLConnection etc, which works just fine. What am I missing here?
using (var context = new PriceListLocalEntities())
{
context.Database.Log = Console.Write;
context.PRICE_LIST.RemoveRange(context.PRICE_LIST.Where(x => x.PRICE_ID != null));
context.SaveChanges();
}
With a service based DB in Visual Studio, a copy of the .mdf file is copied to the output folder. An Entity Model connects to the output version, while raw ADO.NET connects to the project version. Using both types of ADO.NET in the same app with access and update them separately. Which doesn't work for operations that are dependent on each other.

Programmatically script or migrate SQL Compact database for importing into SQL Server

I have used ErikEJ's SQLite/SQL server Compact Toolbox which allows migration from a compact database to SQL Server, but I am trying to implement this process into my ASP.NET application.
The user will have an already completed SQL Server Compact database.
The process will be
1) User selects the database
2) Existing SQL Server database will be deleted
3) Compact database will be scripted/migrated into SQL Server
The part I am unsure of is how I should script or migrate the SQL Compact database. Deleting and creating a new database on the server itself seems easy enough. I have been digging around in the System.Data.SqlServerCe class for a while and am unsure what the best approach would be.
Using my scripting API, you can do something like this:
using (IRepository ceRepository = new DB4Repository(#"Data Source=C:\Data\SQLCE\Test\nw40.sdf"))
{
string fileName = Path.GetTempFileName();
var generator = new Generator4(ceRepository, fileName);
generator.ScriptDatabaseToFile(Scope.SchemaData);
using (IRepository serverRepository = new ServerDBRepository4("Data Source=.;Trusted_Connection=true;Initial Catalog=Test"))
{
serverRepository.ExecuteSqlFile(fileName);
}
}
See my blog post here: http://erikej.blogspot.dk/2013/03/sql-server-compact-code-snippet-of-week_8.html
A more complete implementation (from my Toolbox)
https://github.com/ErikEJ/SqlCeToolbox/blob/master/src/GUI/SqlCe35Toolbox/Commands/SqlServerDatabaseMenuCommandsHandler.cs#L370

Preload the datas in local database in Window phone 8 development

I am new to Windows pnone 8 application development. In my application I need to use local database where datas are constant.(should stored already in database). I gothrough the the following link to use the local database. The datas are giving in runtime. However, How can I preload the datas into the database?
.
http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202876(v=vs.105).aspx
Use the same code which initializes/created database to fill it with required data.
From the tutorial you linked to:
using (ToDoDataContext db = new ToDoDataContext(ToDoDataContext.DBConnectionString))
{
if (db.DatabaseExists() == false)
{
//Create the database
db.CreateDatabase();
//Fill database with data you need
}
}
Depends a bit on what DB you are using, but when you go for SQL CE you can pre load an existing db at start up of your app.
Take a look at this article for all the details: http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh286411(v=vs.105).aspx
But what you need is the class: DataHelper with the method MoveReferenceDatabase - the code is in the article.
Your SQL CE db can be modified using SQL Management studio, or you could use the tools of Erik show here: http://erikej.blogspot.be/2013/04/generate-windows-phone-8-local-database.html

Creating MySql Database on new install of c# application if it doesnt exist

I am about to deploy my application and have came into a bit of trouble.
I have the connection string for the database held in the application.settings and need a way to check if the database exists when the program first starts up, and if it doesn't, i need the program to create it before starting the program.
I am assuming it would be a mysql statement to check if db exists, if not create. However, I don't know where or how to do this, can I create a mysql dump of a blank database with tables etc already created and use that?
I have already stored the mysql dll files locally so there is no problem with that, its just creating the database that the string wants to connect to before the application runs so there are no connection errors straight away.
Thanks.
You can do this by running the following SQL statement:
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = "my_db"
If it doesn't exist from the result set you get returned you can then create it.
This does pose questions regarding MySQL permissions and if your application should have user rights that enable such checking.
Edit in response of comments.
It isn’t clear if you create the connection string or not – I’ll assume the worst and that it is a part of the setup so your client can enter it (if you do know it the process below simplifies.
I would pass the connection string to the constructor of the MySqlConnectionStringBuilder class, this then makes it easy to connect to the database using the MySqlConnection class. I would use the properties from the new instance of the MySqlConnectionStringBuilder class (Server, Host, User etc) to setup the MySqlConnection class.
If the connection didn’t work I would return information to the user and they can update their connection string.
Once I’ve successfully connected to the database I would then use the database name from the Database property of my MySqlConnectionStringBuilder instance to build the query above.
If the command returns NULL the database doesn't exist and then needs creating, if the database does exist then the command will return the name of the database.
Now there are two paths:
It Doesn't exist – It needs creating, I would probably have an external SQL file with the create statements in (can be produced by MySQL dump by using the –nodata option). I would parse this file and execute the create statements
It does exist – I would now check the structure of the database to make sure it is compatible before continuing the installation.

Categories