-2

I am sorry but I want a final answer about that. First I used SQL compact 3.5 for long time and it take from me long time to make tables and it is work good per one user application but now I have customer who want to run my soft on 8 computer by local network so I said ok then I try to share data file .sdf on server and use it but I get an error I don't remember it so I am searched on the internet and I saw that I must update to SQL compact 4 so update it and after that I get this Stupid error I can’t Believe that this data don't support this type of use and it take too long time to make tables and other things on SQL compact database and the my customer will not wait me so what is the Reason My SQL statement is this:

dt = New SqlCeConnection("Data Source=" & dpaa & "\MoveData.sdf;Encrypt Database=True;Password=123cdswdaas;File Mode=Read Write;Persist Security Info=False")
Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105

2 Answers2

1

I think that previous answer is slightly misleading - you are definitely able use SQL Server CE 4.0 with database file located on network share. In fact, I am using this functionality in one of my active projects right now. "File Mode=Exclusive" parameter is not necessary - exclusive mode is the only available mode in such case.

The major drawback of this approach is that only one client is able to read from or write to database file at a given time, due to exclusive locking of entire SDF file. But there a circumstances when it is not possible to have full-featured SQL server in your environment (domain policy etc.). In such case shared database file is the only solution available.

mik61
  • 56
  • 1
0

Accessing SQL Compact database .sdf file on a network share by multiple users is not supported. You should use SQL Server Express edition for this. There are also multiple posts on stackoverflow on this subject. The version 3.5 supports opening .sdf file exclusively from a network share but 4.0 does not. But no SQL CE version supports shared access of multiple network users to 1 .sdf file.

But upgrading your application to support both SQL Express and SQL Compact databases could be relatively easy task. It depends on how your application access data. For example using Entity Framework your queries could be generated depending on your actual database connection.

You can also use generic classes DbConnection, DbCommand etc. instead of SqlCeConnection, SqlCeCommand etc. - thus you can change used database type without having to maintain two separate versions of your project.


Download SQL Express 2014 with Tools SQLEXPRWT http://msdn.microsoft.com/cs-cz/evalcenter/dn434042.aspx. (You can eventually use also older versions e.g. 2008) SQL Server has a lot more SQL features and data types than SQL CE, so watch that you use only stuff that is compatible with SQL CE.

In your app.config have you can have two connection strings:

<add name="CompactDBConnection" connectionString="data source=|DataDirectory|\CE.sdf; password=xxxxxx; SSCE:Max Buffer Size=16384; temp file max size=256; ssce:autoshrink threshold=100; ssce:max database size=4091" providerName="System.Data.SqlServerCe.4.0" />
<add name="ExpressDBConnection" connectionString="Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername; Password=myPassword;" providerName="System.Data.SqlClient" />

You can chose then which one to use at the app startup

For creating DbConnection check this C# Retrieving correct DbConnection object by connection string.

Here is a small example of calling stored procedure using DbCommand instead of SqlCeCommand:

            DbConnection dbConn = GetConnection(connStr); 
            DbProviderFactory sqlF = DbProviderFactories.GetFactory(dbConn);
            using (DbCommand b2bcmd = sqlF.CreateCommand())
            {
                DbParameter msg = sqlF.CreateParameter();
                msg.ParameterName = "@errorMessage";
                msg.Direction = ParameterDirection.Output;
                msg.DbType = DbType.String;
                msg.Value = string.Empty;
                msg.Size = 2048;                                        

                b2bcmd.Connection = dbConn;
                b2bcmd.CommandType = CommandType.StoredProcedure;
                b2bcmd.CommandText = "PB2BImport";
                b2bcmd.Parameters.Add(msg);
                b2bcmd.ExecuteNonQuery();

                result = Convert.IsDBNull(msg.Value) ? "N/A" : (string)msg.Value;
            }

I presume you do not use Entity framework - but it would be much easier with it.

Community
  • 1
  • 1
Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105
  • so there is no way i must use sql express so last question if there is away to convert sql compact tables to sql express and one thing more – Eng-Mohammed AbdAllah Sep 20 '14 at 14:02
  • what is the version of sql express i can work 2008 or else and i want link to download it and if u have some thing that i can follow to make this mission complete as fast as i can at last thank u very much about your support and what u say is not the thing that i want to hear but no way i must do it i am waiting your reply to get start – Eng-Mohammed AbdAllah Sep 20 '14 at 14:10
  • ok i am installing it now but what about away to convert sql compact tables to sql express this is a easy way to do that or i will start from beginning to re Created all tables please still with me because it is the first time to do that and there is no time for mistake – Eng-Mohammed AbdAllah Sep 20 '14 at 16:45
  • See this blog, it is excelent for SQL CE problems http://erikej.blogspot.cz/2012/02/migrating-databases-between-sql-server.html – Vojtěch Dohnal Sep 20 '14 at 17:30
  • In case when users would use .sdf file exclusively (lock the whole database by 1 process) the 3.5 version used to support it, see this link https://connect.microsoft.com/SQLServer/feedback/details/646333/sql-ce-4-0-no-longer-supports-opening-files-on-a-network-share – Vojtěch Dohnal Sep 20 '14 at 17:40
  • "Version 3.5 SP2 would allow you to open a database exclusively that was located on a network drive or share" so i don't need to use sql express right for now ?? or what?? i am want final reason i am now did what u said to me and i am now trying to convert to sql express but i am still afraid about code that it will take along tim to convert because my program is very big if it what i understand so i can try with sql compact 3.5 sp2 or what?? – Eng-Mohammed AbdAllah Sep 20 '14 at 20:40
  • To use database exclusively, modify connection string. Try it yourself. – Vojtěch Dohnal Sep 21 '14 at 06:23
  • i am now using sql compact 4 whith this connection dt = New SqlCeConnection("Data Source=" & dpaa & "\MoveData.sdf;Encrypt Database=True;Password=123cdswdaas;File Mode=Read Write;Persist Security Info=False") and it get me the error i don't think it is from connection string – Eng-Mohammed AbdAllah Sep 21 '14 at 06:28
  • and i follow that blog spot that you send to me but i don't under stand the last step can u help me in it "You can now open the generated script in Management Studio and execute it against a SQL Server database, or run it with sqlcmd as described above." – Eng-Mohammed AbdAllah Sep 21 '14 at 06:34
  • The stuff with SQL CE database on a network share is quite obscure and surely it is not a good practice, as you can see here http://social.msdn.microsoft.com/Forums/sqlserver/en-US/d983dbdf-6be9-4f11-b76f-5a0ada198363/sqlce-35-sp2-on-network-share?forum=sqlce. If you want to experiment with it, you can use `File Mode=Exclusive;` in your connection string. But this is a Q&A site - not tutoring site and I already feel that this answer is way too broad. – Vojtěch Dohnal Sep 21 '14 at 10:14