How to open SQLite database while debugging in VS 2019 C# UWP - c#

I've read a ton of articles on this topic but still not able to overcome the problem.
Standard code to open a database:
using Microsoft.Data.Sqlite;
string _dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "orders.db");
using (SqliteConnection db = new SqliteConnection($"Filename={_dbPath}"))
{
db.Open();
etc.
}
The system doesn't produce any error and the databse has Open state. But it's not my database! It seems the system open new empty database (I can create tables, isert/read records there) instead.
Can anybody explain me what I should do to be able to work with my existing database?
I set Mode=ReadWrite - now the new database is not created and produces the error: cannot open db
The database is located in the project root directory.

Related

windows forms connect to local database

I have created a very basic windows form application using visual studio 2015.
A service based database has been added (mdf database), and a table has been created..
The database has been added to the service explore, and i can see the connection string in the setting file.
I am simple trying to open a connection to the database to be able to write to it.. Cant seem to find any basic example..
Tried to use this :-
using (var con = new
SqlConnection(Properties.Settings.Default.MyConnectionString))
{
con.Open();
}
}
but no luck. Seem to get the following error
A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."}
my connection string :-
Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirector‌​y|\Data\mydb.mdf;Int‌​egrated Security=True

MySQL c# Winform - Create new database from dump file (localhost)

I have a problem I need to solve, but not know how exactly. I have a WinForms application (C#) which connects to an online MySQL server - no problem there. In this application I have an option to make database backups (basically I dump this database to a local file on a computer).
I would like to locally "open" this backup on client's computer (to check some old data) - I don't want to make database restore on my server, because database must still be in use for other users. I want to make clean install of MySQL on a local computer and connect to it trough localhost (like I do for testing ), but I do not have physical access to that computer. I can send MySQL installer to my client, but how to go about automatically creating user with password and database from my dump file?
I know how to create a new database if it doesn't exist, but how to do it if it's clean install of MySQL server - no user and password yet.
string connStr = "server=localhost;user=root;port=3306;password=????;";
using (var conn = new MySqlConnection(connStr))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "CREATE DATABASE IF NOT EXISTS `hello`;";
cmd.ExecuteNonQuery();
}
Any help and direction is appreciated.
Regards!
I don't know whether I understand your problem. However,if you install a new Mysql,you can use root user through no database.
Anyway, one thing you need to know,Connect database must has user and password for mysql or sqlserver.
You may need to be more concise description of your problem.

Local DB in Visual Studio 2015

After a few years, I have returned to writing in C# and I am really struggling here - I would like to have my app to have a local SQL database. I have added Service-based database and "Database1.mdf" was added to my project. I created a table and added some data just to see if it is working but I cannot connect to it. I tried numerous connection strings with no success (Server not accessible).
Do I need to run something else in the background? I thought that I might have a local database and with .NET client I can access it, and I hoped it would work whenever I bring my application (also not requiring any SQL server running). Is that wrong?
If you don't require any SQL server, take a look at SQLite. This is lite SQL database engine. Database is just one file. C# has a great library to SQLite on NuGet: https://www.nuget.org/profiles/mistachkin
SQLite is widely used, event in Android (as a native db engine).
here is what i use to connect. it appears as a Data Connection in Server Explorer.
string con2 = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + Application.StartupPath + "\\jobfile_2017.mdf;Integrated Security=True;Connect Timeout=30";
when i first started working with these, i used this source 1
it works on PC's that i have nothing installed (not even office) but as i said i'd be interested to know of any shortcomings of this method
I experiencing same problem and decided to move mdf file to static c:\db directory. Connection string was changed to incorporate new file location (AttachDbFile).
But AttachDbFile has some issues also (by some reason one table in db is inaccesible and error is access denied).
So I decided to move to Sqlite. LocalDb has many issues to work with. I read good note to resolve problem: in command line stop/remove/start name of instance. But it nuissance.
Wish you good luck to work with these db files.

Getting "The underlying provider failed on Open" exception for Sybase database

I am working on application which uses Sybase database and entity framework for accessing database. I am trying to make application to open connection itself and close it. I created model using Sybase database file and now connecting to it to get data. But when I try to get data I get exception "The underlying provider failed to Open".
Here is my code.
var connectionString = metadata=res://*/SampleModel.csdl|res://*/SampleModel.ssdl|res://*/SampleModel.msl; +
provider=iAnywhere.Data.SQLAnywhere; +
provider connection string="DBF=D:\SampleDatabase.db;UID=DBA;PWD=sql"
var dataContext = new SampleContext(connectionString);
var contacts = dataContext.Contacts; --> Here I get this exception.
Note: If I create a DSN in ODBC and use DSN instead of giving filename it gives me Not connected to a database exception. If I open this connection and do not close it, then it works fine. But I don't want to create DSN entry and open it manually. I want my program to do this.
There was problem with my version of Sybase. I was using 3840 build of Sybase 12. I removed it and installed 3769 version and it worked fine. On different forums I found out that latest (3840) is having problems.

C# and SQLite: "no such table error" when using relative path?

I am having issues connecting to my sqlite database. The file is located in the application's folder. Here is the connection string
string path = "Data Source=MY.db";
I can get it to work if I use the absolute path, but it gives me a "table not found" error if I try to use a relative path. Any ideas?
You are opening up a different -- perhaps a new -- database that does not have said table. (Yes, SQLite will happily create a new database with the default connection settings.)
Make sure the correct database is opened. Remember, relative path is relative to the Current Working Directory, which is likely not that which is expected.
(The working directory is influenced from where, and how, the process is loaded. The working directory for a "Debug" session can be set under Project Settings / Debug / Start Options, for instance.)
Happy coding.
See also:
Make SQLite connection fail if database is missing? (deleted/moved)
Defining a working directory for executing a program (C#) (Shows how to set the current working directory to the directory containing the executing assembly.)
How do I get/set a winforms application's working directory?
Getting path relative to the current working directory?
This happened when you haven't saved the database and its table while using GUI Manager for SQLite .
Two solution;
1) Save your database and its table with CTR+S in GUI Manager
2) Or Simply Just close your GUI manager of SQlite and save all .
Important ! I am using GUI manger for SQLITE (DB Browser for SQLITE) and its all about that.
I've had the same problem for both my windows application (C#) and web application (ASP.net). I usually use SQLite because I found it more easier, especially when I worked with connection strings. But the main obstacle for me was to put a relative path in my code, so I can publish it without worrying about being unable to find the database. I've tried many things(using "|Data Directory|", "~/", "./", ...), and none of them works until I found these solutions. It seems the code is working for me, but wonder if I'm using them right?!
Web Application:
SQLiteConnection sql_con = new SQLiteConnection("Data Source =" + Server.MapPath("~/") + "mydb.db; Version = 3; New = false;);
Windows App:
SQLiteConnection sql_con = new SQLiteConnection("Data Source =" + System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "mydb.db; Version = 3; New = false; Read Only = true");
just replace your .database file into \bin\Debug in project folder, because in your case compiler creates DB file with same name but its totally empty 0bytes

Categories

Resources