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!
Related
How to restore SQL Server backup using C#?
try
{
string test = "D:\\backupdb\\05012017_130700.Bak";
sqlcmd = new SqlCommand("Restore database EmpolyeeTable from disk='D:\\backupdb\\05012017_130700.Bak'", con);
sqlcmd.ExecuteNonQuery();
Response.Write("restore database successfully");
}
catch (Exception ex)
{
Response.Write("Error During backup database!");
}
Quite weird requerement you have right there. I´ve never heard of someone restoring a database backup from a webpage, and as #Alex K. told, it would be quite rare that the user that uses your web application have the required previleges.
Anyway, supposing that everything told above is OK, the code to restore a backup would be this:
Use this:
using System.Data;
using System.Data.SqlClient;
Code:
private void TakeBackup()
{
var conn = new SqlConnection("Data Source=" + Server + ";Initial Catalog=" + Database + ";User Id=" + Username + ";Password=" + Password + ";");
try
{
conn.Open();
SqlCommand command = conn.CreateCommand();
command.CommandText = "RESTORE DATABASE AdventureWorks FROM DISK = 'C:\AdventureWorks.BAK' WITH REPLACE GO";
command.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
conn.Dispose();
conn.Close();
}
}
This is going to work specifically for the problem you posted. Be sure to set all the parameters of your database server on the connection string, it seems from the comments on your question that you are having communication issues. You have to solve that problems before you do anything. Some tips for that:
Be sure you set all the parameters on connection string the right way
Try to connect using another tool like ODBC so you can test all parameters
Check out SQL Network settings to see if TCP/IP is enabled
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)
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
What could cause the following error for the very first time when the exact same code (that produces this error) has been working perfectly all day today?
--------------------------- A network-related or instance-specific error occurred while establishing a connection to SQL Server. The
server was not found or was not accessible. Verify that the instance
name is correct and that SQL Server is configured to allow remote
connections. (provider: Named Pipes Provider, error: 40 - Could not
open a connection to SQL Server)
--------------------------- OK
The code:
this.Cursor = Cursors.WaitCursor;
try
{
// Insert into database
sqlconnection = new SqlConnection(#"Data Source=" + Properties.Settings.Default.DBHost + ";Initial Catalog=BLAHBLAH;Persist Security Info=True;User ID=" + Properties.Settings.Default.DBUserName + ";Password=" + Properties.Settings.Default.DBPassword + ";");
sqlconnection.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlconnection;
int count = 0;
foreach (var item in files)
{
cmd.CommandText = #"insert into Images (Name, Games) values ('" + item.Value + "', '" + games + "')";
cmd.ExecuteNonQuery();
count++;
}
}
catch(Exception exception)
{
MessageBox.Show(exception.Message);
}
sqlconnection.Close();
this.Cursor = Cursors.Default;
This is Sql server network connection error. try to login in sql browser also check your sql service is running or not and try to debug your code that are you able to open connection check/login credentials.
Firstly, I'd like to thank everyone for their help and suggestions. I've managed to solve this problem.
As it turns out, the reason this code was failing was because there was an issue with retrieving setting data from Properties.Settings.Default.SettingName. The settings are there and saved, but it wasn't able to retrieve the settings into variables for some reason.
I got this code to work by storing settings somewhere else other than Properties.Settings.
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();
}