I want to connect to Oracle Database by visual studio and populate datagridview by its data. I know anything about Oracle . I just have SID,Username and password.
this is the code:
using Oracle.DataAccess.Client;
private void Form1_Load(object sender, EventArgs e)
{
var select = "SELECT * FROM tblProject";
conn.ConnectionString = "Data Source=(DESCRIPTION="
+ "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.3.50.205)(PORT=1521)))"
+ "(CONNECT_DATA=(SERVER=DEDICATED)(SID=sid)));"
+ "User Id=username;Password=pass;";
}
using (OracleConnection connection = new OracleConnection(conn.ConnectionString))
{
OracleDataAdapter adapter = new OracleDataAdapter(select, connection);
try
{
connection.Open();
var ds = new DataSet();
adapter.Fill(ds);
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
but the connection wouldn't open? where is the problem?
Thank you
There are several DataConnectors on the market. At least from Microsoft, Oracle and DevArt. The connection strings are not fully compatible. So you have to pay attention when you are looking for examples.
I like to use ezconnect whenever it is possible:
username/password#[//]host[:port][/service_name]
less to type -> less errors. Once I had to connect to a test system which refused the old syntax, but accepted ezconnect. I never figured out why.
Can you try with the below code . Looks like you are using ODP.NET without the tnsnames.ora file using connect descriptor
using Oracle.DataAccess.Client;
private void Form1_Load(object sender, EventArgs e)
{
var select = "SELECT * FROM tblProject";
conn.ConnectionString = "Data Source=(DESCRIPTION="
+ "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.3.50.205)(PORT=1521)))"
+ "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=sid)));"
+ "User Id=username;Password=pass;";
using (OracleConnection connection = new OracleConnection(conn.ConnectionString))
{
connection.Open();
OracleDataAdapter adapter = new OracleDataAdapter(select, connection);
try
{
var ds = new DataSet();
adapter.Fill(ds);
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
check this documentation at the "Using the connect descriptor" section
https://docs.oracle.com/cd/B28359_01/win.111/b28375/featConnecting.htm
Related
I asked a question [HERE] and we got all the information stored in the database SQL Server CE database.
The question remains now how to get the information stored back into variables.
This line of code:
myReader.SqlCeReader();
will not compile, I am asked if I have missed a compiler reference what ever that is.
The information is stored as strings with an Integer “ID” primary key.
The information will be used to create shortcuts on a disk suitable for launching, images in paint, executable programs and so on. They should not be more than strings which is why I find it hard to do, it should be simple.
A sample record
id=int NstacksName=String NstacksPath=String.
I think I have it all wrong and am surprised it even compiles this far.
private void label2_Click(object sender, EventArgs e)
{
string DirName;
SqlCeConnection conn = new SqlCeConnection("Data Source=C:\\Users\\username\\Documents\\Visual Studio 2010\\Projects\\NStacks1\\NStacks1\\Database1.sdf");
String name;
try
{
conn.Open();
SqlCeCommand Command = new SqlCeCommand("SELECT * FROM NStacks1 WHERE ID = 1", conn);
DataTable Data = new DataTable();
SqlCeDataAdapter adapter = new SqlCeDataAdapter(Command);
SqlCeDataReader myReader;
try
{
myReader.SqlCeReader();
DirName = Data.ToString();
con.Close();
name = DirName;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
con.Close();
}
}
private void button4_Click(object sender, EventArgs e)
{
string SName, NStackPath;
string source=("Data Source=C:\\Users\\username\\Documents\\Visual Studio 2010\\Projects\\NStacks1\\NStacks1\\Database1.sdf");
SqlCeConnection Con = new SqlCeConnection(source);
try{
Con.Open();
string Query= "SELECT * FROM Nstacks1 WHERE ID=1";
SqlCeCommand command = new SqlCeCommand(Query , Con);
SqlCeDataReader dr = command.ExecuteReader();
if (dr.Read())
{
textBox1.Text=(dr["NStacksName"].ToString());
label2.Text = (dr["NStacksItem"].ToString());
}
Con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
con.Close();
}
}
}
I am trying using c# code to connect to mysql server that installed on vmware machine and execute query and I can`t do that.
I tested the connection with uld file on my host and that worked.
I also tested the code on a sql server that I am not hosting on my pc and that was worked for me.
private void button1_Click(object sender, EventArgs e)
{
connString = "SERVER=(Main Server Ip)\\SSER;PORT=3306;DATABASE=......;UID=.......;PASSWORD=......";
try
{
conn = new MySqlConnection();
conn.ConnectionString = connString;
conn.Open();
// MessageBox.Show("connection success");
string query = "SELECT * FROM dbo.Table_1";
//Create Command
MySqlCommand cmd = new MySqlCommand(query, conn);
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
while (dataReader.Read())
{
Console.WriteLine(dataReader["id"] + "");
Console.WriteLine(dataReader["num"] + "");
}
conn.Close();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
I need to transfer the table's content to the same table located in another database, and I write this simple code using the C# dataAdapter.Fill() and dataAdapter.Update(), but it's seems not working like I supposed.
SqlConnection sqlConnection = new SqlConnection(strSqlConnectionString);
SqlConnection sqlConnection2 = new SqlConnection(strSqlConnectionString2);
sqlConnection.Open();
sqlConnection2.Open();
DataSet CustomerDataSet = new DataSet();
SqlDataAdapter sqlDA;
SqlDataAdapter sql2DA;
SqlCommandBuilder sqlCmdBuilder;
SqlCommandBuilder sqlCmdBuilder2;
sqlDA = new SqlDataAdapter("SELECT * FROM Articolo;", sqlConnection);
sqlDA2 = new SqlDataAdapter("SELECT * FROM Articolo;", sqlConnection2);
sqlCmdBuilder = new SqlCommandBuilder(sqlDA);
sqlCmdBuilder2 = new SqlCommandBuilder(sqlDA2);
sqlDA.Fill(CustomerDataSet, "Articolo");
sqlDA2.Fill(CustomerDataSet, "Articolo");
sqlDA2.Update(CustomerDataSet, "Articolo");`
What I want to do is to have the second db(string connection: strSqlConnectionString2) with updated data, taken from the first db, exploiting the functionality of dataAdapter.Fill() + dataAdapter.Update().
Is this possible? And can I do the same things but with Access db as a second db?
Can you try it this way?
using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
string connetionString;
OleDbConnection connection;
OleDbDataAdapter oledbAdapter;
OleDbCommandBuilder oledbCmdBuilder;
DataSet ds = new DataSet();
DataSet changes;
int i;
string Sql;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;";
connection = new OleDbConnection(connetionString);
Sql = "select * from tblUsers";
try
{
connection.Open();
oledbAdapter = new OleDbDataAdapter(Sql, connection);
oledbAdapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
oledbCmdBuilder = new OleDbCommandBuilder(oledbAdapter);
changes = ds.GetChanges();
if (changes != null)
{
oledbAdapter.Update(ds.Tables[0]);
}
ds.AcceptChanges();
MessageBox.Show("Save changes");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
I am trying to get my Winforms app to query a SQL Server CE database and return all rows that correspond to the column name specified by the user through a drop down list. When I run the programs it will return just a blank dataGrid. This is my first time working with SQL Server CE so any help would be appreciated.
My code is:
private void srchBTN_Click(object sender, EventArgs e)
{
string conString = Properties.Settings.Default.CurricularChangeTrackerConnectionString;
using (SqlCeConnection conn = new SqlCeConnection(conString))
{
string queryString = ("SELECT * FROM SecondaryEducation WHERE ProgramCode='" + PrgmCde.SelectedValue + "'");
try
{
conn.Open();
using (SqlCeDataAdapter adapter = new SqlCeDataAdapter(queryString, conn))
{
DataTable table = new DataTable();
adapter.Fill(table);
dataGridView1.DataSource = table;
adapter.Dispose();
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
If a property is not specified in ValueMember, SelectedValue returns the results of the ToString method of the object.
My point is you may not be getting what you think from the comboBox.
Try this:
string queryString = ("SELECT * FROM SecondaryEducation WHERE ProgramCode='" + PrgmCde.Items[PrgmCde.SelectedIndex].ToString() + "'");
You are declaring the data table inside the using statement, try this
private void srchBTN_Click(object sender, EventArgs e)
{
string conString = Properties.Settings.Default.CurricularChangeTrackerConnectionString;
DataTable table = new DataTable();
using (SqlCeConnection conn = new SqlCeConnection(conString))
{
string queryString = ("SELECT * FROM SecondaryEducation WHERE ProgramCode='" + PrgmCde.SelectedValue + "'");
try
{
conn.Open();
using (SqlCeDataAdapter adapter = new SqlCeDataAdapter(queryString, conn))
{
adapter.Fill(table);
dataGridView1.DataSource = table;
adapter.Dispose();
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
That way the table wont be disposed of when the scope of the using statement is ended.
I am trying to connect to a database with two tables. However, after I try and log in, I have an error. The error says there is no row at spot zero. I think this is because of my connection:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
namespace Project3
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void login_Click(object sender, EventArgs e)
{
OleDbConnection connect = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\parodeghero\\Documents\\Visual Studio 2010\\Projects\\Project3\\Project3\\App_Data\\QA.mdb;Persist Security Info=True");
//set up connection string
OleDbCommand command = new OleDbCommand("select * from Employee where Login=#login", connect);
OleDbParameter param0 = new OleDbParameter("#login", OleDbType.VarChar);
param0.Value = employeeID.Text;
command.Parameters.Add(param0);
//middle tier to run connect
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataSet dset = new DataSet();
da.Fill(dset);
//problem line
if (dset.Tables[0].Rows[0]["Password"].ToString().Equals(password.Text))
{
You need to open the connection
protected void login_Click(object sender, EventArgs e)
{
string pathToYourFileMDB = #"C:\yourPathHere\File.mdb";
try
{
OleDbConnection connect = new OleDbConnection($"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={pathToYourFileMDB};Persist Security Info=True");
//set up connection string
OleDbCommand command = new OleDbCommand("select * from Employee where Login=#login", connect);
OleDbParameter param0 = new OleDbParameter("#login", OleDbType.VarChar);
param0.Value = employeeID.Text;
command.Parameters.Add(param0);
//open connection
connect.Open();
//middle tier to run connect
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataSet dset = new DataSet();
da.Fill(dset);
}
catch(Exception err)
{
Debug.WriteLine(err.Message);
}
}
You do NOT need to open the connection. OleDbDataAdapter.Fill will open the connection AND close it if it found it closed to start with. Fill leaves the connection in the state that it finds it.
I do question your SQL. My understanding of OleDb is that it does NOT support naming parameters in SQL. It needs a place holder "?" instead of #login. You need a Parameter for each placeholder and the parameters must be added in the order they occurr. If your SQL is not valid, then you will have either a SQL Exception or no data in the DataTable, i.e. NO row 0 !
oledb connection complete code
http://csharp.net-informations.com/data-providers/csharp-oledb-connection.htm
string connetionString = null;
OleDbConnection cnn ;
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=yourdatabasename.mdb;";
cnn = new OleDbConnection(connetionString);
try
{
cnn.Open();
MessageBox.Show ("Connection Open ! ");
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
curos