Is there any way take backup of mongodb database using c# code? - c#

"it is showing error like :- JSON reader was expecting a value but
found 'mongodump'.'"
//i tried this code but itr=s not working
"var cmd = new JsonCommand<BsonDocument>(" mongodump--db XEAP --out
F:\\");
database.RunCommand(cmd);"
I want to take backup of database using c# code.
"var cmd = new JsonCommand<BsonDocument>(" mongodump--db XEAP --out
F:\\");
database.RunCommand(cmd);"

database.RunCommand executes MongoDB command.
mongodump is an OS-level executable. This thread explains how to run this kind of command from C#.
Run Command Prompt Commands

Related

Parameter Query using SQLite

I am trying to make a SQLite SQL query using parameters and for some reason its not working (i have done this with an OLE db before successfully). I took the below syntax from a google search but doesnt seem to be working.
It doesnt seem to like my command.perameter.add line and its giving me a "SQLite is not accessible due to its protection level" error.
I imagine its me converting from OLE to SQLlite syntax that is causing the issue but im not sure. Any ideas? My code is as below:
string sql = "INSERT INTO Jobs (jEnquiryNumber) VALUES (#jEnquiryNumber)";
MessageBox.Show(sql);
SQLiteCommand command = new SQLiteCommand(sql, dbConnectString);
using (command)
{
command.Parameters.Add("#jEnquiryNumber", SQLiteType.Text).Value = textBox1.Text;
}

Am the ODBC driver of MS Access .md and .accdb files only can handle 64 executions?

Im writing a C# program where i need to use the Odbc api to connect to a .mdb database file.
After the planning, implementation and tests, now theres a BIG problem.
It seems to be, that the MS Access driver can only handles exact 64 executions.
After that, the program has to restart.
Can someone confirm this issue?
Or were there other drivers, which were also more performant?
Greetings .. :)
After the idea of checking the cleanups of the resources, i got the answer .. -.- :
The "using" directive from C# to handle resources had no affect on the OdbcConnection object.
You have to call the Disponse method right before the next closing bracket.
This closing bracket calls the Disponse method, but - I REALLY DONT KNOW WHY - this had no affect.
using (OdbcConnection conn = new OdbcConnection(string.Format(connStr, odbcDriver,
dbq, pwd)))
{
conn.Open();
Console.WriteLine($"Conn Create");
await using OdbcCommand cmd = conn.CreateCommand();
await cmd.CommandText = "select * from [testmodel];";
cmd.Prepare();
DbDataReader reader = cmd.ExecuteReader();
Console.WriteLine($"DB EXEC COUNT '{i}'");
await conn.DisposeAsync();
}
I tried out pyhons pyodbc to reproduce, that it is not a language issue .. well
it worked fine..
with pyodbc.connect(f"Driver={dbAccessDriver};Dbq={dbFilePath};Pwd={dbPassword}") as conn:
conn: pyodbc.Connection
cmd: pyodbc.Cursor = conn.cursor()
cmd.execute("insert into [testmodel] values (?, ?)", (i, "Name " + str(i)))
cmd.commit()
Greetings to all programmers ..
Update 19.01.2021
Well, my last answer wasnt as correct as I expect.
I thought i had solved this problem, but its not.
Actually, my last solution has based on the disposed method after the using syntax of the connection and that has to work, because the triggered Disposed event is always the same.
The corrent solution is:
The problem is caused by reading from a table.
If reader were not closed OR disposed, this caused the error.
If you want to control the connections you have to go deeper and control read and write operations.

How can I tell if a SQLIte update/transaction was prevented due to database being locked by another write?

Like the title says, my SQLite query is below. I am using c# and writing an application. I need to know if the query below was prevented from writing to the database due to the database being locked by another write. Since I understand SQLite only allows one write at a time. Do the others just fail? Or do they get queued or something and wait for the lock to be removed and then begin?
using (var cmd = SQLite_Connection.CreateCommand())
using (var SQLite_Transaction= SQLite_Connection.BeginTransaction())
cmd.CommandText = UPDATE testDb.testTable SET testAge= 5 WHERE testName = Joseph
cmd.ExecuteNonQuery();
SQLite_Transaction.Commit();

