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
Related
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
I made a C# project that contains a local database (Microsoft SQL Database Server) (mdf) in my pc that works totally fine. but whenever i use my laptop to run it, it gives me this error:
The database 'C:(the path)\CALENDER.MDF' cannot be opened because it is version 839. This server supports version 782 and earlier. A downgrade path is not supported.
this error appears every time i try to refresh server explorer. i need it to work in my laptop because i use it to make a class presentation.
After a long researches and tries i figured how to solve this issue. its a bit complicated. i converted the mdf database to and access(mdb) and import data from the mdb to a new mdf database.
these are the steps:
Create an empty access (mdb) database.
using SQL Server 2016 CTP3.0 Import and Export Data: import data from database which is calander.mdf in my case to the new access database (mdb).
now in the destination pc, create a new vs form and add a new empty mdf database.
using Microsoft SQL Server Management Studio ( Databases > right click new database > type database name and click OK.)
Right mouse click on your database name and hover to Tasks and then select the Import Data.
select the data source Microsoft Access (Microsoft Access Database Engine) and browse to the access database and click next.
in Destination select Net Framework Data Provider For SqlServer and type the connection string for the new empty created mdf database. then click next and finish.
Copy the new mdf database To your project file
now you got the new database filled with data and all is left is go to your main project and delete the database and then add new existing item and browse to the new database and log file and clock ok. and the database should work
WORKED WITH ME !!!!!!
I am creating a application and I want to use a local database stored on the clients local machines. I am debating over if I should use SQLITE or is there something in Visual Studio to help me. The other thing is that I want to create the database programmatically in the users directory when the application is launched.
I am see a few things online but the articles were all about SQL Server stuff and that is not want I want to do with this application. All data will need to be stored on the local machine.
You can use SQL Server Compact, which has tooling in Visual Studio. It's syntax-compatible with SQL Server, but stores its data in a local file, which you can create on the fly (at app startup, for example).
You can create the SQLite database on the fly with the libraries provided from their website. I have used it in many projects for my personal code, as well as it being used in some of the internal architecture of Data Explorer (IBM Product). Some sample C# to create a database file:
if (!Directory.Exists(Application.StartupPath + "\\data"))
{
Directory.CreateDirectory(Application.StartupPath + "\\data");
}
SQLiteConnection conGlobal;
if (!File.Exists(dbGlobal))
{
conGlobal = new SQLiteConnection("Data Source=" + dbGlobal + ";New=True;Compress=True;PRAGMA synchronous = 1;PRAGMA journal_mode=WAL");
conGlobal.SetExtendedResultCodes(true);
firstRun = true;
}
else
{
conGlobal = new SQLiteConnection("Data Source=" + dbGlobal + ";Compress=True;PRAGMA synchronous = 1;PRAGMA journal_mode=WAL");
conGlobal.SetExtendedResultCodes(true);
}
try
{
conGlobal.Open();
}
catch (Exception)
{
//do stuff
}
Simply initiating a connection to the file will create it if the new=true is passed as the connection string. Then you can query it and get results just like you would any database.
You also have the ability to password protect the database files to prevent access to them from just opening them with an SQLite-Shell or a different SQLite DB viewer.
For more info on the pragma statements that are being passed in the connection string, see the following: http://www.sqlite.org/pragma.html
I'm not sure about programmatically (that's probably what you meant, right?) creating the database, but SQL Server Compact Edition has served me well in the past for simple apps. It's embedded and even runs in medium trust.
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!
I am developing window phone 7 application. I am new to the Window Phone 7 application development. I want to use the SQL server compact edition database in my application for database connectivity. So in my application I have created the database by right clicking the 'Data Connection' & Choosing the 'Add Connection' in Server Explorer. After that I have created the database by using the Create button in the opened window.Then I have tested my connection. It was successful. I have added that created database file in my application by right clicking on the Project Name & choosing the add existing item. The database file was added as the 'ExpenseManagerDB.sdf' I have created the tables for this database by using the Server Explorer. Now I want to programmatically do the database connectivity for my application. So I am using the following code
namespace ExpenseMgrMobAppl.Category
{
public partial class Category : PhoneApplicationPage
{
private void Submitbutton_Click(object sender, RoutedEventArgs e)
{
string conSTR = "Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) + "\\ExpenseManagerDB.sdf;Persist Security Info=False";
SqlCeConnection connection = new SqlCeConnection(conSTR);
SqlCeCommand cmd = new SqlCeCommand("select category_name form category where category_id=1", connection);
connection.Open();
textBlock1.Text = cmd.ExecuteScalar.ToString();
connection.Close();
}
}
}
But I am not finding any Namespace/Assembly for the SqlCeConnection class. I have also tried to add reference to the dll 'System.Data.SqlServerCE.dll' in my application. But the reference could not be added. What should I need do for database connectivity ? What I need to do to use the SqlCeConnection class in my application ? I am looking for thing from which I can use the SqlCeConnection class in my application ? Can you please provide me any solution or link through which I can resolve the above issue ? If I am doing anything wrong in above case then please guide me.
SQL CE is not available in this release of the tools.
The alternatives are storage behind a web service, 3rd party db sources like Sterling or local storage in files using for example XML and LINQ.
As Mick N says there are some 3rd party databases available for the phone. Have a look at
Perst (Commercial, but I believe free for non-commercial use)
Sterling
Windows Phone 7 Database
However my advice would be to think carefully about whether you really need a full relational database in your app on the phone. If you are just using it to store and retrieve data, binary serialized files are extremely efficient and lightweight.
Remember the phone is a very resource constrained environment. Whereas on the desktop we would immediately turn to a database for most data storage requirements because it makes our life easier. On the phone we need to be more careful and optimize for the environment - even if that makes our lives as programmers a little harder. A full database shouldn't necessarily be the first option when thinking about storing data.
That said if you have a data requirement that you feel warrants a full relational database then you should absolutely use one of the aforementioned DBs.