I have an installation package (Visual studio installer). During the installation process i attach a database to MS Sql server using SMO.
When the uninstallation process is started the dialog "The following applications ("SQL server(MSSQLServer)") should be closed before continuing the install" appears. The dialog has two options "Automatically close applications and attempt to restart them after setup is complete" and "Do not close applications(A Reboot may be required)". if i select first one option, i see the error "cannot connect at server". With the second option all works correctly.
In the unistall method of my custom action i use SqlCommand (command text: DROP DATABASE [baseName]) for deleting database.
Uninstall method code:
using(var con=new SqlConnection(_server.ConnectionContext.ConnectionString))
{
con.Open();
string sqlCommandText = string.Format("DROP DATABASE [{0}]", DATABASE_NAME);
var sqlCommand = new SqlCommand(sqlCommandText, con);
sqlCommand.ExecuteNonQuery();
}
How can i avoid the dialog "The following applications ..." and always use the second option? Or maybe there i some other way for deleting db?
Related
I am creating a POS system in Windows Forms (C#) in which I use a SQL Server database file (.mdf) to store items (completely offline). When I install the application on my computer, it works fine, but when I install it on my clients PC, an error happens:
(provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation"
I read somewhere that the problem is caused due to the fact that the connection string of the database of my client's PC is different. I tried to add the connection string dynamically on runtime but again it only worked on my computer.
Another reason that might be causing the problem is that I used 'server-based database' since local database option isn't available in Visual Studio 2017 for some reason.
Another solution I looked up stated that I should install SQL Server Express on my client's PC. That also failed (maybe I have to set it up in a way or something).
I also tried adding the database.mdf and database_log files in the setup folder.
Lastly I tried installing 3rd party installers (Advanced installers 15.8 and InstallShield Wizard in VS 2015) which also failed.
(I have provided the code for the connection of database taking place and the connection string)
public void ConnectToDB()
{
DBConnection = new SqlConnection(#"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename=C:\Users\SAIM NASSER\Desktop\app layer\data layer\Database1.mdf; Integrated Security = True");
DBConnection.Open();
ResultSet = new DataSet();
}
If I understand you correct, you want to use LocalDB
That means using Sql Server without installing a full sql server, but just the localdb part from sql server express.
For this to work you need to install the LocalDB Driver, which can be found here
https://www.microsoft.com/en-us/download/details.aspx?id=29062
You need only the ENU\x64\SqlLocalDB.MSI
This is the only thing you need to install in your clients computer, I believe it can also be installed silent, you have to research a bit for that.
And yes, you also should change the connection string on the clients computer, you need to alter it so it points to the MDF file on the clients computer, because that location will probably be different then on your computer
EDIT
To get the connection string working, you can try this
On the clients computer, create a text file and rename the extension to .udl
So for example you have a file test.udl
Now from explorer, double click it, this will open the datalink editor.
From here you can enter/choose different settings, and click on the test connection button.
Once you get a working connection, save it, and open this file with notepad.
Inside you will find the working connection string
Hope this helps
I am trying to connect to a database which is an .mdb file from my web application on asp.net. This error comes up 'Microsoft.ACE.OLEDB.12.0 Data Source =C:\Users\KIKI\Desktop\ASP.net labs\Erg8\Erg8\ebookstoredb.mdb' provider is not registered on the local machine. The thing is that on my connection string I don't use version 12 but 4. I tried installing Microsoft Access Database Engine 2010 Redistributable which gives me the option to select version 12 when I set it the connection from the "Server Explorer" window but the error is still there. Any ideas? I have spent hours looking for a solution...
Here is my connection string:
OleDbConnection constring = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\KIKI\\Desktop\\ASP.net labs\\Erg8\\Erg8\\ebookstoredb.mdb");
Also, I tested the connection when I set it and a message "Connection Succeeded" came up. The problem seems to be when I run the application on the browser.
I created SSIS package using integration service project in vs 2015.
My connection establishment is successful using Datasource. I can execute package using Execute Package Utility and Command line which is successful.
Please check below screens shots for the same.
I am facing problem while executing same package using c#. getting failed to execute package.
I have applied eventlistener, getting below error.
Please find below few screenshots for more information.
Code for reference:
Application app = new Application();
Package pkg = app.LoadPackage(#"C:\Project\Sample\Package1.dtsx", listener);
DTSExecResult results = pkg.Execute(null, null, listener, null, null);
In results object i got failure with given error.
As #Tab Alleman says you can run the package by calling a stored procedure sp_start_job from C# that starts SQL Agent JOB.
Here a piece of code that can be of help in this type of approach, after creating the SQL Agent JOB:
SqlConnection Conn = new SqlConnection(YOURCONNECTION);
SqlCommand ExecuteJob = new SqlCommand();
ExecuteJob.CommandType = CommandType.StoredProcedure;
ExecuteJob.CommandText = "msdb.dbo.sp_start_job";
ExecuteJob.Parameters.AddWithValue("#job_name", YOURJOBNAME")
ExecuteJob.Connection = Conn;
using (Conn)
{
Conn.Open();
using (ExecuteJob)
{
ExecuteJob.ExecuteNonQuery();
}
}
I hope this help.
Unfortunately your output does not show what the exact error message is. I have had problems in the past executing packages directly from C#.
One way around this is to create a SQL Agent job that executes the package, and then call a stored procedure from C# that starts the SQL Agent Job.
I have a project in visual studio 2010 with a database in sql 2008 R2..
I convert this project with crystal report . But when i want run this project this errors happened .
Error 13 Unable to copy file "XXX.mdf" to "bin\x86\Release\XX.mdf". The process cannot access the file 'XX.mdf' because it is being used by another process
Error 25 Unable to copy file "C:\madrase\madrase\entekhab_vahed\entekhab_vahed_log.ldf" to "bin\x86\Release\entekhab_vahed_log.ldf". The process cannot access the file 'C:\madrase\madrase\entekhab_vahed\entekhab_vahed_log.ldf' because it is being used by another process.
Error 12 Could not copy "C:\madrase\madrase\entekhab_vahed\entekhab_vahed.mdf" to "bin\x86\Release\entekhab_vahed.mdf". Exceeded retry count of 10. Failed. entekhab_vahed
i don't know how fix it .
Make sure your database is not open by Visual Studio. If so Disconnect it using Server Explorer in Visual Studio.
Make sure you close the client. There must be no active client using your project
If it still happens, try to run Visual Studio "As Administrator"
SQL Server .mdf file being used by another process
You probably already solved this by the date, but his is my answer for future searches:
Normally the error will tell you the PID of the process who is using the file, you can end the process, but is not recommended, maybe there is other things using him.
This is happening because you didn't close the connection after the end of your code.
Always use
try
{
//do your thing
}
catch (Exception e)
{
//close connection
}
Try to use
using(...context)
{
//do your thing
}
With this the connection will be closed at the end of stack.
You can implement an IDisposable too or add "Connect Timeout=10" to your connection string.
I want to make a database backup with this C# code:
connect = new SqlConnection(con);
connect.Open();
// Execute SQL
SqlCommand command = new SqlCommand
(
#"backup database MY_database to disk='d:\SQLBackup\wcBackUp1.bak' with init, stats=10",
connect
);
command.ExecuteNonQuery();
connect.Close();
When I run it, the following error message shows up:
Cannot open backup device 'd:\SQLBackup\wcBackUp1.bak'.
Operating system error 3(The system cannot find the path specified.).
If I change the path to d:\wcBackUp1.bak it seems to be ok, is without error, but the file does not exist, it was not generated.
If I run in SQL the command I have the message that it was 100% processed, but I didn`t see the file.
Could someone help me please?
Make sure the location "d:\SQLBackup\" exist in your database server and not on your client machine.
Two things to check.
The Sql Service may not have access to the d:\sqlbackup folder. Old Sql installs used to default to install the service with full access to the machine, but newer instances tighten that up. You could try changing the path to the directory where the default backups are stored.
Secondly, if the sql server is not on the same machine that you are running this program, then you must remember that the D: will be the D: on the sql server and not your local machine
Fundamentally, the Windows account that the SQL Server service runs under must have write permissions on the specified folder.
You can check what account this is by looking in SQL Server Configuration Manager, under SQL Server Services (look at the Log On As column)
Check what permissions that account actually has on the target folder using Explorer -> right click folder -> properties -> security -> advanced -> effective permissions.
One way to check that this is the problem is to change your code to back up to your SQL instance's backup folder, where the permissions are likely to be correct. For example
C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup