I'm trying to protect a Access database by using a workgroup file as user-security. The file must be in the same folder as the database and you must enter a user name and password to access the database. I read something about the parameter "System Database" in connectionString, but I'm not sure how to use it.
The database is created like this:
String accessConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + filepath";
using (OleDbConnection accessConnection = new OleDbConnection(accessConnectionString))
{
ADOX.Catalog cat = new ADOX.Catalog();
cat.ActiveConnection.Close();
var newCreatedCatalog= cat.Create(accessConnectionString);
OleDbCommand oleCommand = new OleDbCommand();
// fill database
}
Related
I'm trying to make a backup to a network shared folder with the following code:
string filePath = ("\\pc-usuario\folder\backup\backup.bak")
string connectionString = String.Format(#"Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=True", server, database);
using (var connection = new SqlConnection(connectionString))
{
var query = String.Format("BACKUP DATABASE [{0}] TO DISK='{1}'", database, filePath);
using (var command = new SqlCommand(query, connection))
{
connection.Open();
command.CommandTimeout = 1800;
command.ExecuteNonQuery();
}
}
Got the following error:
The backup device cannot be opened. Operating system error 5(Access denied.).
If I try it with SQLExpress the same error happens. What am I missing?
You havent given the database backup a file name, only a file path
E.g. the final code should read
backup database wibble to disk = '\\pc-usuario\folder\backup\wibble.bak'
Please i want to creat a Database if not exist, and after if the Database exist i want to creat tables.
I use this code :
using Npgsql;
string server = "localhost";
string database = "MyNewDatabase";
string password = "password1234";
string connectionString = #"Server=" + server + ";User Id=postgres; Password=" + password + ";";
var connection = new NpgsqlConnection(connectionString);
string commandText = string.Format("CREATE DATABASE IF NOT EXISTS \"{0}\";", database);
var command = new NpgsqlCommand(commandText);
connection.Open();
command.Connection = connection;
command.ExecuteNonQuery();
connection.Close();
the problem is i get a error : 42601 (syntax error using « NOT »).
Using C#, i am trying to verify if the database exist, if not we will creat it, and after we will verify if the tables exist, if they not exist we will creat them.
Please, may someone tell me how to do it?
Thanks stackoverflowers
I have created a project in visual studio, that uses a database to store and retrieve details.
I want to deliver an executable and just let the user know that he has to place the database file on the desktop.
if i do
String myDesktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
it looks like i cant set the database position after like this:
using (SqlCeConnection cn = new SqlCeConnection(#"Data Source = myDesktop\Database1.sdf))
Any help?
Form a correct path using the Path.Combine method of the Path class
string conString = "Data Source=" + Path.Combine(myDesktop, "Database1.sdf;");
using (SqlCeConnection cn = new SqlCeConnection(conString))
I have this connection string :
OleDbConnection con = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\Release\DB.accdb"); // Database Connection
I want my program to connect to the database without the "..\Release\" in the string.
What I mean is, that i want the program to look for the database in the program's folder, without specifying the folder's name (whatever the folder's name is).
How is that done?
You should add your DB to the project (Add -> Existing Item...) and set Build Action to Content and Copy to Output Directory to Copy always:
After that you can using following connection string:
string cs = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=test.mdb;Persist Security Info=False;";
Following code, will find database file in program folder or sub-folder:
string programPath = System.IO.Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;
var dbPath = System.IO.Directory.GetFiles(programPath, "*.accdb", SearchOption.AllDirectories).FirstOrDefault();
string cs = null;
if (!string.IsNullOrEmpty(dbPath))
{
cs = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;", dbPath);
}
Perhaps you could use something like
String strAppDir = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
String strFullPathToMyFile = System.IO.Path.Combine(strAppDir, "DB.accdb");
ref:
How to: Get the Application Directory
This is my C# code to connect and work with an Access database.
using System.Data.OleDb;
var cb = new OleDbCommandBuilder(da);
DataRow dRow = ds1.Tables["Customer"].NewRow();
dRow[0] = textBox1.Text;
dRow[1] = textBox2.Text;
dRow[2] = textBox3.Text;
ds1.Tables["Customer"].Rows.Add(dRow);
da.Update(ds1, "Customer");
con.Close();
MessageBox.Show("Entry added");
But the line da.Update(ds1,"Customer");, throws an exception:
The ConnectionString property has not been initialized.
I'm not following your question too well, but here's some sample code that may help you figure out whatever it is that you are trying to do.
For clarity: The database is named "MyDb.accdb" and has a table named "Customer" which has two fields "Name" and "Phone". This example assumes the database lives in the same directory as the executable.
private void AddCustomer(string customerName, string customerPhone)
{
string name = customerName;
string phone = customerPhone;
// An easy way to determine the connection string to your database is to open the database from Visual Studio's 'Server Explorer'.
// Then, from Server Explorer, view the Properties of the database - in the Properties you will see the "Connection String".
// You can/should replace the arbitrary part of the path with "|DataDirectory|".
string connString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|MyDb.accdb;Persist Security Info=True";
// Create your sql query in a string variable
string cmdText = string.Format("INSERT INTO Customer(Name, Phone) VALUES('{0}','{1}');", name, phone);
// Use the 'using' statement on your connection so that the resource is managed properly
using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(connString))
{
// Here's where/how we fire off the INSERT statement
OleDbCommand cmd = new OleDbCommand(cmdText, connection);
connection.Open();
cmd.ExecuteNonQuery();
}
}