Oracle Data Provider for .Net - No data querying - c#

I am using Oracle Data Provider for .NET Assembly to query Oracle database.
Below is my code.
Though the code runs with no error. I don’t get data to my dataset dsOracleData.
However, when I run the query after connecting to SQL Developer, I can see the resulting data for the query.
OracleConnection conn = new OracleConnection(“ConnectionString”)
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = Query1.ToString();
cmd.CommandType = CommandType.Text;
OracleDataAdapter adapter = new OracleDataAdapter(cmd);
OracleCommandBuilder builder = new OracleCommandBuilder(adapter);
DataSet dsOracleData = new DataSet();
adapter.Fill(dsOracleData);

Hi u can try like this,
OracleConnection conn = new OracleConnection("Your Connection string");
Conn.Open;
DataSet dataSet = new DataSet();
OracleCommand cmd = new OracleCommand("your select query");
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
using (OracleDataAdapter dataAdapter = new OracleDataAdapter())
{
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dataSet);
}

Related

OracleDataAdapter not filling DataTable when querying to a DBLink

I'm trying to execute a query from OracleDataAdapter in C#, this query will go to a DBLink available in the DB, but for some reason when it needs to fill the datatable it throws me an error:
IndexOutOfRangeException - ndex was outside the bounds of the array
the code I'm trying to execute is:
OracleConnection conn = new OracleConnection(connectionString);
conn.Open();
DataSet dataSet = new DataSet();
OracleCommand cmd = new OracleCommand("select count(*) alive from dual#MYDBLINK");
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
using (OracleDataAdapter dataAdapter = new OracleDataAdapter())
{
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dataSet); //Here is where it fails
}
The query works if I execute in sql developer, and the code works if I remove the DBLink information and let the query as select count(*) alive from dual, so I asume, that the issue here is something with the DBLink or the # character.

Displaying results of query to Oracle database in dataGrid

I am struggling to make my dataGrid viev results of query. Connection with database is fine. Here is sample of my code:
using (OracleConnection conn = new OracleConnection())
{
conn.ConnectionString = "properConnectionString"
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "select * FROM WORKERS";
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
using (OracleDataAdapter orclDataAdapter = new OracleDataAdapter(cmd))
{
DataTable dt = new DataTable();
orclDataAdapter.Fill(dt);
dataGridView1.DataSource = dt;
}
}
Assuming that your call to the Oracle database is actually returning data, you're missing the databind statement on the gridview. Add this:
dataGridView1.DataBind();
place it right after the dataGridView1.DataSource = dt; line of code.

Getting stored procedure script through C# in ASP .NET

I am working on a DbCompre tool. Basically my requirement is that through C#, generate stored procedure script in web application. First of all is it possible?
In SQL Server we generate script easily through right click or with the help of command sp_helptext <ProcedureName>.
Here is my code:
public DataTable generateScripts()
{
SqlCommand cmd = new SqlCommand("sys.sp_helptext dloc_GetTasksRewarehousePutaway", AppCon);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(DS);
adp.SelectCommand = cmd;
return DS.Tables[0];
}
When code execute I am getting this error:
Could not find stored procedure 'sys.sp_helptext dloc_GetTasksRewarehousePutaway'
But in database this procedure is available.
Absolutely its possible, you need to pass the SP name as a parameter though to the parameter objname:-
SqlCommand cmd= new SqlCommand("sys.sp_helptext", AppCon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#objname", "dloc_GetTasksRewarehousePutaway");
using (SqlConnection con = new SqlConnection ("Connection String Here"))
{
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "sp_helptext #procName";//Stored Procedure name
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("procName", "dloc_GetTasksRewarehousePutaway");
con.Open();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
/*
You will get the CREATE PROC text here
Do what you need to with it. For example, write
to a .sql file
*/
}
}
}
}
Whole example to get stored procedure T-SQL text from database
// add connection string
var conString = "Db Connection String";
// add SP name
var spName = "DbSpName";
var sb = new StringBuilder();
try
{
using (var conn = new SqlConnection(conString))
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand("sys.sp_helptext", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#objname",spName );
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
sda.SelectCommand = cmd;
var dt = ds.Tables[0];
foreach (DataRow dtRow in dt.Rows)
{
sb.Append(dtRow["Text"].ToString());
}
}
}
catch (Exception ex)
{
// log if something went wrong ex.Message
}

Im trying to load and display a table from sql into my datagrid in C# but its not displaying

//instantiating connection
dbConn = new SqlConnection("Data Source = local host; initial catalog=Project; Integrated Security = SSPI ");
ds = new DataSet();
//select everything from the table
dbCommand = new SqlCommand("SELECT * FROM Shops;", dbConn);
dbAdapter = new SqlDataAdapter(dbCommand);
//filling the datagrid
dbAdapter.Fill(ds, "All Shops");
dataGridView1.DataSource = ds.Tables["All Shops"];
To show data into datagrid you need to assign datasource and then use DataBind() to bind the data.
dataGridView1.DataSource = ds.Tables["All Shops"];
dataGridView1.DataBind(); // use this after assigning the datasource
private void GetResults()
{
//Establishing the MySQL Connection
MySqlConnection conn = new MySqlConnection("Database=potentiality_live;Data Source=eu;User Id=ptly;Password=phat40");
string query;
MySqlCommand SqlCommand;
MySqlDataReader reader;
MySqlDataAdapter adapter = new MySqlDataAdapter();
//Open the connection to db
conn.Open();
//Generating the query to fetch the contact details
query = "SELECT id,date_time,link FROM'sdfsdfsdf'";
SqlCommand = new MySqlCommand(query, conn);
adapter.SelectCommand = new MySqlCommand(query, conn);
//execute the query
reader = SqlCommand.ExecuteReader();
//Assign the results
GridView1.DataSource = reader;
//Bind the data
GridView1.DataBind();
}
Reference:
http://www.codeproject.com/Questions/453091/how-to-get-data-from-sql-database-to-gridview-by-c

C# -> Retrieving dataset from SQL Server 2008

I have a table called NAMES in my SQL Server database. I am trying to retrieve the entire table and put it into a dataset:
//get the connection string from web.config
string connString = ConfigurationManager.ConnectionStrings["Platform"].ConnectionString;
DataSet dataset = new DataSet();
using (SqlConnection conn = new SqlConnection(connString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("NAMES", conn);
adapter.Fill(dataset);
}
This throws a sql exception though,
"Invalid Object Name NAMES"...
What am I doing wrong?
Open the connection !!!!!!
//get the connection string from web.config
string connString = ConfigurationManager .ConnectionStrings["Platform"].ConnectionString;
DataSet dataset = new DataSet();
using (SqlConnection conn = new SqlConnection(connString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("select * from [NAMES]", conn);
conn.Open();
adapter.Fill(dataset);
}
You're not passing an actual SQL select command to the SqlCommand constructor.
First check whether Select * from dbo.[Names] is wroking in ur sql ?
string connString = ConfigurationManager .ConnectionStrings["Platform"].ConnectionString;
Dataset ds=new Dataset();
SqlConnection con = new Sqlconnection(connString);
SqlDataAdapter adapter = new SqlDataAdapter("Select * from dbo.[Names]",con);
adapter.Fill(ds);

Categories