How to include .sdf database file when packaging my application - vb.net

I was able to create an application in VB.net that talks to a SQL Server CE database.
Now i want to deploy this application using the publish feature with clickonce.
An issue im having tho is when I install the application on another computer it keeps looking for my sql server on my development pc.
Ive added the .sdf when i package my solution but its still an issue.
How do I change my connection string to connect to the .sdf file that I included in the package?
this is my current connection string which looks at the sql server:
connectionString = " Data Source=CHRIS-PC\SQLEXPRESS;Initial Catalog=ce_db;Integrated Security=True"
Thanks !!

connectionString = "Data Source=localhost\SQLEXPRESS;Initial Catalog=ce_db;Integrated Security=True"
or perhaps
connectionString = "Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\ce_db.sdf;Persist Security Info=False;";
this connection string would work if you had the database in the same directory as the executable using it.
essencially the data source is either the database server that you are connecting to, or the database file that you are connecting to.
If you are using a file then you have only one catalog. and if you are using a server then you will want to specify a specific catalog using the Initial catalog attribute of the connection string.

Related

What other ways to put connection string in vb web form via asp.net not to include the Drive C or D my db is ms access 2007

Below is my connection string code in vb
the reason is that when I deploy this to web hosting there is no Drive D: or C: in website
anyone can help is really much appreciated, this is my first time to make a web program.
Thank you very much in advance.
Dim constr As String = "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=D:\Desktop\Web1\Database\SignUp.accdb"
Dim con As OleDbConnection = New OleDbConnection(constr)
Dim cmd As OleDbCommand = New OleDbCommand()
You almost certainly shouldn't be using a Access database in the first place. If your site is hosted then they would also provide database facilities, which probably means MySQL or maybe SQL Server. You ought to use one of those.
That said, if you're using Web Forms then you should be able to add an App_Data folder in the Solution Explorer and place the data file there, then specify the folder path using "|DataDirectory|", like this.
Dim constr As String = "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\SignUp.accdb"
EDIT: It just occurred to me that you're almost certainly not going to be able to use an ACCDB file regardless. If you were using an MDB file then you could use the Jet OLE DB provider, which is built into Windows. As you're using an ACCDB file, you need to use the ACE OLE DB provider, which is not part of Windows. That means that your host would have to either install Microsoft Office on their web server or else explicitly install the ACE provider, which Microsoft specify is not intended for servers. It can be installed but I very much doubt that any web host is going to do so.

Should I hardcode the path for the database while deploying?

