I want to create connection string so that I can connect to SQL database remotely in my C# code using OleDb as a provider and the server IP address.
Note 1 : I'm using OleDb because I want to handle different database types. SQL is just an example.
Note 2 : The database resides on another machine (machine on another network)
I did all the setup to connect from another machine (firewall,Enable TCP/IP..etc), and now I can connect remotely using Microsoft SQL Server Management 2014 by specifying (server name : My Computer Name-PC\Instance name) and using SQL Authentication then entering the username and password and press connect and then it goes well, I connect successfully.
BUT I TRIED A LOT OF COMBINATIONS TO BUILD THE CONNECTION STRING IN MY C# CODE AND NONE OF THEM WORKS EXCEPT THIS :
OleDbConnection conn = new OleDbConnection(#"provider = sqloledb; data source = MyCompName-PC\sqlexpress; Initial Catalog = DataBase1 ; user id = MyUsername ; password = MyPassword ;");
Otherwise if I try to use my server public IP address instead of MyCompName in Data Source it keeps giving me error : server not found or access denied.
I search in connectionstrings.com but problem is still there.
From your post it looks like you are trying to connect to a SQL Server database then why are you using OleDbConnection? instead of using SQL Server connection provider.
OleDbConnection connection provider is used for connecting to MS Access database.
You should be using a SqlConnection class like
SqlConnection conn = new SqlConnection("data source = MyCompName-PC\sqlexpress; Initial Catalog = DataBase1 ; user id = MyUsername ; password = MyPassword ;")
See SQLConnection Reference for more information as commented by #AlexK.
Related
I have already implement a control panel for MySQL database. That works perfect with local database which is installed on my PC. But when i try to connecting with mysql database on my web host i get this message:
cannot connect to any of the specified mysql hosts
Here is my connection string:
var csb = new MySqlConnectionStringBuilder
{
Server = "database.webhost.com",
Port= "3306"
UserID = "root",
Password ="1234567",
Database ="sampleDB",
SslMode= "None",
};
I was reading something about SSH secure connections to web hosts, but i can't find any examples.
I am helping my friend in his college final year project, Its an C# application in a client machine and it uses SQL server in the windows server 2008 environment.
VM ware environment:
3 clients(windows 7) and 1 SQL database (in windows 2008)
The connection between he server and the client is working, the port 1433 for SQL is open and its listening.
When I try to run the program am getting the following error.
This is the connection string which am using.
cn = new SqlConnection(#"Data Source=192.168.1.2,1433;Initial
Catalog=Test;Integrated Security=True");
And i have also tried to refer this website for connection strings, but i don't know how it works.
http://www.connectionstrings.com/sql-server-2008
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN; Initial
Catalog=myDataBase;User ID=myUsername;Password=myPassword;
I am getting an error. Tried to give a simple username and password in the SQL server and tried it, still the same issue. please check the image. The Username and password which i used is the System login(windows authentication)
I just want to access the SQL server in the Windows 2008 machine.
Where should i check the credentials for this connection string ?
Thank you soo much for reading this patiently.
You can go to Connectionstrings.com to get more information on connection strings and its usages.
Try removing the port from your connection string (1433 is the default SQL port):
cn = new SqlConnection(#"Data Source=192.168.1.2;Initial Catalog=Test;Integrated Security=True");
For a connection using sql authentication (which would eliminate trust issues between machines) use the following (replacing xxxx and yyyy with the appropriate user name and password):
cn = new SqlConnection(#"Data Source=192.168.1.2;Initial Catalog=Test;Integrated Security=False; User=xxxx; Password=yyyy");
The following is a connection string generated when I connect to a database using a configuration tool with Microsoft OLE DB Provider for SQL Server.
Provider=SQLOLEDB.1;Password=password;Persist Security Info=True;User ID=sa;
Initial Catalog=database;Data Source=localhost;Use Procedure for Prepare=1;
Auto Translate=True;Packet Size=4096;Workstation ID=computer1;
Use Encryption for Data=False;Tag with column collation when possible=False
If I connect to the same database but with SQL Server Native Client 10.0 I get this connection string.
Provider=SQLNCLI10.1;Integrated Security=\"\";Persist Security Info=False;
User ID=sa;Initial Catalog=database;Data Source=localhost;Use Procedure for Prepare=1;
Auto Translate=True;Packet Size=4096;Workstation ID=computer1;
Initial File Name=\"\";Use Encryption for Data=False;
Tag with column collation when possible=False;
MARS Connection=False;DataTypeCompatibility=0;Trust Server Certificate=False\0
I have a c# application that reads either one of these connection strings and uses it to create a connection to my database like so
SqlConnectionStringBuilder _sqlConnectionStringBuilder = new SqlConnectionStringBuilder();
OleDbConnectionStringBuilder conBuilder = new OleDbConnectionStringBuilder( CONNECTION STRING SHOWN ABOVE);
_initialCatalogValue = (string)conBuilder["Initial Catalog"];
_dataSourceValue = conBuilder.DataSource;
_sqlConnectionStringBuilder.Password = (string)conBuilder["Password"];
_sqlConnectionStringBuilder.UserID = (string)conBuilder["User Id"];
_sqlConnectionStringBuilder.InitialCatalog = _initialCatalogValue;
_sqlConnectionStringBuilder.DataSource = _dataSourceValue;
_sqlConnectionStringBuilder.PersistSecurityInfo = true;
_conn = new SqlConnection(_sqlConnectionStringBuilder.ConnectionString);
_conn.Open();
The problem that when I use the SQL Server native client the password is empty and my SQLConnection will fail on the login. The error I get is "Login failed for user 'sa'".
The OLE DB connection string is successful. Is my login failing for the sql server native client due to some of the classes I am using in c#? Does the Sql Server Native Client 10.0 encrypt the password? Should I try to identify which provider is in the connection string and have two different code paths? If so what would it take to connect?
Basic question is, how can I ensure a successful connection regardless of which connection string I receive (only the two above)?
N.B. I have no control over the connection strings. I can only work with what I am receiving.
The second connection string you provide does not include a password; thus conBuilder["Password"] returns an empty string when you set conBuilder.ConnectionString to the second string.
The problem was solved as follows.
There was a main application that was making use of the above connection strings and was doing the following.
The application would take the connection string.
If the connection string's provider is SQL Server Native Client (SQLNCLI10.1) the application checks for persistent security. If it cannot find any it adds IntegratedSecurity=SSPI to the connection string and then connects using windows authentication instead. Whether or not this is the right (or secure thing to do) that is what was being done.
To 'answer' the question. You can take in the connection string and if you do not find a password you can set IntegratedSecurity=SSPI. This will allow you to connect using windows authentication instead of SQL Server authentication. I am not advising that you do, but it will work.
I am going to make a smaill application with c# .net 2.0 here I have to connect the Mysql server i am trying many tutorial but fail
I am using
string constr = "Server=11.22.33.123;Port=3306;Database=bfcerin_ad;" +
"Uid=<user id here>;Pwd=<password here>;pooling=false;";
MySqlConnection con = new MySqlConnection(constr);
con.Open();
I have tried twice my user name and password is fine but getting access denied from Server
I am able to connect to the localhost mysql with this code but not remote mysql server
Login your cpanel and click on Remote MYSQL and then add Remote Database Access Hosts put % for giving access to all host.
I have been trying and searched thousands of times in other sites but never found a sample or simple code to use.
I've created an application C# which uses an ODBC connection, i have also MS SQL installed and configured to enable remoting database information sharing. I want to make my database available to everyone uses this application i have made by using a connection.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CaseMatter
{
public partial class mainLog : Form
{
string userID, userName, userAddress, userFName, userLastName, userCity, userPassword, userTele;
public mainLog()
{
InitializeComponent();
this.Size = new Size(442, 162);
}
private void Submitbtn_Click(object sender, EventArgs e)
{
string ConnectionString =
"Data Source=xx.xx.xx.xx,1433;Initial Catalog=master;Integrated Security=SSPI;";
var conn = new SqlConnection(ConnectionString);
conn.Open();
var strSQLCommand = "SELECT Name FROM example WHERE id='1'";
var command = new SqlCommand(strSQLCommand, conn);
var reader = command.ExecuteReader();
while (reader.Read())
{
passwordBox.Text = reader.GetString(0);
}
reader.Close();
conn.Close();
}
}
}
I have just edited the code and tried it, i have added a try catch to handle sql exceptions but it still freezes when i click on submit button.
Hopefully someone figures this out.
ERROR:
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.)"
I suspect there are one of four things going on here.
The SQL Server is misconfigured and isn't responding to tcp connections. The easiest way to trouble shoot this is to use SQL Management Studio to connect. If you can't connect this way then you'll have to look at the sql server service setup. Also, make sure that the SQL Browser service is running on the server.
The SQL Server is configured to use a different TCP port than the one you are trying. Remove the ",1433" from your connection string and try again. SQL Browser should respond back with the actual port that sql server is listening on.
The SQL Server has a firewall in place that is blocking remote connections. Temporarily turn off the firewall and see if you can connect. If you can, then configure the firewall correctly and turn it back on.
Your local box has some type of firewall that is blocking outgoing connections on that port. Try turning yours off to see if this helps. If so, configure it correctly and turn it back on.
If this is a brand new sql server that no one has remotely connected to then it's very likely to be entirely within the configuration of SQL server or the Windows configuration.
In your example you are not passing the USER ID and Password. Is that just because you didnt want to include your credentials? I assume not as you properly XXX out your IP address. I would start by supplying credentials of a SQL server account that can access your database.
I noticed that you're using SqlConnection which is not an ODBC connection object. With SqlConnection you're actually connecting via ADO.NET. The connection string syntax for SqlConnection (both ODBC and ADO.NET) can be found on the following site:
http://connectionstrings.com
By the way, if you're connecting to SQL 2005, the connection string would look like this:
using System.Data.SqlClient;
// trusted connection (SQL configured to use your windows ID)
string ConnectionString =
"Data Source=xxxxxxx;Initial Catalog=xxxxxx;Integrated Security=SSPI;"
// sql authentication (SQL manages its own users/pwds)
//string ConnectionString =
//"Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
var conn = new SqlConnection(ConnectionString);
conn.Open();
var strSQLCommand = "SELECT Name from [example_autoincrement] where id='1'";
var command = new SqlCommand(strSQLCommand, conn);
var reader = command.ExecuteReader();
while (reader.Read())
{
passwordBox.Text = reader.GetString(0);
}
reader.Close();
conn.Close();
Ok, this is a strange one to check but just make sure the time and date is accurate on client and server. SSPI requires a maximum gap of 5 minutes between each