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.
Related
I have a WPF application that accesses a database instance. I develop on my personal machine and the application will run on another machine and database instance. Is there a way or best practice for storing the instance name outside of the WPF application that I can read upon app startup? Currently I have comment and uncomment the database instance name as I move from machine to machine. I would like to have something stored on the local machine that I can just read when the application is launched.
Here is what I currently do:
try
{
if (sqlConn != null)
{
sqlConn.Close();
}
strConnection = "Data Source=" + strDBInstance + "; Initial Catalog=NBFoodPantry;Integrated Security=true; MultipleActiveResultSets = True";
//strConnection = "Data Source=CASS-LAPTOP\\SQLEXPRESS; Initial Catalog=NBFoodPantry;Integrated Security=true; MultipleActiveResultSets = True";
//strConnection = "Data Source=KENTS-WORK-PC\\CASS_SQLEXPRESS; Initial Catalog=NBFoodPantry;Integrated Security=true; MultipleActiveResultSets = True";
sqlConn = new SqlConnection(#strConnection);
try
{
sqlConn.Open();
}
catch (Exception ex)
{
string strMsg;
WriteErrorLog(strErrorLogFile, "ConnectToDatabase", ex.Message, false);
strMsg = "ConnectToDatabase: SQL Open failed. Please make sure that you database ";
strMsg += "instance and name are correct. Also make sure that the SQL engine is running.";
System.Windows.MessageBox.Show(strMsg);
}
}
catch (Exception ex)
{
string strMsg;
WriteErrorLog(strErrorLogFile, "ConnectToDatabase", ex.Message, false);
strMsg = " ConnectToDatabase: failed with error, " + ex.Message + ".";
System.Windows.MessageBox.Show(strMsg);
}
This is what application settings are designed to handle. Under the properties node in your project, open the settings. This has built in support for connection strings:
Then you can access the settings in your code by its name:
Properties.Settings.Default.ConnectionString
All of this is stored in your App.config, which you can modify on various machines to have different values.
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;";
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!
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();
}
}
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!