I created a software using Visual Basic 2010 and SSMS 2012 and I wish to deploy it. The question here is how should I go about doing it? I know I can create an executable file .exe which is already a good thing about VS2010 and I also think that installing SSMS-2012 as well before installing the main setup. Also, the script of the database would be generated and then run on the client's computer which enables the database to be attached. However, the question here is that should I hard-code the directory of the database files (.mdf) in Visual Basic
Currently, my connection string is such:
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
When SSMS 2012 gets installed on any computer, the server (which is based on the computer's name) which could differ in all computers. So will this work? Or are there any other options
P.S i'm a beginner, so please go easy on me :)
if your problem only lies on the computer's name on a different pc, then try to concatenate this on your myServerAddress variable. Use
My.Computer.Name
to get the target computer's name.
This is also a good link I guess, it is an Access database though but the same concept is used (the use of DataDirectory).
I'd not suggest to hardcode the connection string as this ties your application to a very specific deployment scenario. What if a customer wants to use his or her existing SQL Server instance? What if the customer creates a named instance for the SQL Server Express installation (e.g. SERVERNAME\SQLEXPRESS). Sooner or later you'd have to change your code to reflect the different situations and you'd do the same thing that you can do already now.
In .NET, you can store a connection string in an application configuration file. It is named as the Exe file but has a .config extension (e.g. MyExeName.exe.config). In Visual Studio, all you need to do is to add a file to your project ("Application Configuration File", app.config) if it doesn't exist yet. Upon build, it will be renamed to the Exe-name plus .config. See this link for detailed information.
For your scenario, you'd have a configuration file like this. It references the local computer by using "." as the server name. So you don't have to change it after deployment if this is your default scenario:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyConnection"
connectionString="Data Source=.;Initial Catalog=myDatabase;Integrated Security=SSPI"/>
</connectionStrings>
</configuration>
In your code, you can access the connection string using the ConfigurationManager class (you might need to add a reference to the System.Configuration assembly). You can use the acquired connection string when you create the connection:
Dim connStr = ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString
Using conn = new SqlConnection(connStr)
conn.Open()
' Use the connection
End Using
To summarize: you'd add a default configuration file with a connection string that works for any server name to your deployment. If the customer wants to have the database on a different instance or another server, you simply change the connection string - but you don't have to if the customer is fine with the default configuration. As you see, it is not much more effort than to hard code it, but it will make your deployments more flexible.
I work for a large company delivering enterprise solutions and the way we handle this is very easy. All you need is a small configuration utility (could even be your installer wizard) where you request a user credentials (which presumably have enough access right to the SQL Server to create/alter databases and objects), and for the server name.
All you need to do after that is to establish a connection to the server, create the database and run the table and/or views creation scripts.
I see you mentioned attaching an MDF file but I cannot see a reason why you would like to do that. If you have system data you still can insert it using the same script or an additional one.

Entity Exception : the underlying provider failed to open

iv'e got a wcf service host hosting a service on IIS7,
iv'e added a database to it's App_Data folder ,
the service is referenced to a DAL project
which holds an Entity Framework model generated from my DB ( The DB from the WCF Service Host )
i keep getting the above entity exception with this inner message :
{"An attempt to attach an auto-named database for file C:\\Users\\eranot65\\Documents\\Visual Studio 2010\\Projects\\CustomsManager\\WcfManagerServiceHost\\App_Data\\CustomesDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."}
iv'e copied the connection string from DAL/app.config to WcfManagerServiceHost/Web.config
add name="CustomesDBEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\eranot65\Documents\Visual Studio 2010\Projects\CustomsManager\WcfManagerServiceHost\App_Data\CustomesDB.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True'" providerName="System.Data.EntityClient"
this happens when i try to use my data source entity model:
public List<Employee> GetEmployees()
{
List<Employee> employees = null;
using (CustomesDBEntities entites = new CustomesDBEntities())
{
employees = entites.Employees.ToList<Employee>();
}
return employees;
}
it doesn't seem as if the DB is in use some where else ,
(1) how can i check if some other process is holding a handle to my DB ?
(2) in ideas this happens ?
I would consider checking one of two things:
Create a connection to your SQL Express, either in VS server explorer, or by using the SQL management studio, and verify you do not already have a database by that name attached to your server.
Move your project from it's current location to somewhere on the disk which is not user-specific (meaning not on the desktop, documents etc..), for example - c:\temp, c:\projects... The reason for that is that you are running a web application, and in case you run it in IIS, the identity of the worker process is a special identity other than yours which might not have permissions to access the database file since it is located in a private folder of your user
Most likely the problem is that you are opening the database with Visual Studio and your application at the same time. The connection string explicitely configures AttachDbFilename=... AttachDBFilename spins up a user instance of SQL Express attached to a specific DB Filename for single user mode. In single user mode, only one application can open the MDF at a time.
well the answer is always simpler then you would think
all i ended up doing is changing to automatically generated connection string
generated by the EntityFramework model , to a connection string to locate the DB in my App_Data folder
my original connection string :
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='Data
Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\eranot65\Documents\Visual Studio 2010\Projects\CustomsManager\WcfManagerServiceHost\App_Data\CustomesDB.mdf"
;Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True'" providerName="System.Data.EntityClient"
my edited connection string :
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;
provider connection string='Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CustomesDB.mdf;Integrated Security=True;;User Instance=True;MultipleActiveResultSets=True'"

How can i test my TSQL syntax?

Quick question: How do I get some kind of database to use to test my SQL syntax and create basic data.
I have Sqlite Code which I'll soon put on a server. I have SQL Server 2008 installed with visual studio 2010. I tried connecting to the database and had no luck.
I also tried using an .mdf file instead thinking it's a file and I won't have connectivity issues. Wrong, I still couldn't connect and I used this site to help me (i'm aware its 2005)
In that case I used:
var conn = new SqlConnection(#"Server=.\SQLExpress;AttachDbFilename=C:\dev\src\test\SQL_DB_VS_Test\test.mdf;Database=dbo;Trusted_Connection=Yes;");
exception
Unable to open the physical file "C:\dev\src\test\SQL_DB_VS_Test\test.mdf".
Operating system error 5: "5(Access is denied.)".
Cannot attach the file 'C:\dev\src\test\SQL_DB_VS_Test\test.mdf' as database 'dbo'.
With trusted = no I get Login failed for user ''. (What user am I suppose to set?). I created the .mdf with Visual Studio somehow.
What if you try this connection string:
var conn = new SqlConnection(#"Server=.\SQLExpress;
AttachDbFilename=C:\dev\src\test\SQL_DB_VS_Test\test.mdf;
Database=test;Integrated Security=SSPI;");
I don't think it's a good idea to call your database "dbo" (that's a SQL Server keyword - I wouldn't use it for my own purposes!), and also I believe you need to use Integrated Security=SSPI; to define Windows authentication - Trusted_Connection is not used for SQL Server connection strings, AFAIK.
Have you tried using SSMS to access your local instance? It's helpful for getting connected and getting everything setup. Also, I think the default install of Sql Express with VS only support trusted connections.
After creating the mdf file with visual studios right click the mdf and select properties. In it you'll see a row called Connection String. Copy/paste it into your app and it should connect. The key part is User Instance=True

Mysql migration tool, source parameter string | connection string for sql server

I always got failed to "fetching of list failed error".
This is my connection string in ASP.NET
"Data Source=maywood\XSQLSERVER;Initial Catalog=maywood_test;Integrated Security=SSPI"
What exactly should I input at MySQL migration tool for source parameter string
FYI,'maywood' is my computer name and I am using SQL Server 2000.
You have integrated Integrated Security=SSPI set -- perhaps you should try setting the UID and Password manually to values that you know are correct.
Data Source=maywood\XSQLSERVER;Initial Catalog=maywood_test;
User ID=myUsername;Password=myPassword;
Integrated Security uses the credentials from the Windows system that you're using to log in -- these credentials might not be valid for the database you're trying to access.