C# Create SqlConnection instance with Connection String - c#

I tried to create SqlConnection object with connection string such as
SqlConnection con = new SqlConnection("Connection string");
However, when the program runs, exception happens, and the exception says
the keyword, port, is not supported.
So I change connection string from
server=server name;user id= user name;password=myPassword;persistsecurityinfo=False;database=Database name;Port=port number
to
server=server_name, portNum;user id= user_name;password=myPassword;persistsecurityinfo=False;database=Database_name;
or to
server=server_name: portNum;user id= user_name;password=myPassword;persistsecurityinfo=False;database=Database_name;
But I still have problems to connect to database.
Could anyone give me how to connect database with port number?
Sincerely,

You can use the port at the end of the data source
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;
Check http://www.connectionstrings.com/sql-server/

Related

Trying to connect to SQL Server using C#

I am trying to connect to SQL Server and get data. This is what I did, but it's not working:
string connectionString;
SqlConnection cnn;
connectionString = #"Data Source=(IP)\PC-NAME\SQLEXPRESS,3306;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=sa;Password=password";
cnn = new SqlConnection(connectionString);
cnn.Open();
MessageBox.Show("Connection Open !");
cnn.Close();
Your Code is Correct, except your connection string i think
So, first, connect to your database via server Explorer in VisualStudio\View menu
Then right-click on your database and select properties and check the connection string and copy that for test
I think you have a problem with your connection string.
Check your connection string using this given example:
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial
Catalog=myDataBase;User ID=myUsername;Password=myPassword;

C# Winforms connection to MySQL AWS Database

I'm trying to connect a winforms .net application with an AWS RDS MySQL database but I am having difficulty making the connection. I have read a lot of material about connecting through Microsoft SQL database and through Elastic Beanstalk but I haven't come across the answer I'm looking for... possibly because I'm a noob.
I've looked through a few of these questions:
How to connect to MySQL Database?
https://dev.mysql.com/doc/dev/connector-net/8.0/html/T_MySql_Data_MySqlClient_MySqlConnection.htm
using MySql.Data.MySqlClient;
string connection = "server=localhost; Database=database_URL; User Id=admin;
Password=myPassword";
myConn.Open();
MessageBox.Show("Success");
I'm getting the following error message:
MySql.Data.MySqlClient.MySqlException: 'Unable to connect to any of the specified MySQL hosts.'
Is there something simple that I'm missing? I have copied the database endpoint into the database_URL location. My user id and password are correct. My database is setup on AWS as a MySQL database.
Checking back with ConnectionStrings makes it appear as if your parameter-names are wrong. 'username' should be 'uid' and 'password' should be 'pw'.
In any case I'd suggest using the MySqlConnectionStringBuilder-class to construct your connection string.
var connectionStringBuilder = new MySqlConnectionStringBuilder
{
Server = "<Instance_Ip>",
UserID = "root",
Password = "<Password>",
Database = "<Database_Name>"
};
using (var conn = new MySqlConnection(connectionStringBuilder.ToString()))
The error message is given because can't connect to the host.
In your connection string is given the localhost as the server but your database is on cloud (AWS), so it means that you must specify the database's IP or the domain name pointing to that database, not the local (local means that is in your computer). e.g.
string conn = "server=192.168.0.7; Database=database_name; User Id=admin;
Password=myPassword";
Note that the server IP is provided by AWS, and you'd make sure that ports are enable. The most common port for MySQL is 3306.
Best regards.
Try this,
//This is my connection string i have assigned the database file address path
string MyConnection2 =
"host='localhost';database='databasename';username='myusername';password='mypassword'";
//This is my insert query in which i am taking input from the user through windows forms
string Query = "Your query";
//This is MySqlConnection here i have created the object and pass my connection string.
MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
//This is command class which will handle the query and connection object.
MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
MySqlDataReader MyReader2;
MyConn2.Open();
MyReader2 = MyCommand2.ExecuteReader();
// Here our query will be executed and data saved into the database.
MessageBox.Show("Save Data");
while (MyReader2.Read())
{
}
MyConn2.Close();

