I am using Visual Studio 2017 and SQL Server 2017 and the last version of Crystal Reports. I then create a stored procedure in SQL Server and a new report in Visual Studio (C#) using the code to get the connection info from the app.config.
When I try to make the program work in PC client, I have the problem failed to open connection temp - the error is illustrated in the screenshot below.
This is the code for the connection info:
SaleReportCrystal aLL_PUSH_STUDENTS = new SaleReportCrystal();
System.Data.Common.DbConnectionStringBuilder builder = new System.Data.Common.DbConnectionStringBuilder();
builder.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString; ;
string server = builder["Data Source"] as string;
string database = builder["Initial Catalog"] as string;
string UserID = builder["User ID"] as string;
string password1 = builder["Password"] as string;
aLL_PUSH_STUDENTS.SetDatabaseLogon(UserID, password1, server, database);
ReportPrint studentInforamtionRep = new ReportPrint();
studentInforamtionRep.crystalReportViewer1.ReportSource = aLL_PUSH_STUDENTS;
studentInforamtionRep.ShowDialog();
Error:
That error code (17) means: "Server does not exist or access denied."
Hopefully, that should point you in the right direction.
Related
I tried to connect to the Azure MySQL database using MySQL Workbench and MySQL Shell and it works fine. Now I am trying to connect using following C# code:
var connStringBuilder = new MySqlConnectionStringBuilder
{
Server = "creatur-db.mysql.database.azure.com",
Database = "test",
UserID = "creatur_db_main#creatur-db",
Password = "{my_password}",
SslMode = MySqlSslMode.Preferred,
};
using (MySqlConnection connection = new MySqlConnection(connStringBuilder.ToString()))
{
connection.Open();
connection.Close();
}
Here I replaced {my_password} with password to the database and it gives me an exception inside Open method:
MySql.Data.MySqlClient.MySqlException: 'Authentication to host
'creatur-db.mysql.database.azure.com' for user
'creatur_db_main#creatur-db' using method 'mysql_native_password'
failed with message: The connection string may not be right. Please
visit portal for references.
I also tried different connection strings:
Server=creatur-db.mysql.database.azure.com; Port=3306; Database=test; Uid=creatur_db_main#creatur-db; Pwd={my_password}; SslMode=Preferred
and
Database=test; Data Source=creatur-db.mysql.database.azure.com; User Id=creatur_db_main#creatur-db; Password={my_password}
But none of them worked.
The same exception occurs when I create new connection using Server Explorer in Visual Studio 2013. It seems the error has something to do with .NET Connector. I tried to use different versions of MySQL.Data.dll, .NET Framework and Visual Studio but no luck.
I just created a console application using VS2017 I used
Nuget package MySql.Data and .Net Framework 4.6.1
It works perfectly here What I did.
After Creating the MySQL server I used the CloudShell to connect to the SERVER (not a database).
Using this code:
mysql --host franktest.mysql.database.azure.com --user frank#franktest -p
I got an error
ERROR 9000 (HY000): Client with IP address '40.76.202.47' is not allowed to connect to this MySQL server.
So I add that IP and at the same time my IP from where I'm connected.
So once the IP were saved. I executed the previous command, and this time it worked perfectly. I created a database named: frankdemo using this command:
CREATE DATABASE frankdemo;
Then, back in VisualStudio used this code as my Main method, copied from the documentation.
static void Main(string[] args)
{
var builder = new MySqlConnectionStringBuilder
{
Server = "franktest.mysql.database.azure.com",
Database = "frankdemo",
UserID = "frank#franktest",
Password = "gr3enRay14!",
SslMode = MySqlSslMode.Required,
};
using (var conn = new MySqlConnection(builder.ConnectionString))
{
Console.WriteLine("Opening connection");
conn.Open();
using (var command = conn.CreateCommand())
{
command.CommandText = "DROP TABLE IF EXISTS inventory;";
command.ExecuteNonQuery();
Console.WriteLine("Finished dropping table (if existed)");
command.CommandText = "CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);";
command.ExecuteNonQuery();
Console.WriteLine("Finished creating table");
command.CommandText = #"INSERT INTO inventory (name, quantity) VALUES (#name1, #quantity1),
(#name2, #quantity2), (#name3, #quantity3);";
command.Parameters.AddWithValue("#name1", "banana");
command.Parameters.AddWithValue("#quantity1", 150);
command.Parameters.AddWithValue("#name2", "orange");
command.Parameters.AddWithValue("#quantity2", 154);
command.Parameters.AddWithValue("#name3", "apple");
command.Parameters.AddWithValue("#quantity3", 100);
int rowCount = command.ExecuteNonQuery();
Console.WriteLine(String.Format("Number of rows inserted={0}", rowCount));
}
// connection will be closed by the 'using' block
Console.WriteLine("Closing connection");
}
Console.WriteLine("Press RETURN to exit");
Console.ReadLine();
}
Runed it and it works
The documentation I'm referring to is:
Create an Azure Database for MySQL server by using the Azure portal
Azure Database for MySQL: Use .NET (C#) to connect and query data
I am trying to access an Oracle database (version 10.2.0.4.0) using the following code but an "ORA-01005: Null password given; logon denied" exception is raised by the connection when it's open method is called.
var connBuilder = new OracleConnectionStringBuilder();
connBuilder.DataSource = "(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = MyHost.Address)(PORT = ####)) )(CONNECT_DATA =(SERVICE_NAME = MyService)))";
connBuilder.UserID = "validUserId";
connBuilder.Password = "validPassword";
connBuilder.PersistSecurityInfo = true;
var connString = connBuilder.ToString();
using (var con = new OracleConnection(connString))
{
con.Open();
}
If I change the username then I receive the following instead; "ORA-01017: invalid username/password; logon denied" and this is also the case if I change the open call on the connection with con.OpenWithNewPassword("validPassword");
If I try with the deprecated Oracle client it connects with no problems:
using (var depCon = new System.Data.OracleClient.OracleConnection
("Data Source=MyHost.Address:####/MyService;Persist Security Info=True;
User ID=validUsername;Password=validPassword;Unicode=True"))
{
depCon.Open();
}
I'd (obviously) like to use the latest Odp.Net drivers but can't seem to get past this issue. Has anybody got any ideas?
Take a look at this thread for an issue regarding FIPS compliance:
https://community.oracle.com/thread/2557592?start=0&tstart=0
Also:
Oracle.ManagedDataAccess and ORA-01017: invalid username/password; logon denied
Does it work when you do it like this:
var connBuilder = new Common.DbConnectionStringBuilder();
connBuilder.Add("Data Source", "(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = MyHost.Address)(PORT = ####)) )(CONNECT_DATA =(SERVICE_NAME = MyService)))";
connBuilder.Add("User Id", "validUserId");
connBuilder.Add("Password", "validPassword");
Which version of ODP.NET do you use? There are known issues when you connect to a "new" Oracle database with case-sensitive passwords using an "old" ODP.NET provider, see here: https://community.oracle.com/message/2198228
In order to verify run this command on your database:
ALTER SYSTEM SET SEC_CASE_SENSITIVE_LOGON = FALSE;
The issue with case sensitivity and the ODP.Net drivers and different DB versions should be a simple fix. Enclose your connectionstring password in quotes(") such as Password=\"password\"; and this should keep the password case
I am trying to programmatically create a new database using SMO in C#. For this project, I do not want the .mdf/.ldf files placed in the default folder
"C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQL2008R2\MSSQL\DATA". I have not found anything on the web that tells how to modify the setting for the file location.
I get a failed operation exception when I run the following code:
Server srv = new Server(serverName.Text);
var db = new Database(srv, dbName.Text);
db.Create();
DataFile df = new DataFile(db.FileGroups["PRIMARY"],
dbName.Text, pathText.Text + dbName.Text + "_data.mdf");
df.Create();
LogFile lf = new LogFile(db, "Log01", pathText.Text + dbName.Text + "_log.ldf");
lf.Create();
The exception occurs at the df.Create(); line.
Any ideas?
I think this one answers the question for you.
Use SMO to Change SQL Server Database Default Locations
TTRider's Answer was:
You need to add information about Data and Log files explicitly:
TTRider's answer in the linked question points in the right direction, but is incomplete. Using some additional info from this post I was able to get it working (tested with SMO 2016 libraries).
private void CreateDatabase(string connectionString, string databaseName, string dataFilePath)
{
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
ServerConnection serverConnection = new ServerConnection(sqlConnection);
Server sqlServer = new Server(serverConnection);
Database smoDatabase = new Database(sqlServer, databaseName);
string dataFileName = string.Format("{0}_Data", databaseName);
string dataFileFullPath = Path.ChangeExtension(Path.Combine(dataFilePath, dataFileName), ".mdf");
string logFileName = string.Format("{0}_Log", databaseName);
string logFileFullPath = Path.ChangeExtension(Path.Combine(dataFilePath, logFileName), ".ldf");
FileGroup fileGroup = new FileGroup(smoDatabase, "PRIMARY");
smoDatabase.FileGroups.Add(fileGroup);
DataFile dataFile = new DataFile(fileGroup, dataFileName, dataFileFullPath);
dataFile.IsPrimaryFile = true;
fileGroup.Files.Add(dataFile);
LogFile logFile = new LogFile(smoDatabase, logFileName, logFileFullPath);
smoDatabase.LogFiles.Add(logFile);
smoDatabase.Create();
serverConnection.Disconnect();
}
}
I have goggled alot but did't find any solution.
I am using Visual studio 2012 with My Sql as Database and Crystal reports.
I have deployed web application on WINDOWS SERVER 2008 and when i access from remote it give me following error message, as given in following screen.
i am using the following code
string databaseName = "hr";
string serverName = "192.168.137.6";
string userID = "userID";
string pass = "xxxxxxxxxxx";
protected void btn_search_Click(object sender, EventArgs e)
{
CrystalReportViewer1.Visible = true;
ReportDocument reportDocument = new ReportDocument();
string reportPath = Server.MapPath(#"~/GeneralEmpReports/test.rpt");
reportDocument.Load(reportPath);
reportDocument.SetParameterValue("D", tbx_ddoCode.Text.ToUpper());
reportDocument.SetDatabaseLogon(userID, pass, serverName, databaseName);
CrystalReportViewer1.ReportSource = reportDocument;
}
Here i want to add that only Stored Procedures gives me the above error, if i made a report from Tables then no error and execute perfectly. I have checked stored procedure through Navicat tool (GUI Administrative tool for mysql) , it works fine.
I have added the following connection on server through control Panel==>Administrative tools ==>Data Source (ODBC) as shown in following figure.
After searching a lot, i came to know that i am doing mistake in Server Name,
Server Name should be DSN name not IP address. Create System DSN name on your production server same as given in development computer as in following.
Data source Name : DSN_Name
TCP/IP Server : In development PC give it blank it means localhost but in development Server give IP address.
User: UserName
Paswword: Password
Database: Database Name
Following is the code which works fine.
string databaseName = "hr";
string serverName = "hr_domain"; // DSN Name
string userID = "xxxxxxx";
string pass = "xxxxxxxx";
protected void btn_search_Click(object sender, EventArgs e)
{
CrystalReportViewer1.Visible = true;
ReportDocument reportDocument = new ReportDocument();
string reportPath = Server.MapPath(#"~/GeneralEmpReports/test.rpt");
reportDocument.Load(reportPath);
reportDocument.SetDatabaseLogon(userID, pass, serverName, databaseName);
reportDocument.SetParameterValue("D", tbx_ddoCode.Text.ToUpper());
CrystalReportViewer1.ReportSource = reportDocument;
}
I have a DB2 expresss in my machine and I am able to query from the database using command window (after following two commands):
set DB2INSTANCE=db2inst1
db2 connect to tims user
Now, when I try to connect to the database from a C# console application, I am getting following errors with different connection strings.
Attempt 1
string connectionString = #"Provider = IBMDADB2; Database = TIMS; Hostname = localhost; CurrentSchema=db2inst1; ";
SQL1032N No start database manager command was issued. SQLSTATE=57019
Attempt 2
string connectionString = #"Provider = IBMDADB2; Database = TIMS; CurrentSchema=db2inst1; ";
SQL1031N The database directory cannot be found on the indicated file system. SQLSTATE=58031
What should be the correct connection string for this scenario?
CODE
string connectionString = #"Provider = IBMDADB2; Database = TIMS; Hostname = localhost; CurrentSchema=db2inst1; ";
OleDbConnection myConnection = new OleDbConnection();
myConnection.ConnectionString = connectionString;
myConnection.Open();
Do you have multiple DB2 instances running on your machine? You can get a list of instances that exist by executing the db2ilist command.
If you have to execute the set DB2INSTANCE=db2inst1 statement when you open a DB2 Command Window in order to connect to the TIMS database with the db2 connect to TIMS command, then you need to ensure that the environment for your C# application is configured the same way.
You can do this in a number of ways:
by setting the DB2INSTANCE environment variable before starting your application
Change the default DB2 instance on your machine by using the command db2set -g DB2INSTDEF=db2inst1 (** see note below)
Use a TCPIP connection string (as described by #Bhaarat) so that your application does not depend on the database catalog for the default instance
Note: Before changing DB2INSTDEF you may want to see what the current value is, by executing the command db2set -all and looking for DB2INSTDEF in the output. Also note that changing the default instance may affect other applications that run on your machine.
refer this url http://www.c-sharpcorner.com/uploadfile/nipuntomar/connection-strings-for-ibm-db2/
your connection string should be something like this
Provider=IBMDADB2;Database=urDataBase;Hostname=urServerAddress;Protocol=TCPIP;Port=50000;
Uid=urUsername;Pwd=urPassword;
in more you can refer this too
http://www.codeproject.com/Articles/4870/Connect-to-DB2-from-Microsoft-NET
My DB2 insatnce name is "db2inst1" and it was working fine when I used DB2 command window.
Now I made following settings and it is working fine now. :-)
Created a port in the C:\Windows\System32\drivers\etc\services file (db2c_db2inst1 50010/tcp)
Set the “TCP/IP Service Name” ( = db2c_db2inst1”) for the instance. Verified using “db2 get dbm cfg” command
Updated the environment variable DB2INSTANCE with the value “db2inst1”
Restarted the machine
Started the instance
ConnectionString
"Provider = IBMDADB2; Database = TIMS; Hostname = localhost; Protocol = TCPIP; Port = 50010; Uid = myUserID; Pwd = myPassword";
CODE
string queryString = "SELECT * FROM DBATABC";
try
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
if (!reader.IsDBNull(0))
{
string companyCode = reader.GetString(0).ToString();
}
}
}
reader.Close();
}
}
Note: Try to create DSN for ODBC and use ODBC connection in a sample SSIS package. This will help in resolving OLEDB connection issues also (that are common to both)