0

Question background:

I have developed a WinForm app and am now at the stage of wishing to publish it.

I have an SQL server database that I have added into a folder of the solution explorer as I want the database to be deployed within the app, as shown;

enter image description here

Within this DataSet I have a number of stored procedures added.

The issue:

When I debug the app, supplying the following connection string in the code snippet the DataGridView is not populated.

string connectionString = @"Data Source=.;Integrated Security=True;AttachDbFilename=|DataDirectory|\testDB.mdf";**

public override DataSet FillDataGrid()
{

    SqlConnection connection = new SqlConnection(connectionString);
    SqlDataAdapter dataadapter = new SqlDataAdapter("spListAll", connection);
    DataSet ds = new DataSet();
    connection.Open();
    dataadapter.Fill(ds, "results");
    connection.Close();

    int size = ds.Tables[0].Rows.Count;

    return ds;
}

If I supply the connection string from a Database stored locally on my machine with the connection string as follows, it works perfectly populating the grid.

string connectionString = @"Data Source=DAVE-PC\FINAWARE; Initial Catalog=testDB; Integrated Security=SSPI;";

Can anyone tell me how I should be correctly setting this up so I can use the testDB.mdf in the solution explorer folder?

user1352057
  • 3,162
  • 9
  • 51
  • 117
  • do you use clickonce for deployment/publishing? – giammin Jan 07 '14 at 15:14
  • @giammin Its been a long time since I last published an app and the last one I did, the database was excluded and hosted separately on a server so multiple users could access it. At the moment I just want to access the database from within the folder of the solution explorer. – user1352057 Jan 07 '14 at 15:18
  • do you want to use in development the database file in the solution with the same connection string used when you deploy the app? – giammin Jan 07 '14 at 15:23
  • @giammin Yes, I want to ideally deploy this all together with the database as it is now as only one user needs to access it. So yes the connection string would be the same. – user1352057 Jan 07 '14 at 15:36
  • do it work when deployed with your code? – giammin Jan 07 '14 at 15:37
  • No, currently in development it is not working. The datagridview is not being populated from the testDB.mdf in the solution explorer. – user1352057 Jan 07 '14 at 15:39
  • do you use clickonce for deployment? – giammin Jan 07 '14 at 15:41
  • @giammin I'm not 100% as its been a while since I last used it. Will it work once deployed? I cant understand why it won't work now? – user1352057 Jan 07 '14 at 16:06
  • because it depends how it is deployed to make it work for clients – giammin Jan 07 '14 at 16:15
  • @giammin I want the database deployed with the code. I want it all kept together. The application will only be used by a single user, the database wont be shared. – user1352057 Jan 07 '14 at 18:53
  • does it give any errors? if not can you please check and list properties of `connection` object to be sure it connects to the right database. – Erdogan Kurtur Jan 08 '14 at 11:17

2 Answers2

0

Apparently your DataSource is not valid.

Others had the same problem as you and used .\SQLEXPRESS as a DataSource.

Seems you also need to add User Instance = true in your connection string more documentation

Here for a similar problem How do I connect to an MDF database file?

Community
  • 1
  • 1
Irwene
  • 2,807
  • 23
  • 48
0

Of course I am assuming that, whatever the machine from which you run the application, SQL Server was previously installed. (Or at least you have installed the LOCALDB version of Sql Express 2012)

Said that, you should keep in mind that the DataDirectory substitution string is resolved in different ways depending where your app is running.

In a debug session under Visual Studio (Winforms app) the DataDirectory resolves to the PROJECTFOLDER\BIN\DEBUG and the database file should be there (To ensure this you could set the property of the MDF file Copy To The Output Directory to Copy Always or If Newer)

In a production machine, the DataDirectory (always for winforms app), is located in the same folder where you install your application and this is an obvious problem for a database app if you install your application in a special read only folder like C:\program files (x86)

Luckily, it is possible to change change the location of the DataDirectory substitution string. Before trying to use any data access related code, add these lines

string dataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
dataPath = Path.Combiner(dataPath, "MyAppDataFolder");
AppDomain.CurrentDomain.SetData("DataDirectory", dataPath);

Usually the installation package should create the MyAppDataFolder in the CommonApplicationData folder and copy there the MDF file. This is recommended because you have there read/write permissions and other users of the same machine could use your application

As a side note, I see another problem in how you call the stored procedure (though I can't explain why it works if you change the connectionstring). You should tell the adapter that you are using a stored procedure setting the

SqlDataAdapter dataadapter = new SqlDataAdapter("spListAll", connection);
dataadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thank you. As a bit more background to this I'll add the following: 1. During the build and testing of the application, the database is run and stored from SQL Server 2008 - not an express edition - this also contains all of the stored procedures. 2. I thought it would be a simple case of adding the testDB.mdf to the solution explorer and calling it from there, am I correct in saying that I need to add this testDB.mdf to the DEBUG folder during development? 3. So when it comes time to publish my app, I need to add a folder, copy the testDB.mdf there and then publish it? – user1352057 Jan 09 '14 at 10:37
  • In debug, if you want to use a static database MDF inside the main install of Sql Server, then you don't use the DataDirectory approach and you don't need to have the MDF in the DEBUG folder. The code that sets the DataDirectory path would be ignored because you use the database inside the SQL Server or because you apply the conditional `#if !DEBUG` directive. Your connection string will be the one with the Initial Catalog key – Steve Jan 09 '14 at 11:03
  • In deployment, you could use the DataDirectory substitution string to attach your db file to whatever sql is present. However the real difficulty is to know the local DataSource name (`Data Source=.; or Data Source=.\SQLEXPRESS;` ? Is your setup package able to find this or you should ask to your user?). However, once you have a separate app.config with a connection string ready for deployment, there should be no problem. By the way take a look at LOCALDB in my link above, your scenario seems to be the perfect use case for it. – Steve Jan 09 '14 at 11:03
  • again many thanks for your answers. Does localDB support stored procedures? – user1352057 Jan 09 '14 at 12:19
  • Yes, that's a big advantage. In every aspect it is like working with Sql Server Express, but it doesn't require a service installed and doesn't support (outofthebox) data sharing features – Steve Jan 09 '14 at 12:26