I am having problem trying to connect to an external database with Microsoft Dynamics AX.
I have configured the dsn, and i can connect with sql server authentication (not active directory, because is in another server), and when i test, it just works.
But when i try to use the dsn in x++ i am sending the correct user and password to the loginProperty, but always return error, it try to do with the active directory user.
This is my code:
LoginProperty loginProperty;
OdbcConnection odbcConnection;
Statement statement;
ResultSet resultSet;
str sql, criteria;
SqlStatementExecutePermission perm;
TLExternalUser tlExternaUser;
TLExternalUserPwd tlExternalUserPwd;
str strConnectionString;
str dsn = "myDsnName";
str dsnUser = "sqlUser";
str dsnUSerPwd = "sqlPWD";
strConnectionString = strfmt("UID=%1;PWD=%2",dsnUser,dsnUSerPwd);
loginProperty = new LoginProperty();
loginProperty.setDSN(dsn);
loginProperty.setDatabase("MyDatabase");
loginProperty.setOther(strConnectionString);
odbcConnection = new OdbcConnection(loginProperty);
if (odbcConnection)
{
info("success");
}
else
{
throw error("Failed to log on to the database through ODBC.");
}
But i am getting this error:
[Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'DOMAIN\ACTIVEDIRECTORYUSER'.
I was looging to pass directly to my loginProperty the username and password, but such methods does not exist.
How can i make it works?
Try changing it to this and removing loginProperty.setDSN(...):
strConnectionString = strfmt("DSN=%1;UID=%2;PWD=%3",dsn, dsnUser,dsnUSerPwd);
loginProperty = new LoginProperty();
loginProperty.setDatabase("MyDatabase");
loginProperty.setOther(strConnectionString);
Edit If your password has special characters in it, you will need to either escape them with a backslash \ or place an # symbol before the quotes to indicate a string literal, like this str myPass = #'MyP#ssw0rd';.
See here https://msdn.microsoft.com/en-us/library/aa889472.aspx
I can not make it works with dsn, so i use server, in this way:
LoginProperty loginProperty;
OdbcConnection odbcConnection;
Statement statement;
ResultSet resultSet;
str sql ;
SqlStatementExecutePermission perm;
TLExternalUser tlExternaUser;
TLExternalUserPwd tlExternalUserPwd;
str strConnectionString;
str dbServer = 'myipServer';
str serverDbUser = 'myUser';
str serverDbUserPwd = 'MyPassword';
strConnectionString = strfmt("UID=%1;PWD=%2",serverDbUser,serverDbUserPwd);
loginProperty = new LoginProperty();
loginProperty.setServer(dbServer);
loginProperty.setDatabase("MyDatabase");
loginProperty.setOther(strConnectionString);
odbcConnection = new OdbcConnection(loginProperty);
I do not understand why with dsn does not work
Related
I created an ASP.Net 4.0 website and make an OLEDB connection with Visual Foxpro 8.0 databse for selecting data from a table. Used code is written below...
string strConnString = "Provider=vfpoledb;Data Source=C:\Users\mohammads\Documents\Visual FoxPro Projects\dbTallowMaster.dbc;";
OleDbConnection connection = new OleDbConnection(strConnString);
connection.Open();
string username = "675";
string password = "675";
string sqlQuery = "SELECT userinfoid, username, password FROM tm_userinfo.dbf WHERE username = \"" + username + "\" AND password = \"" + password + "\"";
OleDbCommand cmd = new OleDbCommand(sqlQuery, connection);
OleDbDataAdapter oOleDbDataAdapter = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
oOleDbDataAdapter.Fill(dt);
this code is working fine in local system but when I hosted this site on Windows 7 IIS 6.0 then connection doesn't open and I get error that "invalid path or file name".
Please suggest me why this code creates problem only when I host it in IIS.
Is there should be some change in datasource of connection string while hosting site in IIS.
A side-note... I would start building your queries using parameterized, especially if coming from the web. VFP uses "?" as a place-holder for parameters so you don't have to explictily wrap the values in quotes.
string sqlQuery = "SELECT userinfoid, username, password "
+ "FROM tm_userinfo.dbf "
+ "WHERE username = ? and password = ?";
OleDbCommand cmd = new OleDbCommand(sqlQuery, connection);
cmd.Parameters.Add("parmUserName", OleDbType.Char).Value = username; // from your string
cmd.Parameters.Add("parmPassword", OleDbType.Char).Value = password; // variables...
OleDbDataAdapter oOleDbDataAdapter = new OleDbDataAdapter(cmd);
Notice the parameters are added in same sequential order as the "?" in the query.
As for connecting, shouldn't it be
"vfpoledb.1" instead of "vfpoledb" in your connection string.
And finally, a consideration, since your data is in a subfolder under "Users", they are typically "restricted" to that user. When running IIS, the user is typically something like
IUSR_{machine name} representing the internet user, and not YOU. So permissions might be your issue.
When I execute this code:
AdomdConnection con = new AdomdConnection("Data Source=MyServer;User ID=MyDomain\\MyUserName;Password=MyPassword");
con.Open();
con.ChangeDatabase("Analysis Services Project1");
I get this exception:
Either the user, MyDomain\MyUserName$, does not have access to the
Analysis Services Project1 database, or the database does not exist.
The database name comes from looking at the server using Microsoft SQL Server Management Studio. If I bring up the properties on the server and go to the security section, my account is listed as a Server administrator. While in management studio, I can see data sources, cubes and execute mdx queries fine.
Why can't do I get this exception in code?
whihc line causes the error?
I imagine you have to infor the catalog on your connection string. Try addind
Catalog=Analysis Services Project1
to your con string.
Also, Analysis Services Project1 seems to be the projects name? Are you sure this is the database name as well?
First
added the reference for Microsoft.AnalysisServices.AdomdClient.dll
strCon = "Data Source=DBserverServerName;Integrated Security=SSPI;Initial Catalog=DBName;";
AdomdConnection con = new AdomdConnection("connectionstring"); // connect DB con.Open(); AdomdCommand cmd = new AdomdCommand("MDX query", con); //query
AdomdDataReader reader = cmd.ExecuteReader(); //Execute query
while (reader.Read()) // read
{
Data dt = new Data(); // custom class
dt.Gender = reader[0].ToString();
dt.Eid = reader[1].ToString();
dt.salary = reader[2].ToString();
data.Add(dt);
}
Hopefully this is not a ServerFault question...
I'm working forward on migrating a project from storing data in XML Serialization to a MySQL database. I'm using the example provided me from a previous question answered yesterday.
Connecting using phpMyAdmin and MySQL Workbench I've created a Stored Procedure called 'sprocOrderSelectSingleItem'. It seems to work well with MySQL for all I can tell. When I run the SHOW CREATE PROCEDURE sprocOrderSelectSingleItem it returns the following:
CREATE DEFINER=username#% PROCEDURE sprocOrderSelectSingleItem(IN orderID INTEGER)
BEGIN SELECT * FROM tblOrders WHERE ID=orderID; END
My cooperative ASP.NET code goes something like this:
public static Order GetItem(int ID)
{
Order objOrder = null;
using (OdbcConnection objConnection = new OdbcConnection(Utils.ApplicationConfiguration.ConnectionString))
{
OdbcCommand objCommand = new OdbcCommand("sprocOrderSelectSingleItem", objConnection);
objCommand.CommandType = CommandType.StoredProcedure;
objCommand.Parameters.AddWithValue("orderID", ID);
objConnection.Open();
using (OdbcDataReader objReader = objCommand.ExecuteReader())
{
if (objReader.Read())
{
objOrder = FillDataRecord(objReader);
}
objReader.Close();
}
objConnection.Close();
}
return objOrder;
}
When I view the page I get the following error message:
ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-5.0.77]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sprocOrderSelectSingleItem' at line 1
Really not catching on to what could be missing or going wrong. Are there any additional tests I should/could be running to confirm things are working on the MySQL side? Am I missing a step to pass the Stored Procedure call correctly in ASP.NET? The code breaks at the line of:
using (OdbcDataReader objReader = objCommand.ExecuteReader())
Replacing the line of
OdbcCommand objCommand = new OdbcCommand("sprocOrderSelectSingleItem", objConnection);
with this instead
OdbcCommand objCommand = new OdbcCommand("SELECT * FROM tblOrders WHERE ID=" + ID + ";", objConnection);
and everything works as expected.
Thanks for any help you guys can provide.
Your can run an execute on sprocOrderSelectSingleItem in Mysql directly with the ID parameter.
It will show that your StoredProc run correctly.
Here is a sample code in C# that call a stored proc.
OdbcCommand salesCMD = new OdbcCommand("{ CALL SalesByCategory(?) }", nwindConn);
salesCMD.CommandType = CommandType.StoredProcedure;
OdbcParameter myParm = salesCMD.Parameters.Add("#CategoryName", OdbcType.VarChar, 15);
myParm.Value = "Beverages";
OdbcDataReader myReader = salesCMD.ExecuteReader();
Look at the "Call" in the OdbcCommand and the "?" for the parameter that is later supplied with a value.
Can you try something like below:
OdbcCommand cmd = new OdbcCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "{call LoadCustCliOrders(?,?,?,?)}";
cmd.Parameters.Add("CUST_ID",OdbcType.Int);
cmd.Parameters.Add("CLIENT_ID",OdbcType.Int);
cmd.Parameters.Add("DATE_FROM",OdbcType.Date);
cmd.Parameters.Add("DATE_TO",OdbcType.Date);
...
cmd.Parameters["CUST_ID"].Value = _CustId;
cmd.Parameters["CLIENT_ID"].Value = _ClientId;
cmd.Parameters["DATE_FROM"].Value = _DateFrom;
cmd.Parameters["DATE_TO"].Value = _DateTo;
cmd.ExecuteReader
Are you sure that you are using the same username or user with the same access privileges.
I think you need to add the word "CALL" before the stored proc.
It should be CALL sprocOrderSelectSingleItem and try.
i have the info below from the oracle DBA and want to connect to oracle from a .net application. i just got done installing the oracle tools/ drivers for windows/ .net and now want to get a console app to connect tot he oracle DB and extract data from oracle into SQL server.
another solution would be to have SQL server pull from oracle all the records in the bugs table. i have no clue what the oracle connection string is and have tried to create a system DSN but failed at that prior to turning to the SO gurus...
ORACLE SQL user name is ‘USER_dev’,
password is ‘welcome’.
Connection string is
‘jdbc:mercury:oracle://qct-ds2-p.apps.com:1139;sid=QCTRP1’
i got lucky and found a simple solution that is all in .net and requires nothing more than this syntax by way of a connection string. all the .ora stuff is presented in the connection string and works well.
static void getData()
{
string connectionString = GetConnectionString();
using (OracleConnection connection = new OracleConnection())
{
connection.ConnectionString = connectionString;
connection.Open();
//Console.WriteLine("State: {0}", connection.State);
//Console.WriteLine("ConnectionString: {0}", connection.ConnectionString);
OracleCommand command = connection.CreateCommand();
string sql = "SELECT * FROM BUG";
//string sql = "SELECT table_name FROM user_tables";
command.CommandText = sql;
OracleDataReader reader = command.ExecuteReader();
while (reader.Read())
{
//string myField = (string)reader["Project"];
string myField = (string)reader[0];
Console.WriteLine(myField);
}
}
}
static private string GetConnectionString()
{
return "User Id=USER_dev;Password=welcome;Data Source=(DESCRIPTION=" +
"(ADDRESS=(PROTOCOL=TCP)(HOST=111.123.479.24)(PORT=1139))" +
"(CONNECT_DATA=(SID=QCTRP1)));";
}
go into your ORACLE_HOME\network\admin directory. Create a file called tnsnames.ora if it is not there already. Add this into it:
QCTRP1.WORLD =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = qct-ds2-p.apps.com)(PORT = 1139))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = QCTRP1)
)
)
I'm making some assumptions here...
does qct-ds2-p.apps.com resolve?
looks like the DBA set it up the listener on port 1139... default is normally 1521 so you'll need to confirm.
You can then set up a connection string as follows:
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci8:#QCTRP1.WORLD", "dev", "welcome");
now that you have the tnsnames created from #erbsock, you will need to get the .net side of
things working. Oracle has several tutorials available to help you in this matter.
When you install the Oracle ODP (in the link you will see samples as well as the downloads) drivers you will get a directory created in the
%oracle_home%\odp.net\samples\4\DataSet\DSPopulate\src
(this appears to be the same example as located # http://www.oracle.com/technology/sample_code/tech/windows/odpnet/DSPopulate/ViewProducts.cs.html)
as an initial test I would recommend just changing around the id/passwrd/sql/etc and trying it out.
it is not that fundamentally different than using System.Data; but there are a few gotchas and remember that each computer will need to have ODP/Oracle client installed to have them work with the ora connection.
if you have any problems post the exact error msg that is displayed as well as a snippet of your .net code that establishes the connection, does the query, and where it is breaking
I`m having problems between two servers, wich use differente odbc dsn.
My apps work great but crystal reports uses the original odbc connection, how can I fix this?
I'm thinking of using the same connection string in the web.config, but I don't know how.
found this but is too confusing for me
thanks,
You can't use a connection string in the same way you can with an ado.net connection. However, you can certainly use values from the web.config to specify the connection info. Create an instance of the ConnectionInfo class and set the ServerName, DatabaseName, UserID, and Password properties. Then attach it to each of the tables in the report:
ConnectionInfo reportConnectionInfo = new ConnectionInfo();
reportConnectionInfo.ServerName = ConfigurationManager.AppSetting["ServerName"];
reportConnectionInfo.DatabaseName = ...;
reportConnectionInfo.UserID = ...;
reportConnectionInfo.Password = ...;
foreach (Table table in reportDocument.Database.Tables) {
table.LogOnInfo.ConnectionInfo = reportConnectionInfo;
}
If you try to use this suggestion, beware of my sloppy typing...
You can use the connection string.
You have to create an instance of the database provider ConnectionStringBuilder passing in the Connection String pulled from a config file. Here is an example in VB.
Dim connString As String = ConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString
Dim connStringBuilder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder(connString)
Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()
myConnectionInfo.DatabaseName = connectionStringBuilder.InitialCatalog
myConnectionInfo.UserID = connectionStringBuilder.UserID
myConnectionInfo.Password = connectionStringBuilder.Password
myConnectionInfo.ServerName = connectionStringBuilder.DataSource