Unable to Format Message Error when connecting to DB - c#

I am currently working on a function that connects to a database through a connection string that includes the parameter: "Provider=OleDB.Provider". When I ran the code I get the following error:
Unable to format message, ID: 0xc0010001: Class not registered
I have also made sure the file is located on the database and the name parameter I am passing matches the name of the Database, since I got this from the msdn webstie:
"The variable cannot be found. This occurs when an attempt is made to retrieve a variable from the Variables collection on a container during execution of the package, and the variable is not there. The variable name may have changed or the variable is not being created."
Some of the code I have for the connection is(error happens on conn.open):
using (OleDbConnection conn = new OleDbConnection("Provider=mrOleDB.Provider.2;Persist Security Info=False;User ID=\"\";Data Source=" + data_source + ";Location=\"Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" + user + "; PWD=" + password + ";Initial Catalog='" + jobnumber + "';Data Source=" + oDatabase + ";OLE DB Services=-4\";Extended Properties=\"\";Initial Catalog=" + oroot + jobnumber + "\\" + jobnumber + ".mdd;Mode=ReadWrite;MR Init MDSC=\"\";MR Init MDSC Access=2;") {
//(Theres a little more on the connection string but its adding settings for the data collection similar to the last part: MR Init mdsc)
try{
conn.Open();
}
catch(exception er)
Console.Writelin(er.message)
}
Could you please help me understand this error, or what it means? I tried looking for some solutions but was not able to find a solution that is similar to my issue. Thank you for all your help!

Related

SSIS generate Programmatically: Project connection manager don't appear in the project connection list

I create Package from c#,and this code use to work with SQL server 2016 but now with Sql server 2014 its giving me issues :
I am adding the package to the project this way :
proj.PackageItems.Remove("HSTG_" + hstg_table.tablename + ".dtsx");
proj.PackageItems.Add(p, "HSTG_" + hstg_table.tablename + ".dtsx");
In the list of connection of the project i can see 3 Connection manager,
but in the the package i see 0 connection and then of course when i try to access one of them this way p.Connections["META"] it throw me error saying the connection cannot be found.
local_variable2
Instead of reading an existing ispac,I create one programmatically and it does work. But i still don't understand why it doesn't work in the previous way.
/* Previous way of doing it which work for me on a previous project but not
working anymore
*/
Project proj = Project.OpenProject(fixed_settings.Default.PROJECT_ISPAC);
// New way by creating the project on the fly
//Test creation project
Project proj = Project.CreateProject();
ConnectionManagerItem connectionManagerItem_meta = proj.ConnectionManagerItems.Add("OLEDB", "META.conmgr");
connectionManagerItem_meta.ConnectionManager.Name = "META";
connectionManagerItem_meta.ConnectionManager.ConnectionString = "Data Source=" + fixed_settings.Default.server_meta + ";Initial Catalog=" + fixed_settings.Default.server_meta + ";Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;";
ConnectionManagerItem connectionManagerItem_stg = proj.ConnectionManagerItems.Add("OLEDB", "STG.conmgr");
connectionManagerItem_stg.ConnectionManager.Name = "STG";
connectionManagerItem_stg.ConnectionManager.ConnectionString = "Data Source = " + fixed_settings.Default.server_stg + "; Initial Catalog = " + fixed_settings.Default.database_stg + "; Provider = SQLNCLI11.1; Integrated Security = SSPI; Auto Translate = False;";
ConnectionManagerItem connectionManagerItem_hstg = proj.ConnectionManagerItems.Add("OLEDB", "HSTG.conmgr");
connectionManagerItem_hstg.ConnectionManager.Name = "HSTG";
connectionManagerItem_hstg.ConnectionManager.ConnectionString = "Data Source=" + fixed_settings.Default.server_hstg + ";Initial Catalog=" + fixed_settings.Default.database_hstg + ";Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;";

connect mysql database in websever with c# application

i have a PHP website and MySQL database in same web sever. i wont to connect that MySQL database with c# application. my c# application in run another computer.
how can i connect that MySQL database ?
First make sure you have downloaded and installed the MySQL Connector/NET from the MySQL official website. In this article, I will use the Connector/NET version 6.1.
Then add MySql.Data namespace in Reference
Then you can use mysql in .net
now create a connection strin like this
class ConnectToMySql{
private MySqlConnection connection;
private void Initialize()
{
server = "localhost";
database = "connectcsharptomysql";
uid = "username";
password = "password";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
connection.Open();
}
}
Then
1 Create a MySQL command.
2 Assign a connection and a query to the command. This can be done using the constructor
3 using the Connection and the CommandText methods in the MySqlCommand class.
4 Create a MySqlDataReader object to read the selected records/data.
5 Execute the command.
6 Read the records and display them or store them in a list.
7 Close the data reader.
8 Close the connection.
I tooth this will help full for you . now you can start working on mysql in .NET
Best of luck
you firstly have to add mysql ref to your project:
plz right click on your project and go to the ref part in it
after that try to find mysl.conncetor and tick it
and refresh your project and firstly try to add using mysql.net.client
now you can use data base located in the mysql db from c# coding
be sure you have downloaded both mysql connector and installaton which is about 500 mb files.
class ConnectToMySql{
private MySqlConnection mscon;
private void Initialize()
{
server = "localhost";
database = "your own database name";
uid = " your username";
password = "your password";
string const;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
mscon= new MySqlConnection(const);
mscon.Open();
}
}

Sql connection not working in release

I've trouble with my program, everything works in debug mode but when i switch to release i'm getting InvalidOperationException:
_sqlcon.ConnectionString = "Data Source=" + Properties.Settings.Default.serverAdress + ";" + "Initial Catalog=" + Properties.Settings.Default.initialDB + "; User ID=" + Properties.Settings.Default.sqlID + "; Password=" + Properties.Settings.Default.sqlPass + ";" + "Connect Timeout=" + Properties.Settings.Default.timeOut + "; Asynchronous Processing = true;";
this is my connection string
Data Source=.\\SQLEXPRESS;Initial Catalog=visondb; User ID=sql; Password=test;Connect Timeout=30; Asynchronous Processing = true;
and i try simple to open connection using
try
{
// await dbConnAsync(_sqlcon);
_sqlcon.Open();
}
catch (SqlException ex)
{
MessageBoxResult result = System.Windows.MessageBox.Show(ex.ToString());
}
But in release it's not working and when i'm using exe i just get window "program stop responding".
Where to look for error ?
My best guess (based on the limited info here) would be that one of the values you're pulling from your release Properties.Settings.Default to generate your connection string is either
blank (an empty string), or
not what you expect it to be.
Thus creating an invalid connection string.
You should debug on the line before _sqlcon.Open(); and make sure the connection string is what you expect it to be when you're in release mode.

failed to copy database as backup file

Description:
I am trying to copy my sql database as backup file, once I login, and redirect to backup.aspx and I clicked backup button, it prompt me an error output
If I open directly backup.aspx I can copy the database without any error
I knew that the problem is my database is connect after I login, so it tell me it is being used by another process
What I want to ask is that anyway can solved this problem ?
I aim to disconnect from db on page load but I cant make it, it still prompt me the same error
Error message:
The process cannot access the file 'C:\Users\Roy\Desktop\backup
fyp\10-18-2011\WebSite5\App_Data\Database.mdf' because it is being
used by another process.
Code for button click:
string time1 = DateTime.Now.ToString("dd-MM-yyyy hh-mmtt");
Directory.CreateDirectory(#"C:/SME-Online/" + time1);
string destination = #"C:/SME-Online/" + time1;
string source = Server.MapPath(#"~/App_Data");
File.Copy(Path.Combine(source, "Database.mdf"), Path.Combine(destination, "Database.mdf"), true);
File.Copy(Path.Combine(source, "Database_log.LDF"), Path.Combine(destination, "Database_log.LDF"), true);
You should backup database first and then copy *.bak file as backup, here some code to get you started, of course you can alter that query or filename generation code to suit it to your needs, be sure to check Backup T-SQL statement help.
public void BackupDatabase()
{
/// this method should get opened connection
SqlConnection conn = GetOpenedDBConnectionFromSomewhere();
string dbName = conn.Database;
string backupFName = "c:\\MSSQLData\\Backup\\" + dbName + "_" + DateTime.Now.Ticks.ToString() + ".bak";
string sql = "BACKUP DATABASE [" + conn.Database + "] TO DISK = '" + backupFName + "'" +
"WITH NOFORMAT, INIT, NAME = 'Backup of DB:" + dbName + "', SKIP, NOREWIND, NOUNLOAD, STATS = 10;";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.ExecuteNonQuery();
}
}

Error while opening a connection to MS Access 2007 file: Cannot open the MS Office Access database engine workgroup information file

Cannot open the MS Office Access database engine workgroup information file - When I have code as posted.
What I am trying to do in my code is to create MS Access 2007 file and then set the user name and password to it from my program. What am I doing wrong here?
Error occurs here: objOleDbConnection.Open();
EDIT: I have made some changes, seems like it opens a connection but the command is incorrect.
Now problem is here:
objOleDbCommand.CommandText =
"ALTER USER " + storedAuth.UserName +
" PASSWORD [" + storedAuth.Password + "] []";
The entire code:
// Creating an object allowing me connecting to the database.
OleDbConnection objOleDbConnection = new OleDbConnection();
objOleDbConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + sfdNewFile.FileName + ";Persist Security Info=False";
// Creating command object.
OleDbCommand objOleDbCommand = new OleDbCommand();
objOleDbCommand.Connection = objOleDbConnection;
try
{
objOleDbConnection.Open();
objOleDbCommand.CommandText = "ALTER USER " +
storedAuth.UserName + " PASSWORD [" +
storedAuth.Password + "] []";
objOleDbCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
// Displaying any errors that
// might have occured.
MessageBox.Show("Error: " + ex.Message);
}
finally
{
objOleDbConnection.Close();
}
To change an Access DB password, you must it open in exclusive mode. Try adding this to your connection string ;Exclusive=1.
createMSFile.Create("Provider=Microsoft.ACE.OLEDB.12.0;Exclusive=1;Data Source=" +
sfdNewFile.FileName);
Well, the error you are getting suggests someone else is keeping the file open, which prevents the password change...
HelpNeeder, I think the problems you are experiencing should first be solved in your other question:
Error tells me I haven't close the connection, but haven't I?
Thanks!

Categories