Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi Friends,

I have created windows application, in that i am using a database part so i need to provide a option to create .MDF file in selected path(user define path) using scripts in c# coding. Here i am facing the problem is 'unable to create the .MDF file in c drive', if i create .MDF file in some other drive (D,E,....) means i am unable to open the file and throwing a error as "Access is Denied". I have given user full permission for the path where the .MDF file is created.

Code for Creating .MDF file is

C#
string conxString = "Data Source=" + ServerName + ";Initial Catalog=master; Integrated Security=True";

                using (SqlConnection DBCon = new SqlConnection(conxString))
                {
                    DBCon.Open();

                    string str = "CREATE DATABASE " + FileName + " ON PRIMARY " +
                    "(NAME = N" + FileName + " ," +
                    "FILENAME = N'" + FileFullPath + ".mdf', " +
                    "SIZE = 4MB, MAXSIZE = UNLIMITED, FILEGROWTH = 10%) " +
                    "LOG ON (NAME = N" + FileName + "_Log, " +
                    "FILENAME = N'" + FileFullPath + "Log.ldf'," +
                    "SIZE = 2MB, MAXSIZE = 2048GB, FILEGROWTH = 10%)";

                    SqlCommand Cmd = new SqlCommand(str, DBCon);
                    Cmd.ExecuteNonQuery();
                    DBCon.Close();
}


This path and .MDF file name i have stored in app.config (|DataDirectory|\DataBase=.MDF File path) 


<userSettings>
    <DAL.Properties.Settings>
      <setting name="InformerConnectionString" serializeAs="String">
        <value>Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DataBase\Informer.mdf;Integrated Security=SSPI;User Instance=True</value>
      </setting>
    </DAL.Properties.Settings>
  </userSettings>

from here to i am getting the connection string



Thanks Maciej for your Replay,


So can any one suggest me to fix the issue

Thanks in Advance.
Posted
Updated 15-May-13 0:05am
v5
Comments
Maciej Los 9-May-13 15:19pm    
Please, be more specific and provide more details about mdf file creation (code). Use "Improve question" widget.
Mohan Gopi 10-May-13 0:17am    
Thanks Maciej for your Replay, I have added Create .MDF coding in above. This code is working fine in D,E,... Drive's(Only for Creating), after Creating .MDF, when i access this MDF file it's throwing 'Error: An attempt to attach an auto-named Database for File' but i have given user full permission for the path where the .MDF file is created. I am trying to create this .MDF file in C drive it's throwing 'Access is Denied' for Creatng .MDF file.
Maciej Los 10-May-13 1:58am    
And where is the code to access to the database after creation?
Mohan Gopi 15-May-13 6:07am    
Thanks Maciej for your Replay, I have added access .MDF File coding in above.

1 solution

Hi Mohan,

I think you have to create the .mdf file in the application path (where your windows app .exe file present). This could most probably solve your issues.

Try it, if any other queries please let me know.

Best of luck...
 
Share this answer
 
Comments
Mohan Gopi 15-May-13 5:58am    
Thanks Mohammed for your Replay, I have Create .MDF File in user selected path it should be any where in the system its working fine but while accessing this .MDF file(Already created .MDF file) that time i will facing the problem.
Mohammed Hameed 15-May-13 6:02am    
Can you once create in the application path and after that verify whether it works fine or not.
Mohammed Hameed 15-May-13 6:04am    
Did you copied this .mdf file in any other system and tried?
If not tried, try it once; Who knows may be it's not working on your system but it may work on other system.
Mohan Gopi 15-May-13 6:08am    
Thanks Mohammed for your Replay, its working on other systems
Mohammed Hameed 15-May-13 6:15am    
Then it means there is no issue with the code which you have written. There is some issue with your system. May be the .mdf file is not compatible with the software you have on your system.Can you provide the error/exception details to understand the issue?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900