how to deploy winform app with mdf file on server - c#

I have a winform app with a database running on sql server 2012. I want the app to work on multiple computers connected to a server in the local network.
I deployed the app using clickonce and moved the mdf file to the server.
Then I installed the app in a few computers and it works on every one of them separately however when the app runs on one computer and I try to open it on another one I get the following exception:
Cannot open database (database name) requested by the login. The login failed.\r\nLogin failed for user 'USER-PC\user'
using (SqlConnection con = new SqlConnection(conString))
using (SqlCommand command = new SqlCommand("select 1", con))
{
con.Open(); //exception thrown here
object returnValue = command.ExecuteScalar();
if (returnValue != null)
returnString = returnValue.ToString();
con.Close();
}
The connection string is:
Data Source=(LocalDB)\v11.0; AttachDbFilename=path on server\database
name; Integrated Security=true;
I have tried to change attachdbfilename to Initial Catalog=database name, and add users to the db in ssms and add user id and password to the connection string. Nothing helped.
I'm afraid I have some basic concept misunderstood and doing it the wrong way.
How can I resolve this problem?

Please check/correct broken links between Login and DB-User with sp_change_users_login.

Related

Attaching SQL database behaviors

I am trying to create a setup.exe that installs an application I've written.
The application uses a database to store and retrieve information.
In the setup, I have the installing user supply a Database server\instance, credentials and the DATA directory to copy the database files to.
I want to programmatically and permanently attach the database.
I use the following to programmatically attach a database to my instance.
The con.Open() and con.Close() are just for testing.
System.Data.SqlClient.SqlConnection con;
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = #"Data Source=.\SQLEXPRESS;Integrated Security=true;Connect Timeout=30;User Instance=True";
ServerConnection serverconn = new ServerConnection(con);
Server s = new Server(serverconn);
s.DetachDatabase("DBX2", true, true);
s.AttachDatabase("DBX2", new System.Collections.Specialized.StringCollection { #"C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\DBX2.mdf", #"C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\DBX2_log.ldf" }, AttachOptions.None);
con.Open();
MessageBox.Show("Connection Opened");
con.Close();
MessageBox.Show("Connection Closed");
Several questions to just confirm:
I get an error that it is already open, when it tries con.Open(), so does attaching a database this way automatically open it? When I remove the con.open(), it runs through and closes it just fine.
When I attach my database this way, I do not see it attached in the SQL Server Management Studio, like if I were to manually attach it there. So is the database still attached and working after I exit my application, so that other applications can access it? Is attaching this way temporary, or is it just not picked up by the SQL Management Studio?
Is this the correct way to go about achieving what I want?
Each application has it's own independent 'connection string' that provides a way to connect to the same database. SSMS and other client executables are just more of these applications. Of course, each connection string could actually be the same thing as they all point to the same database, they are just stored differently.

SQL Server connection C# : Authentication Windows to database uses computer name instead

I have a problem connecting to my SQL Server Database, i should use a Windows Authentication, so i added Integrated Security = true in my connection string but i get the wrong identity. The program tries to open the connection to the database with the computer name instead of the session name.
I get the error that the login is unknown in the Database, of course it's unknown it's not the good one.
Here is my connection string :
"Server=SERVER\DEV;Database=MYDATABASE;Integrated Security=true"
(I replaced the true names of the server and the Database for confidentiality issues)
Here is the C# code:
private string connectionString = ConfigurationManager.AppSettings["connectionString"];
public IList<string> GetAuthorizedGroupsForAJob(string jobId)
{
IList<string> res = new List<string>();
try
{
SqlConnection cnn = new SqlConnection(connectionString);
//The error happens in the line below
cnn.Open();
I searched for a while but everyone who gets the login error seems to forget the Integrated Security=true or Trusted_Connection=true.
Any help is welcomed here
Enable ASP.NET impersonation on IIS at the website level if you want the logged in user to reach your SQL server.

Saving to database from WinForm

I'm having trouble to save details about a movie in my database. I know the name giving is not good, but I have just made 1 textbox for input right now, as I am getting desperate. Can anyone tell me what I have done wrong in my code.
If it helps I having the following error which suggests that it can't connect to my localDB but I am not sure.
Error from code :
Code:
private void btnRes_Click(object sender, EventArgs e)
{
SqlConnection sc = new SqlConnection();
sc.ConnectionString = ("Data Source=localhost;Initial Catalog=LoginScreen;Integrated Security=True");
SqlCommand com = new SqlCommand();
sc.Open();
com.Connection = sc;
com.CommandText = ("INSERT into movieTable (movieID, movieName, movieLength, movieDescription) VALUES ('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"');");
com.ExecuteNonQuery();
sc.Close();
Your local database is not accessible over network. Try the following -
Check whether server is on
Check whether firewall is not blocking the Program
Check whether Named Pipes and TCP/IP is enabled in SQL Configuration manager. To access SQL Configuration Manager, go to Start > SQL Server (Version) > SQL Configuration Manager. Then expand Client Configuration (if it's x64 then there will be 2 item , click that does not have 32 with it).
From the right pane select Named Pipe, Vias and TCP/IP and enable all and restart server.
Make sure you are using correct instance name. MQL Server Enterprise or Standard editions might not have instance names but also can be installed with instance name. See in you SQL configuration manager that you are using the proper instance name.
Then try again.
First of All check the Authentication mode of the Sql Server whether it is in Windows Authentication Mode or SQL Server Authentication Mode
Secondly Check whether the service of the SQL Server is in running state or not
If the SQl Server installation mode is SQl Server Authentication Mode then the connection string used is :
Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;
Password=myPassword;
If the SQL Server installation mode is Windows Authentication then the connection string is :
Initial Catalog=MyDb;Data Source=MyServer;Integrated Security=SSPI;

connection not opening from WCF with dsn

This code is written in WCF. when I add reference from localhost then this code works fine but when service reference is added from IIS then it fails in cn.Open() . It doesn't throw any exception and just hangs in this line.
OdbcConnection cn = new OdbcConnection();
cn.ConnectionString = "DSN=myDsn;Uid=myuid;Pwd=mypassword;";
cn.Open(); // Fails on this line
Or is there any other way which which I can get datasource name with dsn in C#?
Makes sure the DSN was created as a system DSN and not a user DSN. If it's created as a user DSN (as you) then the IIS service user will not have access to it.

SQL connection string for database on network

I want to connect to a database on a host except localhost, my DBMS is SQL Server and I'm using ado.net, like this:
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand("insert into st (ID,Name) values ('"+cnt.ToString()+"','havijuri');", con);
//some sql commands.
con.Close();
what should I use as the constr (connection string), and with these information:
host IP: 10.15.10.12
the file is database1.mdf,
in this directory(on the host): D:\Project1\DataBase
Tell me if any other information is needed
If the mdf file is not attached to an instance of sql server and you want to connect to the database while it does not exist on the same machine as your application, you need first to copy the database to the server with the mentioned IP and attach it to an instance of sql server installed on that server.
The connection string in this case -if you have a domain and will be authenticated to the database server by windows authentication- will be as follows:
"data source=10.15.10.12; integrated security=SSPI;initial catalog=database1"
Or you can create a sql server user on the database sever and connect using the following connection string:
"data source=10.15.10.12; initial catalog=database1;user id=<username>;password=<password>"
http://www.connectionstrings.com/sql-server-2008
create a file on your desktop called test.udl open it up and follow the steps to connect to your database then click test to make sure it works. then open the file in notepad, it will be 1 line and contain the connection string
and with these information:
•host IP: 10.15.10.12 •the file is
database1.mdf, •in this directory(on
the host): D:\Project1\DataBase
You can not. Database file attachment is only supported by express, not by the real server. ForSL Server, you need the database name (which can be different than the file name) and the database must e mounted first by the DBA. You also need acces to the server (as in: username, password). The security credentials are - again - determined by the DBA.
So, you miss the critical information (name of the database, username, password) to access a database server.

Categories

Resources