System cannot find file while connecting database in nhibernate - c#

I am using fluent nhibernate since long. Its working fine until I updated my database. Prior I was using SQL Server 2012, and updated it to 2016. When try to connect database in application, it throws an error:
The system cannot find the file specified.
when it tried to connect. My connecting function is as below
Fluently.Configure().Database(MsSqlConfiguration.MsSql2005.ShowSql().ConnectionString(x=> x.FromConnectionStringWithKey("imConnectionString2"))).Mappings(m=> m.FluentMappings.AddFromAssemblyOf<MapUsers>()).BuildSessionFactory();
This was working fine before database update. I change MsSql2005 it to MsSql2012, but same result.
Do I have to do anything in Fluent Nhibernate side or configuration?
Any assistance please

There is a major change in the client connectivity for SQL Server 2016.
This change is due to sql client is becoming support windows and other O.S like Linux.
install Microsoft ODBC Driver 13 for SQL Server - Windows
https://www.microsoft.com/en-us/download/details.aspx?id=50420
review: Installing SQL Server Native Client
SQL Server Native Client (SNAC) is not supported beyond SQL Server 2012. Avoid using SNAC in new development work, and plan to modify applications that currently use it. The Microsoft ODBC Driver for SQL Server provides native connectivity from Windows to Microsoft SQL Server
Before configuring Nhibernate, be sure that you can connect with SQL server 2016 by using sqlcmd tool (for server 2016- download it)
Update:
When you install the driver named msodbcsql.msi, it's really the same driver of sql 2012 client.
I installed the driver in windows 7 (32 bit), using the library FluentNHibernate v 2.0.3 and connecting to SQL server 2016 and running successfully the following code:
class FluentNHibernateTest
{
private static ISessionFactory CreateSessionFactory()
{
//also MsSqlConfiguration.MsSql2005 is working
return Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2012.ShowSql()
.ConnectionString(x => x.FromConnectionStringWithKey("test16")))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
.BuildSessionFactory();
}
public static string GetSqlVersion()
{
string sql = "select ##version version";
var sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
var query = session.CreateSQLQuery(sql);
var result = query.UniqueResult();
Console.WriteLine(result);
return result.ToString();
}
}
}
Output result:
FluentNHibernateTest.GetSqlVersion();
Microsoft SQL Server 2016 (RTM-CU1) (KB3164674) - 13.0.2149.0 (X64)
Jul 11 2016 22:05:22
.......

Related

How to find out that SSIS installed in SQL Server using C#?

