Parameters needed to connect to a MySql Database using C# - c#

When I want to connect to Database using C# visual studio 2013, I provide the following parameters to the "connectionString" using "MySqlConnection":
string server="localhost";
string database="database123";
string uid = "*****";
string password = "******";
string port = "3306";
but I noticed that if I don't provide "database" or "port" it works fine too.
My question is what should one provide to establish a proper connection?
or What is considered sufficient info to establish a connection?

I would use this.
SqlConnection connection = new SqlConnection("server=somthing; database=something; User ID=******; Password=******");

Related

Oracle connection with port, service name, and database from C# (ORA-12514

I'm new to Oracle. Trying to connect C# windows app to an Oracle database but can't seem to establish a proper connection. Keep getting exception: "ORA-12514: TNS:listener does not currently know of service requested in connect descriptor". I have to specify the port, service name, and database name in the connection string because the service id has access to multiple databases. I know that the values in the string are valid (valid server, valid serviceid, valid username, password, etc) because I have a third-party tool that is able to connect using the same parameters from a wizard. I've tried a lot of different ways to format the connection string but I always get the same 12514 error. In the code example, you'll see three formats (cxn, cxn2, and cxn3), I've tried each of them but get the same error.
string cxn = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MyServerName)(PORT=MyPortNumber))" +
"(CONNECT_DATA=(SERVICE_NAME=MyServiceId)));User Id=MyUserName; Password=MyPassword;";
string cxn2 = "DATA SOURCE=MyServerName:MyPortNumber/MyUserName;" +
"PERSIST SECURITY INFO=True;USER ID=MyUserName; password=MyPassword; Pooling = False;";
string cxn3 = "DATA SOURCE=MyServerName:MyPortNumber/MyServiceId;" +
"PERSIST SECURITY INFO=True;USER ID=MyUserName; password=MyPassword; Pooling = False;";
using (OracleConnection conn = new OracleConnection(cxn3))
{
string sqlSelect = "SELECT * FROM PERSONS";
using (OracleDataAdapter da = new OracleDataAdapter(sqlSelect, conn))
{
var table = new DataTable();
da.Fill(table);
if (table.Rows.Count > 1)
Console.WriteLine("Successfully read oracle.");
}
}
Again, I've used MyServiceId in the third-party tool's wizard and I connect just fine and select my database. I'm using Oracle.ManagedDataAccess.Client. I consulted a number of articles online including Oracle's guidance in section "Getting Started with ODP.NET, Managed Driver". How can I get the driver to recognize the valid service id and then also accept the database name? Any guidance is appreciated. Thanks.
Well I wish I had a more definitive explanation but as it turns out the code from my original question works NOW using the connection string defined in variable "cxn". I ran it many times before with no success, so my only guess is that the DBA changed something or rebooted the server since initial configuration.

How to create connection string that independent on instance and server name?

