connection not opening from WCF with dsn - c#

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.

Related

C# connect WinForm App running locally via Remote Desktop (or other) to a Database

EDIT
RDP is just the current way I do this, if there is another way to connect to a database in a different environment I will do that. Ideally I want this to connect to the database as it would if the executable was in the RDP environment and show no indication of connecting to the DB via the other environment.
EDIT 2
I have also tried adding a second connection string and opening it then having my current connection string immediately after, that didn't work.
I have a C# Win Forms application which connects to a database, this works fine except I need to RDP into the correct environment and have the executable ran from there to do so.
Is it possible to do the RDP connection inside of the application then connect to the database without having to have the executable inside the RDP.
I have tried to build up a connection string using the MSTSCLib library which I found here however this isn't working at code level anyway.
Code:
private void rdpConnect_Click(object sender, EventArgs e)
{
MSTerminalServiceControl1.Server = rdpServer.Text;
MSTerminalServiceControl1.UserName = rdpUserName.Text;
IMsTscNonScriptable secured = (IMsTscNonScriptable)MSTerminalServiceControl1.GetOcx();
secured.ClearTextPassword = rdpPassword.Text;
MSTerminalServiceControl1.Connect();
}
private void rdpDisconnect_Click(object sender, EventArgs e)
{
MSTerminalServiceControl1.Disconnect();
}
Current Path:
Desired Path:
I'm sure, that can not work, because...
RDP (Remote Desktop Protocol) is a Protocol to connect you from a remote machine to a session on a Terminal Server. That's the purpose. In that session, you can start programs, open documents and do your work. Like you were sitting in front of that machine. The benefit is, this can be done by multiple users, depending only on the number of user licenses and hardware power.
There a some special cases, e.g. you can start a program automatically on start up. Then your session is limited to this program and will end, when the program ends. But this is only a restricting. You will still start a session for that.
Also, there is a small version of Terminal Server build in every windows version, not only servers. This can be used a for a remote connection in the same way.
So, sad that, RDP can not connect to a database. In no way. You need a program for that. You can build a local program with a RDP-Client inside. That can connect to a session on a remote machine and start another program, that connects to the database. But you will have no communication between the to programs by default, so there is no benefit.
I think, like others said, the best is to connect to the database directly.
Looks like you do not have references to the ActiveX Control.
In your Toolbox Choose the COM Component : Microsoft RDP Control - version x
The version here is important it should match the installed RDP version, once you have added this in your tool box drop it on your form, if there is version mismatch it would throw error. I would say start from the latest version of control and go down till you are able to successfully add it to your form. The MSTerminalServiceControl1 in your code is actually the name of this control that you should thus put on your form.
What you need is to Connect multiple sql users to one database and no to use RDP.
you should write a special connection string, for each user something like below:
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
"Data Source=ServerName;" +
"Initial Catalog=DataBaseName;" +
"User id=" + UserName + ";"
"Password=" + Password + ";";
conn.Open();
Put this in a class that accepts the username and password.
Full example:
class myConnection
{
public static SqlConnection GetConnection(string UserName, string Password)
{
string str = "Data Source=ServerName;Initial Catalog=DataBaseName;User id=" +
UserName + ";Password=" + Password + ";";
SQlConnection con = new SqlConnection(str);
con.Open();
return con;
}
}
Then connect each user to DB directly.
Provided you have app.config file for the executable, update the connection string parameter to point to the ip of rdp server.
Before you do this enable database port on firewall and also enable remote connections to database.
For mssql server.
Run sql server configuration manager.
Select database instance.
Enable tcp/ip under network configuration.
Under io addresses tab.
Set tcp port to 1443.
Restart instance.
Data Source=IP,1443\dbservername;
I hop this can be a starting point.
I've seen a Network Administrator configured our normal winform application to be opened remotely over the Internet using Citrix.
We made no changes to the exectuable file.
It will launch the application in the Citrix Virtual Server.
The problem is that it's a paid software.

how to deploy winform app with mdf file on server

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.

SQL Server Azure "Login failed for User"

I am using the following code to perform SQL commands to my azure DB. The I do two calls inside my ASP.NET MVC action method. One to delete from Table A, and the second call to delete from Table B.
using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConnectionString))
{
using (System.Data.SqlClient.SqlCommand command = conn.CreateCommand())
{
conn.Open();
command.CommandText = statement;
command.ExecuteNonQuery();
}
}
For whatever reason when I make the second call to this code (I have it in a Helper service class, it bombs with the following Azure error.
Additional information: Login failed for user 'MyUser'.
Is there something I'm not doing correctly but not perhaps closing a connection or something that Azure is having issues with this?
You need allow your IP Address to access azure database. click in configure and add your IP.
I just needed add to the connection string
Persist Security Info=False;
when you publish your project into azure from visual studio, there is a 'settings' tab on the left.
go to the settings. it will show you the connection string that you're using in the web.config.
what worked for me is I unchecked the check box that says 'Use this connection string at runtime (update destination web.config)' and everything went well for me.

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;

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