I am new to SQL Server and C#. I have Computer1, Windows 7. I use Windows Forms and SQL Server 2008 R2 Express.
I developed a software which will be running later in other Windows computers and those computers will only have SQL Server database engine (no management studio installed).
Instead of attaching the database manually I want to attach it programmatically when the programs starts.
I created a folder (D:\MyFolder) and I placed the mydb.mdf and mydb.ldf files in it (re-created database).
Then I used the following code to attach mydb database to the server and it was successful done and I can read from the database. I have to give MyFolder Full control permission or otherwise the program will throws database login failure.
Also I have Computer2 which has exactly same windows, same SQL Server version as in computer1. I copied both mdf and ldf files from computer1 and pasted them in D:\MyFolder in computer2 and I run the program the database attached successfully.
Until now everything works fine and both computers attached the database fine.
Now I go to computer2, to D:\MyFolder and right click on the database files and delete them and I go to computer1 and copy the database files and paste them into computer2 (in same directory D:\MyFolder) and when i run the program it says:
Cannot open database "mydb" requested by login. the login failed. Login failed for user 'Computer name/ username'
Even after I gave permission for everyone on MyFolder, it still throws the same error.
I searched all day and I checked these answers here, here, here, here and here but nothing helped. Anyone knows what can I do to fix it? any suggestion will be helpful.
private void Attach_DB_Click(object sender, EventArgs e)
{
try
{
// This is to give permissions to folder
string folderPath = @"D:\MyFolder";
var directoryInfo = new DirectoryInfo(folderPath);
var directorySecurity = directoryInfo.GetAccessControl();
var currentUserIdentity = WindowsIdentity.GetCurrent();
var fileSystemRule = new FileSystemAccessRule(currentUserIdentity.Name, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow);
directorySecurity.AddAccessRule(fileSystemRule);
directoryInfo.SetAccessControl(directorySecurity);
// This is to connect to the attached database
SqlConnection MyConnection = new SqlConnection(@"Data Source=localhost ; AttachDbFilename=D:\MyFolder\mydb.mdf; Initial Catalog =mydb; Integrated Security=true");
SqlCommand MyCommand = new SqlCommand();
DataTable DataTable = new DataTable();
SqlDataAdapter Sql_Data_Adapter = new SqlDataAdapter();
DataTable.Rows.Clear();
MyConnection.Open();
MyCommand.CommandText = "SELECT * FROM Table_Customers ";
MyCommand.Connection = MyConnection;
Sql_Data_Adapter.SelectCommand = MyCommand;
Sql_Data_Adapter.Fill(DataTable);
dataGridView1.DataSource = DataTable;
MyCommand.Parameters.Clear();
Sql_Data_Adapter.Dispose();
MyConnection.Close();
}
catch (System.Exception excep)
{
MessageBox.Show(excep.Message);
}
}