0

I have problem in database file which I made in Access, language C#,It works fine after installation in default location "C:/program files/default company name/project name/abc.mdb",
but when I install it in different drive (non-window drive) then my project doesn't find the database file.So,how can I remove this problem.

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by chetanbasuray
0

When you install your project you have to put the database files in that disk where you install the project. You cannot install part of it in one disc and other part in any other disc.
However, if you are having problems with installing the whole of the project in a different disc apart from c, the code you have used to attach the database into the code must be linking your project only to the c drive.
Maybe this code can help you out :)

SqlConnection sqlcon = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master;Data Source=.\\SQLEXPRESS");
            try
            {

                SqlDataAdapter dafetch = new SqlDataAdapter("select name from master.sys.databases sd where name = N'<database name>'", sqlcon);
                DataSet dsfetch = new DataSet();
                dafetch.Fill(dsfetch);

                if (dsfetch.Tables[0].Rows.Count == 0)
                {
                    sqlcon = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master;Data Source=.\\SQLEXPRESS");
                    string strQuery = "USE [master]" + " " + "CREATE DATABASE [<database name>] ON ( FILENAME = N'C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Data\\<database name>.mdf' )," + " " + "( FILENAME = N'C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Data\\<database name>_log.ldf' )" + "FOR ATTACH" + " " + "if exists (select name from master.sys.databases sd where name = N'<database name>' and SUSER_SNAME(sd.owner_sid) = SUSER_SNAME() ) EXEC [<database name>].dbo.sp_changedbowner @loginame=N'" + Environment.MachineName + "', @map=false";
                    sqlcon.Open();
                    SqlCommand sqlcmd = new SqlCommand(strQuery, sqlcon);
                    sqlcmd.ExecuteNonQuery();
                    sqlcon.Close();
                }


                SqlDataAdapter da1 = new SqlDataAdapter(" Select * from <any table name>", con);
                DataSet ds = new DataSet();
                da1.Fill(ds, "same table name");


            }
            catch
            {

            }
            finally
            {
                if (sqlcon.State != ConnectionState.Closed)
                    sqlcon.Close();
            }

Change the file formats and the connection strings before moving forward. :)
Hope I helped. :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.