I'm trying to connect to oracle database but I get this error
System.BadImageFormatException: 'Could not load file or assembly 'Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342' or one of its dependencies. An attempt was made to load a program with an incorrect format.'
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
string ordb = "Data Source =orcl ;User Id = hr ; password =hr;";
OracleConnection conn;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
conn = new OracleConnection(ordb);
conn.Open();
}
}
}
Check your dll references, your app seems to be an X64 base, but maybe one of the dll's is of a different format
Related
I've just working with c# and mysql and have ran into a problem. I've created a mysql db(remote access) using mysql workbench. I've been using visual studio to write the code(c#), xamarin.forms. The aim is to connect the database on multiple platforms. I'm trying to make it connect through windows(uwp) and android, it works fine on the windows(uwp). However when I try and run it through my android device to test the app I get an error. The error states "SYSTEM.TYPEINITIALIZATIONEXCEPTION......OPERATION IS NOT SUPPORTED ON THIS PLATFORM". Any idea on how to get around this? Thank you.
This is the code:
'''
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using MySql.Data.MySqlClient;
using System.Data;
namespace demo {
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
void Button_Clicked(object sender, EventArgs a)
{
MySqlConnection con = new MySqlConnection(user id; password);
try
{
con.Open();
if (con.State == ConnectionState.Open)
{
((Button)sender).Text = $"Connected";
}
}
catch (Exception ex)
{
((Button)sender).Text = ex.ToString();
}
con.Close();
}
}
}
I having an aspx.cs file that I am adding code to. I have all the right namespaces and references in my solution, but my code is referencing the wrong namespace with the following error on my Server server = new Server()
System.Web.UI is a 'property' but used as a 'type'
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.IO;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace IISLoggingSolution
{
public partial class Main : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//connection string for SQL DB
string connectionString = #"Data Source=Computer\SQLEXPRESS;Initial Catalog=IISLogs;Integrated Security=True";
//Location of the query to be ran
string script = File.ReadAllText(#"C:\\Users\\User\\Desktop\\query.txt");
SqlConnection conn = new SqlConnection(connectionString);
//this is the error line
Server server = new Server(new ServerConnection(conn));
//executes query
server.ConnectionContext.ExecuteNonQuery(script);
}
}
}
How can I get it to use the Microsoft.SqlServer.Management.Smo instead of trying to used the System.Web.UI?
using NS_Server = Microsoft.SqlServer.Management.Common;
using System.Web.UI;
...
NS_Server.Server server = new NS_Server.Server();
the line using NS_Server is an alias. You can use it as a shortcut when there are namespace collisions.
You can use an alias directive:
using Server = Microsoft.SqlServer.Management...Server;
It may not work if property takes precedence, but in that cas you can simply specify the full namespade path.
var server = new Microsoft.SqlServer.Management....Server();
I have run into a problem where my C# code is creating a whole new database instead of using a preexisting one. Then my program runs into errors where the program cannot find the table to insert the information even though the preexisting database has the table because the code itself is looking at the new table. Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Finisar.SQLite;
namespace WestSlope
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SQLiteConnection sqlite_conn;
SQLiteCommand sqlite_cmd;
//SQLiteDataAdapter sqlite_datareader;
sqlite_conn = new SQLiteConnection("DataSource=ClientLogDB.db;Version=3;New=True;Compress=True;");
//open conection
sqlite_conn.Open();
//create sql commands
sqlite_cmd = sqlite_conn.CreateCommand();
//Let SQLite command know query is known
sqlite_cmd.CommandText = "INSERT INTO ASAM (ASAMone, ASAMtwo, ASAMthree, ASAMfour, ASAMLim, ASAMLimEX) VALUES ('Had to call', 'Reffered', 'Had to call', 'Watched', 1 , 'Injured legs');"
;
//execute query
sqlite_cmd.ExecuteNonQuery();
sqlite_conn.Close();
}
}
}
What the code is supposed to do is when the user presses a button the program will save information to the preexisting database; but, as you can see the program is making a new database instead of using the preexisting one.
Use new=false in your connection string to use existing database file.
Following should be the connection string:
sqlite_conn = new SQLiteConnection("DataSource=ClientLogDB.db;Version=3;New=False;Compress=True;");
I was doing a basic example of 3 tier architecture in c#.I created two dll's for data and business layer.Also I am using data layer dll in business layer code.And,business dll and data access dll in presentation layer(which is a winform application).Now,when the presentation layer code is being executed,an exception is coming which says :
Database
'D:\11feb\practice\3tier\PresentationLayer\PresentationLayer\bin\Debug\Data.mdf'
do not exists.
I had created my database Data.mdf in data layer.
I copied the database files to the location mentioned in exception and the application successfully got executed.But I want the database to be accessed from my data layer.
Data Layer Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DataAccessLayer
{
public class DataAccess
{
public DataTable dataRead()
{
SqlConnection con = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Data.mdf;Database=Data;Integrated Security=True;User Instance=True");
DataTable dt = new DataTable();
con.Open();
SqlCommand cmd = new SqlCommand("select ID,Name from datatable", con);
try
{
SqlDataReader rd = cmd.ExecuteReader();
dt.Load(rd);
return dt;
}
catch
{
throw;
}
}
}
}
Business Layer Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;
using System.Data;
namespace BusinessLogicLayer
{
public class BusinessLogic
{
DataAccess dataAccess = new DataAccess();
public DataTable getPersons()
{
try
{
return dataAccess.dataRead();
}
catch { throw; }
}
}
}
Presentation Layer Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;
namespace PresentationLayer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
BusinessLogic BusinessLogic = new BusinessLogic();
this.dataGridView1.DataSource = BusinessLogic.getPersons();
}
catch
{
MessageBox.Show("Error Occurred");
}
}
}
}
The problem is that you have added Data.mdf in the solution but when the application runs, it tries to find out the mdf file in the bin directory. Click on Data.mdf file in the solution. Go to its properties ( By Pressing F4) and then look out for the property "Copy to the Output Directory" and then change the value to Copy Always.
Also check out your connection string.
I cannot connect to MySQL it fails on line connection.Open(), is there something wrong with my code ?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace MySQLConnection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string MyConString = "SERVER=localhost:3316;" +
"DATABASE=mydb;" +
"UID=user;" +
"PASSWORD=password;";
MySqlConnection connection = new MySqlConnection(MyConString);
connection.Open();
// ...
connection.Close();
}
}
}
this is the string format I use to connect via the MySql.Data.dll version 6.1.2.0
server={0};user id={1};password={2};database={3};port={4}
so your connection string should be
server=localhost;user id=user;password=password;database=mydb;port=3316
You need to specify the Port as a separate argument in the connection string and it looks like the password key is "Pwd" instead of "Password".
See connectionstrings.com for help on the exact syntax.