Send Oracle Connection State to Label

Okay I'm working on a C# web app and I am trying to test the connection to my oracle database. I would like to send the connection state to a label. I don't know if I am using the commands correctly (this is my first time using an oracle connection with C#). Here is my code:
OracleConnection conn = new OracleConnection();
conn.ConnectionString = "Data Source=servername; Initial Catalog=dbname; User ID=userid; Password=password; Integrated Security=no;";
conn.Open();
string connstate;
conn.State.ToString(connstate);
Label_connectiontest.Text = connstate;
It is just
Label_connectiontest.Text = conn.State.ToString();
The State property of a IDbConnection object is an enum of type ConnectionState with the FlagAttribute set.
Applying the ToString method to this enum results in returning a value that is a string containing a delimiter-separated list of the names of the constants.

SqlConnection unable to connect using IP address

pretty simple question.
I cannot connect using this string:
SqlConnection myConnection = new SqlConnection("network address=(192.168.1.120); password=userpassword; user id=username; database=myDB");
I try to change it to data source, address, anything and anytime I have a period in the string it raises an exception. If I change it to point to my localhost name with no period or a hostname that is not using an FDNQ it works just fine. does anyone know what's up with that?
The exception raised when using an IP address or server name with several subdomains is that the period is invalid. "Incorrect syntax near '.'."
I went to connectionstrings.com and copied the same string I used before and it failed.
my SQLCommand had bad SQL syntax in it due to several lines of concatenation. Stupid me.
Try it like this:
SqlConnection myConnection = new SqlConnection("network address=192.168.1.120; password=userpassword; user id=username; database=myDB");
You can find the comprehensive Keyword reference for SqlConnection connection strings here: http://msdn.microsoft.com/en-us/library/ms130822.aspx

Can't connect to database server

I'm encountering a problem with my database connection.
I started with a new blank solution, then I added a WCF library project, and last but not least a WCF website (service).
In the website i added a reference to the library where I have the interface (data contract), the class that implements the interface and the dataclasses.
what I'm trying to do is to connect to a database on a server and try to retrieve some data from there.
So the connection string looks like:
<add name="myConnectionString" connectionString="Data Source=MyServer; Initial Catalog=MyDatabase; User Id=me; Password=me123;" providerName="System.Data.SqlClient" />
and this is how I'm trying to connect with the database:
public List<string> GetEngagements(string id)
{
string sql = "SELECT myColumn FROM myTable WHERE Id = '" + id + "'";
string connString = string.Empty;
SqlConnection connDB;
connString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
connDB = new SqlConnection(connString);
SqlCommand command = new SqlCommand(sql, connDB);
connDB.Open();
SqlDataReader rdr = command.ExecuteReader();
List<string> numbers = new List<string>();
while (rdr.Read())
{
numbers.Add(rdr[0].ToString());
}
rdr.Close();
return numbers;
}
I'm getting an exception on connDB.Open().
Then exception message says:
Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.
I've been getting this message for 2 days now, I've googled a lot and deleted the C:\Documents and Settings\username\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS directory but it didn't work for me..
Any solution???? help please
The error message:
Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance.
Suggests that you're using user instancing, and therefore your connection string will point to an .mdf file on disk rather than the name of a database.
So I'll assume that you want to connect to a file instance rather than a server instance.
I'll also assume that you're using SqlExpress rather than the full fat version.
In which case your connection string is wrong. It should look more like this:
"Data Source=.\SQLEXPRESS;
AttachDbFilename=fileOnDisk.mdf;
Integrated Security=True;
User Instance=True;"
User instancing means that this server instance and the DB inside will only be visible to the application opening the connection string.
You don't have to use user instancing - you can set User Instance=False or just leave it out. Then once the application has made the connection you can connect other tools to the server instance and connect to the DB yourself.

Categories

Resources