i want my application can run on every device that maybe have different server name and instance. my connection string is below:
DBDataContext db = new DBDataContext(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\App_data\DB.mdf;Integrated Security=True;User Instance=False");
If I understand you correctly and the Data Source is always on the local server, why don't you get the ip address of the server with System.Web.HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"];
string myLocalserverIP = System.Web.HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"];
then use that in your Data Source string.

MySql access denied for user - can't create the user the software is looking for

My connection string looks like this:
string server = "localhost";
string database = "stman";
string user = "logan";
string pass = "root";
string connStr = String.Format("Data Source={0};Initial Catalog={1}; User Id={2}; Password={3};", server, database, user, pass);
Given my understanding, this means MySql should be using 'logan'#'localhost' for the login, but it isn't.
It looks as though it's using 'logan'#<server's fully qualified name>:
Access denied for user 'logan'#'HP-PL-ML110.young.home'
I'm at a loss right now. SqlYog doesn't allow me to create a user with that hostname, my options for hostname are %, localhost or 127.0.0.1
Can anyone help me make sure that the website is using the specified username here? I have no idea what to look at to fix this
Joe's comment:
Have you tried disabling host name lookup, as in this question
Applying this didn't fix the issue but it did change the settings so that I could use the IP address in the connection string.
This does mean I had to create a new user (I found that, in Sqlyog, you can enter something different in the hostname field in user creation) and grant the new user the required permissions on the target database.
It's not exactly a solution, but it is a viable workaround that's got me back to a point where I can actually progress further in my development now.
Thanks Joe
string connStr = String.Format("Data Source={0};Initial Catalog={1}; User Id={2}; Password={4};", server, database, user, pass);
Should be
string connStr = String.Format("Server={0};Database={1};Uid={2};Pwd={4};", server, database, user, pass);

How do I find my Connection String required to automate SAP through a C# application?

I am trying to make a connection to SAP through C#. For reference, I am using code found here: How do I automate SAP GUI with c#
I have added a reference to sapfewse.ocx and the corresponding using statement. Here is my code:
using SAPFEWSELib;
private void OpenSAP()
{
GuiApplication Application;
GuiConnection Connection;
GuiSession Session;
Application = (GuiApplication)System.Activator.CreateInstance(Type.GetTypeFromProgID("SapGui.ScriptingCtrl.1"));
// How do I find the connection string that I use to connect to SAP?
Connection = Application.OpenConnectionByConnectionString("XXXXXXXXX", false, true);
Session = (GuiSession)Connection.Sessions.Item(0);
Session.TestToolMode = 1;
((GuiTextField)Session.ActiveWindow.FindByName("RSYST-MANDT", "GuiTextField")).Text = "XXX";
((GuiTextField)Session.ActiveWindow.FindByName("RSYST-BNAME", "GuiTextField")).Text = "XXXXXXXX";
((GuiTextField)Session.ActiveWindow.FindByName("RSYST-BCODE", "GuiPasswordField")).Text = "XXXXXXXX";
((GuiTextField)Session.ActiveWindow.FindByName("RSYST-LANGU", "GuiTextField")).Text = "XX";
}
I am getting an error when I run this code saying "...connection broken". How can I be sure of the correct connection string to use? Where can I find the connection string that I use to connect to SAP? Or is there a better way than using the OpenConnectionByConnectionString() method?
Use this line of code:
Connection = Application.OpenConnectionByConnectionString("/H/IP Address/S/3200", false, true);
H: Host IP Address
S: Host Port Number. Default is 3200. Check yours.
Here is a part of the official documentation that describes the format of the route or connection string:
Definition
A route string describes the stations of a connection required between
two hosts. A route string has the syntax
(/H/host/S/service/W/pass)*
It consists of any number of substrings in the form
/H/host/S/service/W/pass.
Caution: H, S, and W must be uppercase!
Structure
A route string contains a substring for each SAProuter and for the
target server. Each substring contains the information required by
SAProuter to set up a connection in the route: the host name, the port
name, and the password, if one was given.
Syntax for substrings:
/H/ indicates the host name. Note that the host name must be at least two characters long.
/S/ is used for specifying the service (port); it is an optional entry, the default value is 3299
/W/ indicates the password for the connection between the predecessor and successor on the route and is also optional (default
is “”, no password)
Your connection string is the name of the SAP area such as ERP, P56, or DFQ
Connection = Application.OpenConnectionByConnectionString("ERP", false, true);
Use that
The "XXXXXXXXX" argument ("ConnectionString") is actually the description of the connection string that you can locate in the saplogon.ini file, which is at %AppData%\Roaming\SAP\Common

How do I get the Application Name from either SQL Server connection string or the IIS application name using C#?

How do I get the Application Name from an SQL Server connection string in the web.config file.
I want to use to log escalated error messages from a web application into the Windows Event Log.
Maybe there is a better way of doing this, ie using the IIS/Web application name?
Thanks
Mark
What does the connection string look like?
DbConnectionStringBuilder is good for parsing and inspecting connection-string values by key:
DbConnectionStringBuilder db = new DbConnectionStringBuilder();
db.ConnectionString = connectionString;
Console.WriteLine(db["Application Name"]);
otherwise you can get various details from the http server variables.
SqlConnectionStringBuilder is also useful if you are using SQL Server:
SqlConnectionStringBuilder sc = new SqlConnectionStringBuilder(connectionString);
string applicationName = sc.ApplicationName;

Categories