I'm writing a Windows Form application using C#. I use
ssisServer = new IntegrationServices(ssisConnection);
to run and work with SSIS packages but before running the package I wanted to make sure the user runs the application in the correct system and SQL Server has the SSIS on it's SQL Server.
How can I find whether SSIS is installed on the server or not in C#?
Check if the SQL Server Integration Services service is running on the server your connecting to.
SQL Server Service Name
SQL 2008 MsDtsServer100
SQL 2012 MsDtsServer110
SQL 2014 MsDtsServer120
SQL 2016 MsDtsServer130
SQL 2017 MsDtsServer140
SQL 2019 MsDtsServer150
The name of the service will depend on what version of SQL Server you are connecting to. If the server was SQL Server 2019 then it would be MsDtsServer150 for example. You could simply check if the service exists and is running.
Add System.ServiceProcess to your project references (It's on the .NET tab).
using System.ServiceProcess;
ServiceController sc = new ServiceController("MsDtsServer150");
switch (sc.Status)
{
case ServiceControllerStatus.Running:
return "Running";
case ServiceControllerStatus.Stopped:
return "Stopped";
case ServiceControllerStatus.Paused:
return "Paused";
case ServiceControllerStatus.StopPending:
return "Stopping";
case ServiceControllerStatus.StartPending:
return "Starting";
default:
return "Status Changing";
}

Unable connect to MySQL from C# but possible from other apps

I wanted to change MySQL server in my C# app. The app is run in my company and both - old and new MySQL servers are only accessible from the intranet. The problem is I can connect from HeidiSQL software and from python code but not from C#...
I have tried all possible solutions what I found, for ex. disabling firewall, using different packages from NuGet, modifying connection string, I created new console project only to paste various MySQL connection code - always with same error
Message: Unable to connect to any of the specified MySQL hosts.
Source: MySql.Data
Number: 1042
I', using .net Framework 4.5.2 (4.6.1 in my test project) and MySQL Server is '5.6.44-log - MySQL Community Server (GPL)'
One of C# connection code example that I have tested that is NOT working
using MySql.Data.MySqlClient;
public static MySqlConnection DB_connection;
DB_connection = new MySqlConnection(#"Server=MyIP;Database=myDB;Uid=my_user;Pwd=pass;");
try
{
DB_connection.Open();
isConn = true;
}
catch (...)
Working python code run from the same PC
import pymysql
import pprint
connection = pymysql.connect(host='MyIP',
user='my_user',
password='pass',
db='myDB')
try:
with connection.cursor() as cursor:
sql = "select * from table;"
cursor.execute(sql)
# connection.commit()
result = cursor.fetchall()
pprint.pprint(result)
finally:
connection.close()
Don't see anything wrong with your posted connection string but in case your's is a replicated scenario (I mean DB replication exists) then you will have to specify the replicated server IP/hostname as well like
Server=serverAddress1, serverAddress2, serverAddress3;Database=myDataBase;
Uid=myUsername;Pwd=myPassword;

Programmatically script or migrate SQL Compact database for importing into SQL Server

I have used ErikEJ's SQLite/SQL server Compact Toolbox which allows migration from a compact database to SQL Server, but I am trying to implement this process into my ASP.NET application.
The user will have an already completed SQL Server Compact database.
The process will be
1) User selects the database
2) Existing SQL Server database will be deleted
3) Compact database will be scripted/migrated into SQL Server
The part I am unsure of is how I should script or migrate the SQL Compact database. Deleting and creating a new database on the server itself seems easy enough. I have been digging around in the System.Data.SqlServerCe class for a while and am unsure what the best approach would be.
Using my scripting API, you can do something like this:
using (IRepository ceRepository = new DB4Repository(#"Data Source=C:\Data\SQLCE\Test\nw40.sdf"))
{
string fileName = Path.GetTempFileName();
var generator = new Generator4(ceRepository, fileName);
generator.ScriptDatabaseToFile(Scope.SchemaData);
using (IRepository serverRepository = new ServerDBRepository4("Data Source=.;Trusted_Connection=true;Initial Catalog=Test"))
{
serverRepository.ExecuteSqlFile(fileName);
}
}
See my blog post here: http://erikej.blogspot.dk/2013/03/sql-server-compact-code-snippet-of-week_8.html
A more complete implementation (from my Toolbox)
https://github.com/ErikEJ/SqlCeToolbox/blob/master/src/GUI/SqlCe35Toolbox/Commands/SqlServerDatabaseMenuCommandsHandler.cs#L370

MS Sync Framework and SQL Server Compact

I develop a Windows C# application which can work in Online and Offline mode.
When in Online mode it connects to a SQL Server. In Offline mode it connects to a local DB.
I use the Microsoft Sync Framework 2.1 to sync the 2 databases on demand.
Until now I used a LocalDB instance of SQL Server as the local database. But it is a pain to setup the system automatically during the installation process of my application. So I tought to use SQL Server Compact 3.5 or 4.0 which is very easy to distribute (comes in a single file).
But I cannot get it to even compile the provisioning code of the Compact DB:
DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription("MyScope");
SqlCeConnection clientConn = new SqlCeConnection(OfflineConnectionString);
var clientProvision = new SqlCeSyncScopeProvisioning(clientConn, scopeDesc);
clientProvision.Apply();
which I used before (without the Ce classes) but SqlCeSyncScopeProvisioning cannot be resolved.
Something is terribly wrong here.
How can I sync my CompactDB to distribute this as my local database?
First ensure, you have successfully installed the sdk?
After this, make sure you have added the following references:
Microsoft.Synchronization.Data.dll,
Microsoft.Synchronization.Data.Server.dll
Microsoft.Synchronization.Data.SqlServer.dll
Microsoft.Synchronization.Data.SqlServerCe.dll
Also in my case it worked with adding
System.Data.SqlServerCe - 4.0
EDIT
Against your comment, this is only working if you use SQL Server CE 4.
I've tried it now with SQL Server CE 3.5 and indeed I could reproduce your issue.
Switching to SQL Server CE 4.0 fixed it.
ExampleTable 4.0
TestCode
var scopeDesc = new DbSyncScopeDescription("MyScope");
var tbl = new DbSyncTableDescription("TestTable");
var pkColumn = new DbSyncColumnDescription("Id", "int");
pkColumn.IsPrimaryKey = true;
tbl.Columns.Add(pkColumn);
tbl.Columns.Add(new DbSyncColumnDescription("Name", "nvarchar(254)"));
scopeDesc.Tables.Add(tbl);
var clientConn = new SqlCeConnection(#"Data Source=test.sdf;Persist Security Info=False;");
var clientProvision = new SqlCeSyncScopeProvisioning(clientConn, scopeDesc);
clientProvision.Apply();
Result
Everything compiles nicely. After following the above steps, you should be able to easily migrate your code to SQL Server CE

Enable remote connections to sql express with a script

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/

Categories