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;
Related
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.
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.
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.
I'm trying to access my SQL Server Express database, but every time I try, I receive this message:
Login failed for this user.
Here is the code:
sqlConnection cs = sqlConnection("Data Source=.\\SQLEXPRESS;
Initial Catalog=MyDatabase#4; Integrated Security=TRUE");
cs.Open();
MessageBox.Show(cs.State.ToString());
cs.Close();
How can I fix this problem?
Try to write your computer name instead of . after "Data Source". For example:
Data Source=Michael-PC\\SQLEXPRESS;Initial Catalog=MyDatabase#4;Integrated Security=True";
The SQL is not able to authenticate your windows user because it may have no access rights on the database... Try using an administrator account or to use a SQL Server privileged account.
What I sometimes do is get Visual Studio to connect, via the Server Explorer, and then copy and paste the connection string from there. If you have SQL Management Studio Express, that will work too.
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.