I've been having some difficulty connecting to an iseries DB2 database from a .net 4.0 application I'm developing. I've been trying to use the IBM.Data.DB2.dll library to connect to it using the following code;
String connectionString = "Database=[DBName];UserID=[UserID];Password=[Password];Server=[ServerName]";
connection = new DB2Connection(connectionString);
connection.Open();
When the connection.Open() command is run I receive the following error:
ERROR [58009] [IBM] SQL30020N Execution of the command or SQL
statement failed because of a syntax error in the communication data
stream that will affect the successful execution of subsequent
commands and SQL statements: Reason Code "0x124C"("0100")"".
SQLSTATE=58009
Does anyone know of another way of connecting to this kind of database in .net?
This works for me:
class Program
{
static void Main(string[] args)
{
string connString = "DataSource=SYSTEM;UserID=USER;Password=PASSWORD";
iDB2Connection conn = new iDB2Connection(connString);
conn.Open();
string cmdString = "CRTPF FILE(TESTLIB/TESTNET) RCDLEN(100)";
string cmdText = "CALL QSYS.QCMDEXC('" + cmdString + "', " + cmdString.Length.ToString("0000000000") + ".00000" + ")";
iDB2Command cmd = new iDB2Command(cmdText, conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
}
}
We are using Data Direct DB2 driver from Progress. This supports Entity Framework. May be you can try with that. You can download the evaluation version online (progress.com)
Related
I trying to connect to an database in a little console application it works. but now i am making a restfull api for school but i am stuck on a runtime error:
System.TypeInitializationException' in Oracle.DataAccess.dll
my code end exception in my output
I tried a lot. checked if the version are correct it is al set up good i think. i am using
[HttpPost("InsertKlok")]
public IActionResult InsertKlok(Klok klkregd)
{
string constr = "Data Source=MydataSource;User Id=MyuserId;Password=Mypassword;";
var query = "Insert INTO KLOK(KLOK_ID, REDEN,INUIT) VALUES(" + klkregd.Klok_Id + ",'" + klkregd.Reden + "','" + klkregd.InUit + "')";
try
{
OracleConnection conn = new OracleConnection(constr);
OracleCommand command = new OracleCommand(query, conn);
conn.Open();
command.ExecuteNonQuery();
Console.WriteLine("Record inserted");
}
catch (Exception e)
{
Console.WriteLine(e);
}
return Ok(klkregd);
}
[Route("klok/")]
[HttpPost("AddKlok")]
public JsonResult AddKlok(Klok klkregd)
{
Console.WriteLine("In registerStudent");
KlokRegistratieAntwoord klkregdreply = new KlokRegistratieAntwoord();
KlokRegistratie.getInstance().Add(klkregd);
klkregdreply.Klok_Id = klkregdreply.Klok_Id;
klkregdreply.Reden = klkregdreply.Reden;
klkregdreply.InUit = klkregdreply.InUit;
return Json(klkregdreply);
}
}
}
`
In my console application i have same code and file for oracle connection but its not working here pls can someone help me
Since you use .net core you can try Oracle.ManagedDataAccess.Core instead of
Oracle.DataAccess; quotation:
Oracle Data Provider for .NET (ODP.NET) Core is an ADO.NET driver that
provides fast data access from Microsoft .NET Core clients to Oracle
databases. ODP.NET Core consists of a single 100% managed code
dynamic-link library
(bold is mine). You can get the assembly via Nuget:
https://www.nuget.org/packages/Oracle.ManagedDataAccess.Core/
Please, have a look at the tutorial from Oracle as well:
https://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/ODPNET_Core_get_started/index.html
I am trying to execute a command with OdbcCommand in c# but it seems i can't even open the connection. This is the data I got to create the Connection String
Server: APPRDNETEZZA (192.168.0.64)
Web Server: beamprdwb3
ODBC DSN Name (64bit): NZ_FUTUREBRANDS
User: MSTR_ADMIN
Port: 5480
This is my code
string connetionString = null;
OdbcConnection cnn;
OdbcCommand cmd;
string sql = null;
connetionString = "Driver={NetezzaSQL}; servername=APPRDNETEZZA ;
database=NZ_FUTUREBRANDS; port=5480; username=MSTR_ADMIN;
password=mstr17Uz1%4;";
sql = "CREATE EXTERNAL TABLE X_STORE_GROUP_GUID_12345(" +
"STORE_ID VARCHAR(10)," +
"STORE_NAME VARCHAR(50)," +
"USERID Varchar(255)," +
"import_guid Varchar(255), )" +
"USING(" +"DATAOBJECT('/apnas01/vol2.nfs.Data/ap_prod/data/store_upload/scripts/12345_guid_12345')" + "logDir '/apps/ap_prod/log'" + " delimiter ','" + " "; ";
cnn = new OdbcConnection(connetionString);
try
{
cnn.Open();
Console.WriteLine("Connection Opened ");
cmd = new OdbcCommand(sql, cnn);
cmd.ExecuteNonQuery();
cmd.Dispose();
cnn.Close();
result = "Executed sucessfully";
}
catch (Exception ex)
{
result = "Error" + ex.InnerException.ToString();
}
The error happens when it tries to open the connection. While debugging it gave me this inner exception
[ODBC Driver Manager] Data source name not found and no default driver
specified
I have the Odbc driver installed in my machine. I dont know it this is happening cuz the connection string is in bad format. I have spent many hours trying to figure it out why doesnt work. Appreciate any kind of help. Thanks
Here is an example Netezza ODBC connection string:
https://www.connectionstrings.com/netezzasql-odbc-driver/
Driver={NetezzaSQL};servername=myServerAddress;port=myPortNumber;
database=myDataBase;username=myUsername;password=myPassword;
Your connection string basically looks the same:
connetionString = "Driver={NetezzaSQL}; servername=APPRDNETEZZA ;
database=NZ_FUTUREBRANDS; port=5480; username=MSTR_ADMIN;
password=xyz;";
SUGGESTIONS:
Make the entire connection string (everything inside the quotes) ONE LINE. Don't break the string into separate lines.
Or use "+" (like you did with your SQL string).
Copy the driver (nsqlodbc.dll?) into the same directory as your .exe assembly (path issue?)
Try both the 32-bit and 64-bit drivers (CPU platform issue?)
Definitely post back what you find!
I know that it is an easy problem but I can't find the error. I want to save data in my database. I create a database named "Examen" with Microsoft SQL Server 2008 then in my app in visual studio I make the connection string like this :
string connectionstring = #"Data Source=.\sqlexpress;Initial Catalog=Examen;Integrated Security=True";
Then I use this code to insert data into a "Test" table:
MySqlConnection connection = new MySqlConnection(connectionstring);
MySqlCommand cmd;
connection.Open();
try
{
cmd = connection.CreateCommand();
cmd.CommandText = "Insert into Examen.Test (nom,prenom) values (" + txbnom.Text + "," + txbprenom.Text + ") ";
cmd.ExecuteNonQuery();
MessageBox.Show("ok");
}
catch (Exception)
{
throw;
}
finally
{
if(connection.State == ConnectionState.Open)
{
connection.Close();
}
}
When running this code i had an error when openning the connection
Unable to connect to any of the specified MySQL hosts.
You are mixing MySQL and MSSQL.
Are you sure you want to connect to a MySQL server? Use http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection(v=vs.110).aspx if you would like to connect to MSSQL.
Also you should make yourself familiar with SQL injection
I have created a SQL Server Compact Database (.sdf file) and I want to be connected to it for do some insert , delete ... .
This is my creation code for it:
if (File.Exists(dbfilename))
File.Delete(dbfilename);
string connectionString = "Data Source=" + dbfilename + """;
SqlCeEngine engine = new SqlCeEngine(connectionString);
engine.CreateDatabase();
engine.Dispose();
SqlCeConnection conn = null;
try
{
conn = new SqlCeConnection(connectionString);
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "CREATE TABLE Contacts (ID uniqueidentifire, Address ntext)";
cmd.ExecuteNonQuery();
}
catch { }
finally
{
conn.Close();
}
Is it true?
How can I connect to it?
That connection string is broken.
"
is not a valid entity where you are trying to use it. Fix your connection string.
Also, you will need to associate the command with the connection, either in the constructor or after the fact.
Please read through an example such as this one, which was the first google result for "connect sql compact example c#":
I read that I have to download this http://dev.mysql.com/downloads/mirror.php?id=13427#mirrors, but it says that I can't install it because I need .NET Framework. I already have 4.0?!
Use this link, it will work if you have VS.NET 2010 http://dev.mysql.com/downloads/connector/net/
Installer checking for .Net 3.5 or 2.0 mabey =
You should use MySQL Connector/Net 6.3.5 available at mentioned location (http://dev.mysql.com/downloads/connector/net/)
I pieced this together by copy/paste from an existing project then sanitizing it...so it's not been compiled and tested, but you get the idea. So here's some sample code to get you started:
using MySql.Data.Types;
using MySql.Data.MySqlClient;
private void Function()
{
//Set up connection, SqlHost/etc are classwide and declared elsewhere:
MySql connection = new MySqlConnection("SERVER=" + SqlHost + ";DATABASE=" + DatabaseName + ";UID=" + user + ";PASSWORD=" + password + ";pooling=false");
//Setup query:
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "your query here";
//Connect to relation system and execute query:
connection.Open();
Reader = command.ExecuteReader();
while(Reader.Read())
{
MessageBox.Show("here's a row from the query response: " + Reader[0].ToString());
}
//Clean up:
connection.Close();
Reader.Close();
}