This question already has an answer here:
Missing library to reference OLEDB connection types
(1 answer)
Closed 8 years ago.
I cannot seem to connect to the database connection i have set up in the SSIS package inside a script task. Here is my code it is a OleDB connection.
public void Main()
{
// TODO: Add your code here
OleDbConnection myOleDbConnection = new OleDbConnection();
myOleDbConnection = (OleDbConnection)(Dts.Connections["Connection"].AcquireConnection(Dts.Transaction) as OleDbConnection);
MessageBox.Show(myOleDbConnection.ConnectionString, "OleDB Connection");
Dts.TaskResult = (int)ScriptResults.Success;
}
I get the following exception
AcquireConnection() method of the connection manager returns a unmanaged Object, in your case of Oledb it returns native COM object. so use Ado.net connection if possible in your scenario.
If you want to stick to Oledb then here is a workaround.
ConnectionManager cm = Dts.Connections["Connection"];
IDTSConnectionManagerDatabaseParameters100 cmParams = cm.InnerObject as
IDTSConnectionManagerDatabaseParameters100;
OleDbConnection myOleDbConnection = cmParams.GetConnectionForSchema() as OleDbConnection;
Related
I am using dremio to query a large amount of data and it is working very fine. It has rest API to fetch data but the only constraint is that it can give 500 records as a result. In java, Dremio community has given jdbc connection string but our project is in .net or c# so we need the connection string to fetch the massive amount of data from dremio. If the connection string is not there for C# then can anyone suggest us how do we use JDBC connection string in C#.
Drill and Dremio have ODBC interface for that purpose, see:
https://drill.apache.org/docs/configuring-odbc/
https://docs.dremio.com/drivers/dremio-odbc-driver.html
So you can setup up your C# project to use ODBC connection string instead of JDBC:
https://support.office.com/en-us/article/connect-to-an-odbc-source-49b0cf4d-ef78-4ad1-b224-39091c067953
or programmatically:
static private void InsertRow(string connectionString)
{
string queryString =
"INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')";
OdbcCommand command = new OdbcCommand(queryString);
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
command.Connection = connection;
connection.Open();
command.ExecuteNonQuery();
// The connection is automatically closed at
// the end of the Using block.
}
}
where connection string examples:
DRIVER=MapR Drill ODBC
Driver;AdvancedProperties={HandshakeTimeout=0;QueryTimeout=0;TimestampTZDisplayTimezone=utc;ExcludedSchemas=sys,INFORMATION_SCHEMA;};Catalog=DRILL;Schema=hivestg;ConnectionType=Direct;Host=192.168.202.147;Port=31010
DRIVER=MapR Drill ODBC
Driver;AdvancedProperties={HandshakeTimeout=0;QueryTimeout=0;TimestampTZDisplayTimezone=utc;ExcludedSchemas=sys,
INFORMATION_SCHEMA;};Catalog=DRILL;Schema=;ConnectionType=ZooKeeper;ZKQuorum=192.168.39.43:5181;ZKClusterID=drillbits1
I use something like this for my application
MySqlConnection cnn = new MySqlConnection("Server=myServerAddress;" +
"Database=myDataBase;" +
"Uid=myUsername;" +
"Pwd=myPassword;");
And this changes everytime because we deploy databases with our application.
It works fine. I type in using(new connection(cnn)){ query... } and go.
And I've got it working with a dataset using a connection defined in the windows ODBC datasouce administrator.
But I'm curious, is there a way to use visual studio's dataset items using the my local test db and then change the connection of the dataset at runtime? Even better, can I use c# to programmatically add the ODBC data source at runtime?
Usually a connection string is loaded from the application exe.config file present in the same folder of the application. This connection string could be defined using the Settings tab in the project properties.
Right click on Properties of your project
Select the Settings tab (confirm the creation if you have no
settings)
Click on the ComboBox in the column type and select Connection String
Give a symbolic name to your connection
Type the connection string in the Value column (Examples at
connectionstrings.com)
Now in your project files you should have the file app.config (that becomes yourapp.exe.config) where there is a section like this
<configuration>
<connectionStrings>
<add name="MyAppConnection"
connectionString="Server=myServerAddress;Database=myDB;Uid=user;Pwd=pass;" />
</connectionStrings>
</configuration
At this point you read it in the program using
string conString = ConfigurationManager
.ConnectionStrings["MyAppConnection"]
.ConnectionString;
Instead in a dynamic situation where you want to build yourself the connection string during runtime (from user inputs, your own configuration files and so on) then you could leverage the functionality of the class MySqlConnectionStringBuilder
MySqlConnectionStringBuilder msb = new MySqlConnectionStringBuilder();
msb.Server = "localhost";
msb.Port = 3306;
msb.UserID = "root";
msb.Password = "xxx";
msb.Database = "test";
MySqlConnection cnn = new MySqlConnection(msb.ConnectionString);
cnn.Open();
Of course, these literal values could be substituted by your own variables.
The documentation of this class is surprising difficult to find. The best docs are the one of the Sql Server equivalent. It is interesting that you could read a static connection string from your config file and then change only the property needed.
string conString = ConfigurationManager
.ConnectionStrings["MyAppConnection"]
.ConnectionString;
MySqlConnectionStringBuilder msb = new MySqlConnectionStringBuilder(conString);
msb.Database = "AnotherDB";
MySqlConnection cnn = new MySqlConnection(msb.ConnectionString);
Application connection string cannot be changed at runtime.
User settings can be changed.
Assuming you are using an application setting-property named "MyConnectionString" which holds the connection string for the entire application.
On your main Program class create a global string:
internal static string Prconnstring;
Create and save this settings.cs file:
namespace MYSOLUTIONORPROJECTNAME.Properties
{
// (Not sure where I found this solution some time ago)
// This class allows you to handle specific events on the settings class:
// The SettingChanging event is raised before a setting's value is changed.
// The PropertyChanged event is raised after a setting's value is changed.
// The SettingsLoaded event is raised after the setting values are loaded.
// The SettingsSaving event is raised before the setting values are saved.
internal sealed partial class Settings
{
public Settings()
{
// // To add event handlers for saving and changing settings, uncomment the lines below:
//
// this.SettingChanging += this.SettingChangingEventHandler;
//
// this.SettingsSaving += this.SettingsSavingEventHandler;
//
}
private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e)
{
// Add code to handle the SettingChangingEvent event here.
}
private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e)
{
// Add code to handle the SettingsSaving event here.
}
public override object this[string propertyName]
{
get
{
if (propertyName == "MyConnectionString")
{
return Program.Prconnstring;
}
else
{
return base[propertyName];
}
}
set
{
base[propertyName] = value;
}
}
}
}
Before calling-opening any object that uses the connection string (examples include Forms that use datasets or other classes that use datasets created on the development enviroment) create your new connection string by any means you think. (Example: You might want to use as user name in the connection string the current user. Create the connection string using the info provided form the environment.)
Program.Prconnstring = thenewruntimeconnectionstring.
Now whenever the application tries to get MyConnectionString (which is hardcoded in the myapplicationname.config and cannot be changed) instead gets the new thenewruntimeconnectionstring you provided to Program.Prconnstring.
Be aware that the development connection string will be available-visible to final user, since it is just a text file. If you do not want this, you can change that file (will be a file named NAMEOFMYAPPLICATION.exe.config) during deployment, since the connection string hardcoded there, will be of no use for the running app. Do not delete it, just change.
Your connection string will be stored in your App.config (or c# equivalent). Say it's called MyConnectionString. Just add My.Settings("MyConnectionString")="[your new connection string]" to your entry point to change to database binding at runtime. E.g:
Public Sub New()
' This call is required by the designer.
InitializeComponent()
My.Settings("MyConnectionString") = "server=remotedb.uk;user id=MainUser;password=2jdi38edhnche73g;database=mainDb;persistsecurityinfo=True;allowuservariables=True;defaultcommandtimeout=480;characterset=utf8mb4"
End Sub
I've been searching for a solution for days now and I still cant seem to find one. I have a problem acquiring a connection in my Script component. I need to query my database to retrieve an Id to be used before I insert it in the
public override void AcquireConnections(object Transaction)
{
connMgr = base.Connections.Connection;
conn = (SqlConnection)connMgr.AcquireConnection(null);
}
I get an exception here.
System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to class type 'System.Data.SqlClient.SqlConnection'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
Any solutions?
For those that want to be able to do this in a Script Component:
Double Click the Script component to open the "Script Transformation Editor"
Click the "Connection Managers" list item.
Add a new Connection Manager. Select an existing ADO.NET connection manager.
Click on the "Script" list item and then the "Edit Script..." button.
You can do something like this inside your script:
using (SqlConnection connection = this.Connections.Connection.AcquireConnection(null) as SqlConnection)
{
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "SELECT [Value] FROM dbo.MyTable";
command.CommandType = CommandType.Text;
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
ProfanityWords.Add(reader.GetValue(0).ToString());
}
}
}
this.Connections.Connection.ReleaseConnection(connection);
}
ADO.NET connection manger should be created and refer into the code to type cast to the SqlConnection. If you dont have the ADO.NET connection in your SSIS pakcage you will get the TypeCast exception. Following steps should be used if you want to use the SqlConnection.
Create the ADO.NET connection.
Use the following line in your code.
var connObj = Dts.Connections["ADO.NETConnectionName"].AcquireConnection(null);
var sqlConn = (SqlConnection)connObj;
Once you done with your SQL connection. Use the following code to Close/ Release your connection.
Dts.Connections["ADO.NETConnectionName"].ReleaseConnection(connObj);
Hope this helps.
This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 7 years ago.
When trying to connect to mysql I always get this error:
java.sql.SQLException: No suitable driver found for localhost test
I already included the mysql-connector.jar in the /WEB-INF/lib in my app. What else do I need to configure to make it work? Do I need to add something in web.xml? I'm not using the appengine.
Here is my code in the server:
package com.mysql.server;
import java.sql.Connection;
import java.sql.DriverManager;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.mysql.client.ConnDb;
public class ConnDbImpl extends RemoteServiceServlet implements ConnDb {
public Connection con;
#Override
public String tryConn() {
try{
String host = "localhost";
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "pwd";
Class.forName(driver).newInstance();
con = DriverManager.getConnection(host+db, user, pass);
return "Connected to Database";
} catch(Exception ex) {
return ex.toString();
}
}
}
You will get this exception when the JDBC URL is not accepted by any of the loaded JDBC drivers as per the Driver#acceptsURL() method. You actually forgot the JDBC driver specific URI prefix. For the MySQL JDBC driver this is jdbc:mysql://. The full connection URL should look like this:
con = DriverManager.getConnection("jdbc:mysql://localhost/test", user, pass);
See also:
Connector/J documentation - Obtaining a connection
I found another cause for this error message. In my case the user simply had no privilege to the database e.g. to the selected table. Dear driver developers, why do you use such misleading error messages? A lot of people have real trouble with this.
For me, it was forgetting to include the MySQLJDBC Driver in the project libraries. DOH!
This was giving that error:
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/lib_db","root","root");
but when I changed that to:
Connection connection =DriverManager.getConnection("jdbc:mysql://localhost/db_name?"+"user=root&password=root");
error was gone
I am having some problems with getting my OSGI programs to recognzie/utilize the mysql jdbc driver.
I have a bundle that is speficcally for entering data into a mysql database. I have copied over all the same methods as in a test program (non-OSGI). I am not able to create a connection suing DriverManager.getConnection().
I have added the driver to the class path andhave tried all the solutions on this site such as using Class.forName(). Possibly I am inputting the wrong string arg into forName().
public void createConn(String URL, String DBName, String username, String password){
try {
Class.forName("mysql-connector-java-5.1.14-bin.jar");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
try {
conn = DriverManager.getConnection(URL + DBName,username,password);
System.out.println("Connection Created");
stmt = conn.createStatement();
System.out.println("Statement Created");
//data = new ApplianceData();
//flag = true;
//this.writeThread = new Thread();
//writeThread.start();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
Can someone tell me the argument they used in Class.forName();
Does anybody have a solution to this problem or encountered this?
Thanks, that took care of the classNotFound Exception. Now I have an error stating that the driver is not suitable. I know OSGI has some issues with drivers etc. Can someone recommend a way to circumvent this?
I have placed the jdbc jar in the java installation bin folders, and in the bin folder of the bundle.
ClassLoader DBHCL = ClassLoader.getSystemClassLoader();
DBHCL.loadClass("com.mysql.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver", true, DBHCL).newInstance();
System.out.println("Class Loaded");
//DriverManager.getDriver("jdbc:mysql://localhost/timedb");
//System.out.println("Driver Gotten");
conn = DriverManager.getConnection(URL + DBName,username,password);
System.out.println("Connection Created");
stmt = conn.createStatement();
System.out.println("Statement Created");
connFlag = true;
Console Output, Error:
osgi> start 7
Data Base Service (MYSQL) Starting
Class Loaded
No suitable driver found for jdbc:mysql://localhost/timedb
Exception in thread "Thread-1" INSERT INTO appliance1...
Does anybody have any insight into this problem?
Thanks
Class.forName(String) takes a fully qualified class name, not a jar file. You should use something like
Class.forName("com.mysql.jdbc.Driver");
i know this has 3 years but i'm answering just in case someone googles it.
so the problem is that the bundle does not see the jar from the classloade, so you need to import its packages in the manifest.
the way i did, is i created a separate bundle containing mysql connector jar.
newProject> plugin from existing jar; then i added all of its packages to "exported packages" in its manifest file. Then in my first bundle i added all the packages of the connector to "imported packages" and it worked.