I have a WPF C# application and an SQL Server Compact 4.0 database. Initially I had my Data Source pointing to the location of the database like this:
public static SqlCeConnection sqlCeConnection = new SqlCeConnection(#"C:\VSP Road Maintenance Solution\VSPApp_Mono\VSPApp\VSPCompactDatabasse.sdf");
This worked great on my local machine but when I wanted to package the application (ClickOnce), it didn't work on the client's machine. After going through MSDN documentation on creating an ms sql server compact database and deploying it and other related similar issues such as http://social.msdn.microsoft.com/Forums/sqlserver/en-US/dc31ea59-5718-49b6-9f1f-7039da425296/where-is-datadirectory-?forum=sqlce and http://msdn.microsoft.com/en-us/library/37z40s1c(v=vs.110).aspx
I tried to access the AppDomain via C# like (in App.xaml.cs):
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Process p = Process.GetCurrentProcess(); // Pick desired process
p.PriorityClass = ProcessPriorityClass.RealTime; // Assign priority
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.SetData("DataDirectory", #"C:\VSP Road Maintenance Solution\VSPApp_Mono\VSPApp");
}
And now to create a connection to the database I just:
public static string dbFileName = "VspCompactDatabase.sdf";
SqlCeConnection sqlCeConnection = new SqlCeConnection(#"Data Source=|DataDirectory|\" +dbFileName);
This works on my local machine too, but when I create the ClickOnce package. The application does not seem to find the database. All the prerequisite software gets installed. Why is it that when the application is deployed on a different machine, the seems to be no connection with the database?
Related
I've created a C# WPF application which interacts a SQLite database. I want to create installation file so as to be able to install the app on another computer. Does anyone know how to deploy C# WPF app with SQLite database, I've created a setup file by using ClickOnce, but when I run the app and do something with the app which requires fetching data from database, the shuts down.
I don't think it's all about making the installation file. I think, the application crashes because the software can't find the database. If you develop your software in such a way that, it creates the database (if it doesn't exit) on startup, the way you create the installation file won't be a problem. Here is what I mean:
//use an environment generated file path.
public string _dataBasePath = $#"{ System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData)}\databaseName.db";
Using an object relational mapper like NHibernate. You can override OnStartUp and create your database:
// In the App.xaml.cs class
private override void OnStartup(StartupEventArgs e) {
base.OnStartup(e);
BuildSchemaAction(config);
}
private void BuildSchemaAction(Configuration config) {
new SchemaExport(config).Create(false, !File.Exists(_dataBasePath));
}
protected Configuration config = new Configuration();
The database file (since it is SQLite) will not exist, so it will be created. Subsequently, it will not be created.
The _dataBasePath field will be the path in the connection string, in order to access the database in the path it was created.
x.ConnectionString = $"Data Source={_dataBasePath};version=3";
My application does basic CRUD operations over an external .mdf file. The database file is created separately in SSMS. On my PC, everything works perfectly fine. When I install it on someone else's computer, it refuses to connect to the database. The other PC also has the same database at the exact same location.
The connection string I am using is (in app.config):
conString=Data Source =.\sqlexpress; Initial Catalog = dbName;
Integrated Security = True; Pooling = False
In my code:
dbConnection = new SqlConnection(Settings.Default.conString);
The other PC has SQL Server installed as well. Tried going through almost all the results but almost all of them suggest either
Adding the existing database with the project. I can't seem to do that because when I try that I get an error:
permission denied
I can't rewrite the whole code with a service based database pre-attached to it.
I created a connection to a SQL database, but when I close it, the process sqlservr.exe keeps running even after closing the application. I've tried to use Dispose too, but had the same problem. sqlservr.exe is meant to keep running(It wasn't before starting the application)? Is there any way of killing it?
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string conexao = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=C:\\Users\\SMITH\\Documents\\C#\\WindowsFormsApplication3\\WindowsFormsApplication3\\Database1.mdf;Integrated Security=True";
SqlConnection conn = new SqlConnection(conexao);
SqlCommand comando = new SqlCommand("SELECT COUNT(*) FROM Usuarios WHERE NomeUser = #user and SenhaUser = #senha", conn);
comando.Parameters.Add("#user", SqlDbType.VarChar).Value = textBox1.Text;
comando.Parameters.Add("#senha", SqlDbType.VarChar).Value = textBox2.Text;
conn.Open();
int i = (int)comando.ExecuteScalar();
string a = i.ToString();
textBox3.Text = a;
if(i>0){
MessageBox.Show("Existe");
}else{
MessageBox.Show("Nem existe");
conn.Dispose();
}
}
}
}
You're probably thinking you're doing something else than what you're really doing.
You're expecting you've got local access to a local DB through an embedded SQL server. This is not the case. In reality, you're just starting the full fledged MS SQL Server service (sqlservr.exe) which is not tied to your application at all - apart from being started by your process, it's the same as if you had it configured to run at startup of Windows etc.
This isn't necessarily a bad thing, but if you're expecting your application to work with an embedded server, you can run into issues. Namely, the SQL server is configured on the computer, not through your application, it has to be installed (separately), if there is another server running, you're attaching to that one - for which you don't necessarily have permissions etc.
If you only use this for an internal tool, don't bother with changing anything, having the sql server process running is fine and you can shut it down through Services. If this is a part of your distributed application, consider using a different SQL server, for example SQL Server Anywhere (SQL Server CE), or perhaps even something completely different, like MS Access (freely available on every Windows PC, not just with Office) or FireBird.
Since your connection string indicates you're using LocalDB, from Introducing LocalDB, an improved SQL Express
LocalDB doesn't create any database services; LocalDB processes are started and stopped automatically when needed. The application is just connecting to "Data Source=(localdb)\v11.0" and LocalDB process is started as a child process of the application. A few minutes after the last connection to this process is closed the process shuts down. (emphasis added)
I have confirmed the "few minutes after" behavior in my own environment. But be sure you don't have any processes holding onto a connection anywhere.
I am creating a application and I want to use a local database stored on the clients local machines. I am debating over if I should use SQLITE or is there something in Visual Studio to help me. The other thing is that I want to create the database programmatically in the users directory when the application is launched.
I am see a few things online but the articles were all about SQL Server stuff and that is not want I want to do with this application. All data will need to be stored on the local machine.
You can use SQL Server Compact, which has tooling in Visual Studio. It's syntax-compatible with SQL Server, but stores its data in a local file, which you can create on the fly (at app startup, for example).
You can create the SQLite database on the fly with the libraries provided from their website. I have used it in many projects for my personal code, as well as it being used in some of the internal architecture of Data Explorer (IBM Product). Some sample C# to create a database file:
if (!Directory.Exists(Application.StartupPath + "\\data"))
{
Directory.CreateDirectory(Application.StartupPath + "\\data");
}
SQLiteConnection conGlobal;
if (!File.Exists(dbGlobal))
{
conGlobal = new SQLiteConnection("Data Source=" + dbGlobal + ";New=True;Compress=True;PRAGMA synchronous = 1;PRAGMA journal_mode=WAL");
conGlobal.SetExtendedResultCodes(true);
firstRun = true;
}
else
{
conGlobal = new SQLiteConnection("Data Source=" + dbGlobal + ";Compress=True;PRAGMA synchronous = 1;PRAGMA journal_mode=WAL");
conGlobal.SetExtendedResultCodes(true);
}
try
{
conGlobal.Open();
}
catch (Exception)
{
//do stuff
}
Simply initiating a connection to the file will create it if the new=true is passed as the connection string. Then you can query it and get results just like you would any database.
You also have the ability to password protect the database files to prevent access to them from just opening them with an SQLite-Shell or a different SQLite DB viewer.
For more info on the pragma statements that are being passed in the connection string, see the following: http://www.sqlite.org/pragma.html
I'm not sure about programmatically (that's probably what you meant, right?) creating the database, but SQL Server Compact Edition has served me well in the past for simple apps. It's embedded and even runs in medium trust.
I am deploying an application with sql server express 2008. In the prerequisites section of my application I have included:
As a result when a user installs my application it will install sql express as well.
Then I will be able to connect to that database engine as:
try
{
// database should be in the same network
SqlConnection conn =
new SqlConnection(#"Data Source=.\sqlexpress; Integrated Security=True");
conn.Open();
MessageBox.Show("Connection succesfull");
}
catch
{
MessageBox.Show("Unable to connect");
}
Now when I install a different application(client version) I will like to be able to connect to that database engine. I managed to connect to it by doing something like:
try
{
SqlConnection conn =
new SqlConnection(#"Data Source=192.168.0.120\sqlexpress,22559; USER=sa; PASSWORD=*********");
conn.Open();
MessageBox.Show("Connection succesfull");
}
catch
{
MessageBox.Show("Unable to connect");
}
In order for that code to work I had to do the following:
So my question is:
How could I configure this with code? When I deploy my application I want my application to install sql express like it does but I also whant to enable tcp/IP connections, enable some ports and lastly create a password for the account "SA" because I am not able to connect to the database remotly if the sa account does not have a password.
Or maybe I am asking to much and I am doing the wrong thing. perhaps I should do all this just for the database that I am planing on deploying not the database engine. whatever is easier. I have had a hard time deploying this maybe it will be eassier to deoploy a local database along with a wcf service in order to create CRUD operations on the local database remotely.
EIDT
I found this 3 links that claim on doing something similar and I still cannot make it work.
1) http://support.microsoft.com/kb/839980
2) http://social.msdn.microsoft.com/Forums/en-US/sqlexpress/thread/c7d3c3af-2b1e-4273-afe9-0669dcb7bd02/
3) http://www.sql-questions.com/microsoft/SQL-Server/34211977/can-not-connect-to-sql-2008-express-on-same-lan.aspx
downloaded sql server express 2008 (SQLEXPR32_x86_ENU.exe) and place it in the root of my c drive. then I install it with the following parameters:
C:\SQLEXPR32_x86_ENU.exe /q /hideconsole /action=Install /features=SQL /instancename=SQLEXPRESS /enableranu=1 /sqlsvcaccount="NT Authority\Network Service" /AddCurrentUserAsSqlAdmin /skiprules=RebootRequiredCheck /TCPENABLED=1
I add /TCPENABLED=1 in order to enable TCP/IP
I suggest you to create modified bootstrapper package to install Sql Server 2005 Express with customzation.
As an alternative, you can also use a custom action in your installer to change the targeted server using SMO.
Something like this:
Server server = new Server( "ServerName\\InstanceName" );
server.ConnectionContext.Connect();
server.Settings.LoginMode = ServerLoginMode.Mixed;
server.Settings.Alter();
We use SMO object to create user login and associate user to our created application database.. even run sql script to create database if database is not available..
Refer these links:
Configuring SQL Express During Installation
Configuring SQL Server when included as a requirement
Note: Create your sql server connection string settings in App.config file rather than putting hardcore in code.. this will help you customize application first run customization e.g. database creation.
These might be of some help, I've had it on my todo list for a while for the computers I have to setup for my app to run with Sql Server 2008 Express. It's basically a way to setup a script that the SQL08exp installer will read and automate a lot of the setup according to what you set in the script.
http://digitalformula.net/articles/how-to-perform-an-unattended-installation-of-sql-server-2008-express/
http://blogesh.wordpress.com/2008/09/23/silent-install-of-sql-server-2008/