Run MongoDB commands from C#

I want to be able to run any MongoDB command from C#. I know that this can be done. I start with a simple example, instead of using the dropDatabase method from the C# driver I am trying to drop a database using the db.runCommand method as follows.
I have tried in two ways, passing the command as a string and also passing the command as a BsonDocument but nothing is working and I don't have any clues where I'm wrong even after researching on the internet I cannot find a suitable example.
I'm having a really hard time to identify why this piece of code is not working.
Command passed as a string:
database.RunCommand<string>("{dropdatabase : 1}");
Command passed as a BSON document:
var command = new BsonDocument { {"dropdatabase", "1" } };
var execute = database.RunCommand<BsonDocument>(command);
You can use a JsonCommand like this:
var command = new JsonCommand<BsonDocument>("{ dropDatabase: 1 }");
db.RunCommand(command);
or use a CommandDocument like this:
var command = new CommandDocument("dropDatabase", 1);
db.RunCommand<BsonDocument>(command);

Insert Into local database C#

I can't Insert and select from Local database data in C#.
I've read these articles
C# - Writing data in local database
http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlceconnection(v=vs.100).aspx
How to add local database items into textBox using listBox in C#
All the code samples are the same, here's my sample.
SqlCeConnection conn = new SqlCeConnection(#"Data Source=|DataDirectory|\PacjenciDB.sdf");
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText="INSERT INTO pacjenci (nazwiskoimie,adres,skierowany,opis) values (#nazwiskoimie,#adres,#skierowany,#opis)";
cmd.Parameters.AddWithValue("#nazwiskoimie", txtnazwiskoimie.Text);
cmd.Parameters.AddWithValue("#adres", txtadres.Text);
cmd.Parameters.AddWithValue("#skierowany", txtskierowany.Text);
cmd.Parameters.AddWithValue("#opis", txtopis.Text);
cmd.ExecuteNonQuery();
conn.Close();
Can someone tell me what I'm doing wrong?
I've tried tons of samples about insert data, but it doesn't work.
I can manage .MDF, but .SDF seems quite problematic.
Hope you help me
Ok, I'm going to take a guess here. Is the PacjenciDB.sdf included into Visual Studio project by any chance? Do you have the property "Copy to output folder" set to "Always" or something similar? It seems that every time you do a build you could be overwriting your output folder database file. Try putting the database in a folder that is not inside VS project.
BTW, your code is OK.
Look for a copy of the database with data in your bin/debug folder.
Solution is to not use |DataDirectory|, but use full path instead.
The above code is correct and most likely the problem is somewhere else, where you call that code from. If there is a problem with db connection or data is wrong - you should get an exception. Since there is no error occurred and no new records added - the code is not executed at all.
P.S.
.sdf is a Sql Server Compact Local Database, so using System.Data.SqlServerCe is correct
http://msdn.microsoft.com/en-us/library/system.data.sqlserverce(v=vs.100).aspx
Try using double backslash when setting the path to your database file:
string dbPath + "Data Source=C:\\DataDirectory\\PacjenciDB.sdf";
SqlCeConnection conn = new SqlCeConnection(dbPath);
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText="INSERT INTO pacjenci (nazwiskoimie,adres,skierowany,opis) values (#nazwiskoimie,#adres,#skierowany,#opis)";
cmd.Parameters.AddWithValue("#nazwiskoimie", txtnazwiskoimie.Text);
cmd.Parameters.AddWithValue("#adres", txtadres.Text);
cmd.Parameters.AddWithValue("#skierowany", txtskierowany.Text);
cmd.Parameters.AddWithValue("#opis", txtopis.Text);
cmd.ExecuteNonQuery();
conn.Close();

Categories