2

I have a login form in my project and I write below code to attach my database( there is in d:\ ) when this login form loads:

try
{ 
    SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=master;Integrated Security=True");
    con.Open();

    SqlDataAdapter da = new SqlDataAdapter("select name from sys.databases", con);
    DataTable dt = new DataTable();
    da.Fill(dt);

    string[] array = dt
        .AsEnumerable()
        .Select(row => row.Field<string>("Name"))
        .ToArray();

    if (!array.Contains("cstmrDB", StringComparer.OrdinalIgnoreCase))
    {
        SqlCommand cmd = new SqlCommand("sp_attach_db");
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@dbname", "Apdb");
        cmd.Parameters.AddWithValue("@filename1", @"d:\Apdb.mdf");
        cmd.ExecuteNonQuery();

    }
}
catch (exception ex) 
{ 
    messagebox.show(ex.message); 
}

It works fine in my laptop. I publish my project (using c# publish) and install SQL Server and my project and copy my database in d:\ in another PC. but when i run my project, the database won't attach! I don't know why this problem occurrs... but I think maybe the reason is that I don't write any code to define *.ldf file (but i put both mdf and ldf file in d:\ )

ERROR: A network-related or instance-specific error occurred while establishing a connection to SQL Server.

The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.

(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

12
  • Do you get any error into catch? Commented Feb 29, 2016 at 12:54
  • Well, yeah, basic debugging skills are really hard to get. Start with catching the exception and looking at the SQL Server log to see what happens. I am quite sure the call does not magically just do nothing. Commented Feb 29, 2016 at 12:59
  • @DynamicVariable question updated Commented Feb 29, 2016 at 17:58
  • 1
    @TomTom Yes, "." connects you to the local instance, you usually see it with SQL Server express in the form of: .\MYINSTANCE. Commented Feb 29, 2016 at 18:03
  • 1
    Please refrain from putting tags directly in the title. For more information, see Should questions include tags in the title Commented Feb 29, 2016 at 18:04

1 Answer 1

3

I would venture to guess this is a permissions issue, usually a freshly installed SQL Server is running a NETWORK_SERVICE which is a pretty low privilege account. It probably does not have access to the root of your G drive (or any part of it for that matter).

You can test this very quickly by changing the servie to run as LocalSystem. Once you've confirmed this as an issue I would recommend changing the log on use back to NETWORK_SERVICE and then applying the appropriate permissions to the folder/files that need it.

Sign up to request clarification or add additional context in comments.

10 Comments

how can i "changing the log on use back to NETWORK_SERVICE and then applying the appropriate permissions to the folder/files that need it"
See this link about configuring a service to run as NETWORK SERVICE: technet.microsoft.com/en-us/library/cc755249.aspx. Once you've got it running as NETWORK SERVICE again then you can simply add that user to the file/folder permissions as necessary (I usually add it to the ACL as NETWORK_SERVICE).
how can i do it programmatically?!
Do what programmatically?
"changing the log on use back to NETWORK_SERVICE and then applying the appropriate permissions to the folder/files that need it" i dont want user do this steps...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.