I'm having trouble attaching a database DBName.mdf to a network SQL Server. The admin can manually attach the database but if I try, I get the following error message.
Database 'DBName' cannot be upgraded because it is read-only, has read-only files or the user does not have permissions to modify some of the files. Make the database or files writeable, and rerun recovery. (Microsoft SQL Server, Error: 3415)
Here is my code:
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
conn.ConnectionString = #"Server=" + SQLServerName + ";database=master;User ID=" + UserName + ";Pwd=" + Password; ;
try
{
conn.Open();
System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand("CREATE DATABASE DBName ON ( FILENAME = '" + #"C:\DBName.mdf" + "' ), ( FILENAME = '" + #"C:\DBName_log.ldf" + "' ) FOR ATTACH", conn);
com.ExecuteScalar();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
Here is the details of SQL Server
- Product - Microsoft SQL Server Express Edition (64-bit)
- Version - 11.0.2218.0
I can create a new database just fine but cannot attach an existing database. What am I missing here?
Any help will be appreciated.
I figured out what the problem was.
This link was helpful in figuring out the problem.
http://www.sqlservercentral.com/Forums/FindPost1367859.aspx
The user under which the SQL Service was running didn't have full access permissions to that folder. I gave the user full permission and everything worked fine.
I had the same error trying to attach via SQL server management studio.
Running SQL server Management studio as Administrator solved this problem for me.
Info gleaned from here http://www.nickyvv.com/2013/02/database-databasename-cannot-be.html.
I also faced the similar problems and I resolved it using both approaches listed above by Butters and pblack. To sum up they are:
Go to the system services and find SQL Server Database service.
Right click on the service and select properties
Go to the Log On tab
If the "Log On As" user from your service is a user account, make sure that user account has Full Control on that folder.
If the "Log On As" user from your service is "Network Service" or "Local System" those account should already have access, but go ahead and add them and give them Full Control.
Lastly, RUN SQL Server management studio using Administrative privileges
I also faced similar situation while using T-SQL Script but i choose another way through SSMS. Here are some easy steps that are very helpful to you to attach SQL MDF file through SQL Server Management Studio.
Go through Start Button->All Programs->SQL Server XXXX->SQL Server Management Studio
Login in it
Select Object Explorer enlisted databases
Then Right click on it and Select Attach database
Database Attachment Windows appears on the Screen then click on ADD button
Then you need to select MDF file which you want to attach and click OK
Confirm it and Click OK
Database is successfully attached. You can checkout in the Databases list.
Related
I'm attempting to run a stored procedure that processes an excel file for import from a Windows Service developed in C#. I can run the stored procedure directly in SQL Server Management Studio, but when running the stored procedure through the service I get the following error;
Ad hoc access to OLE DB provider 'Microsoft.ACE.OLEDB.12.0' has been denied. You must access this provider through a linked server.
Microsoft.ACE.OLEDB.12.0 is listed in Server Object\Linked Servers\Providers, additionally, Disallow adhoc access is unchecked in the Provider Options.
I don't know what else to try. Any thoughts? TIA
EDIT: added some service code
public ProcessFile(string fileName)
{
this.fileName = fileName;
fileToProcess = Properties.Settings.Default.importFilePath + "\\" + fileName;
log.Debug("fileToProcess: " + fileToProcess);
OpenExcelFile();
excelSheets = getSheetNames();
excelWorkSheet = (Excel.Worksheet)excelSheets.get_Item(2);
log.Debug("sheet Name: " + excelWorkSheet.Name);
connectToDb();
CloseExcelFile();
}
private void connectToDb()
{
int retVal = db.LoadSPVDataSheet(Properties.Settings.Default.importFilePath, this.fileName, excelWorkSheet.Name);
}
EDIT 2
I'm using SQL Server 2012 running on Windows Server 2012 R2
EDIT 3
I found the entry for AllowInProcess, and the Disallow Adhoc entry wasn't there. It was located #:
HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WOW6462Node\Microsoft\Microsoft SQL Server\MSAS11.<ServerName>\Providers\MSOLAP
I'm going add the entry and report back.
EDIT 4
The service code is now connecting and processing correctly. I found another place where theDisallowAdHocAccess registry key was missing.
HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WOW6462Node\Microsoft\Microsoft SQL Server\MSSQL11.<ServerName>\Providers\Microsoft.ACE.OLEDB.12.0
I added the key there, and it worked.
Check out this connect item: https://connect.microsoft.com/SQLServer/feedback/details/257494/smss-does-not-handle-the-provider-option-disallowadhocaccess-correctly
or read the detailes explained here: https://www.mssqltips.com/sqlservertip/4582/sql-server-ad-hoc-access-to-ole-db-provider-has-been-denied-error/
"We can disable "Disallow adhoc access" from the SSMS provider properties, but it removes the entry in the registry instead of changing the value to zero and we still get the ad hoc access to OLE DB provider has been denied error."
So you should manually add the missing entry in registry and enter 0 in it
I have a problem I need to solve, but not know how exactly. I have a WinForms application (C#) which connects to an online MySQL server - no problem there. In this application I have an option to make database backups (basically I dump this database to a local file on a computer).
I would like to locally "open" this backup on client's computer (to check some old data) - I don't want to make database restore on my server, because database must still be in use for other users. I want to make clean install of MySQL on a local computer and connect to it trough localhost (like I do for testing ), but I do not have physical access to that computer. I can send MySQL installer to my client, but how to go about automatically creating user with password and database from my dump file?
I know how to create a new database if it doesn't exist, but how to do it if it's clean install of MySQL server - no user and password yet.
string connStr = "server=localhost;user=root;port=3306;password=????;";
using (var conn = new MySqlConnection(connStr))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "CREATE DATABASE IF NOT EXISTS `hello`;";
cmd.ExecuteNonQuery();
}
Any help and direction is appreciated.
Regards!
I don't know whether I understand your problem. However,if you install a new Mysql,you can use root user through no database.
Anyway, one thing you need to know,Connect database must has user and password for mysql or sqlserver.
You may need to be more concise description of your problem.
We're having trouble getting our program to open a connection to our sql database
We're hosting the sql database installed with xampp on another computer and developing and testing this code on another. The computer holding the code has been tested and is able to connect to the host computer and can access the database as well as
We already have our code below but we're getting an inner exception "unknown username or bad password". We understand that Visual Studio has already connected to the sql database because under Server Explorer we can see database rows that were manually inputted before. Our troubleshooting was as follows:
We were thinking before that it might have been with the connection string but this seemed ok as it was copied from the connection string generated by visual studio.
We also checked with the sql database and no users or groups were overwriting the credentials that we were using.
We already turned off our firewalls
upon testing on the remote computer,our credentials (not root, custom made ones) can write manually on the database
Is there anything we missed?
Thank you
con = new SqlConnection(#"Server=192.168.0.128;Database=transaction_table;user id=new;database=transaction_table");
try
{
con.Open();
}
catch(Exception ex)
{
MessageBox.Show(ex.InnerException.Message + "\n\n" + ex.Message );
return;
}
Please find Updated Code
when u host outside domain password is must
con = new SqlConnection(#"Server=192.168.0.128;Database=transaction_table;user id=new;password:abc123");
try
{
con.Open();
}
catch(Exception ex)
{
MessageBox.Show(ex.InnerException.Message + "\n\n" + ex.Message );
return;
}
We opted to use MySQL.Data.dll instead. Problem was with the connector we used earlier
I have a C# application that, when a user clicks a button, will open SQL Server Management Studio query editor with a specified server and database connection. What I would like to do is be able to have this same functionality, but with an already running instance of SSMS (not start a new process).
My code so far:
if (IsProcessOpen("Ssms") == false)
{
Process ssms = new Process();
ssms.StartInfo.FileName = "C:\\Program Files (x86)\\Microsoft SQL Server\\110\\Tools\\Binn\\ManagementStudio\\Ssms.exe";
ssms.StartInfo.Arguments = "-S " + StaticVariables.Getserver + " -d " + StaticVariables.Getdatabase;
ssms.Start();
}
else
{
//In already running SSMS process, open connection to server and database with new query window.
}
As far my research goes, there is no API available to interact with an existing SSMS processs. (http://stackoverflow.com/questions/2334435/how-do-i-programmatically-open-a-new-tab-in-an-open-instance-of-sql-management-s)
You could try doing a hack involving SendKeys, but this would have a downside of not knowing the current state of the SSMS window (if it has any active windows open or anything)
If you could share the original requirement, we may look at some better alternatives.
This problem was driving me crazy, and I finally found that when I changed from logging in as "sa" to using Windows Authentication, the problem was resolved.
Sorry for the confusing name, I did not know of better one. There is a network with SQL 2005 server. I am provided with the windows account information to this computer so I can use remote desktop or map its drives. The SQL uses trusted connection. I am thinking whether I can connect remotely only to the SQL server? Thanks
This is based on my experience with SQL Server, but may not be entirely complete in scope.
SQL Server Manager will let you connect to the server instance with windows credentials, but to access the database tables you will need to create a login within SQL Server. Using SQL Server Manager, you can do this under the Security folder of the hierarchy of objects in the left column.
Next you will need to create a database (if one doesn't already exist that you are interested in using) and in that database's Security folder, create a user that corresponds to the login. When doing this you will also need to assign the permissions of the user (select, insert, delete, etc.).
Once that is complete, you can access the server anywhere on the network with the appropriate credentials, but you will need to make sure that there are no blocked ports, etc. from a network perspective.
In C# the statement to create a new connection looks like this:
SqlConnection conn = new SqlConnection("user id = username; " +
"password = secret; " +
"server = servername\sqlexpress; " +
"database = databasename; " +
"connection timeout = 30");
Hope that helps.