How do I connect to an Oracle 11g database using asp.net3.5?
what is the name space and how to write connection string in web.config file?
please help me..
It depends on the data provider. See: ConnectionString.com And perhaps more specifically: The .NET Data Provider for Oracle. The connection string should look very similar in your web.config file. The only differences, obiously, will be the system/db name(s), user id, pwd etc.
Namespaces:
it is necessary to know which type of
objects can have the same name and
which are not. For this it is
necessary to introduce the concept of
a namespace. A namespace defines a
group of object types, within which
all names must be uniquely
identified—by schema and name. Objects
in different namespaces can share the
same name.
Here's also a nice tutorial you can follow that is ASP.NET-specific. And another article that may be of interest.
And a code snippet (using .NET Oracle provider:)
public DataTable myDataTable(string SQL, string ConnStr)
{
OracleConnection cn = default(OracleConnection);
DataSet dsTemp = null;
OracleDataAdapter dsCmd = default(OracleDataAdapter);
cn = new OracleConnection(ConnStr);
cn.Open();
dsCmd = new OracleDataAdapter(SQL, cn);
dsTemp = new DataSet();
dsCmd.Fill(dsTemp, "myQuery");
cn.Close();
return dsTemp.Tables[0];
}
Related
I am creating a C# WinForms Application along with SQL LocalDB. I am new to SQL and I am following this tutorial (timestamp: 5.50) to add database in project. I have also added a setup project to install the application to other PCs. When I installed the project for the first time it worked perfectly well, but when I updated the database schema (added one more column) the changes are not reflecting, updated version of setup & assembly and installed the setup file again, the database schema changes are not reflecting in the application. It is giving the following error:
My code is something like this:
private void load_list()
{
string conn = Properties.Settings.Default.dbAgeConnectionString;
SqlConnection sqlConn = new SqlConnection(conn);
string sqltext = "SELECT Name, Age, DOB from Users";
if (sqlConn.State != ConnectionState.Open) sqlConn.Open();
DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(sqltext,sqlConn);
adapter.Fill(table);
sqlConn.Close();
lstRecords.DisplayMember = "Name";
lstRecords.ValueMember = "Id";
lstRecords.DataSource = table;
gvrecords.DataSource = table;
}
Connection String:
Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\dbAge.mdf;Integrated Security=True
I tried adding the database files manually in Application folder of Setup but still no results.
I searched on the internet but did not find any database version control or upgrade method for SQL LocalDB. Is there any way I can achieve this?
Thanks in advance.
The reccomended way to use MySql.Data is to NOT use a MySqlConnection object; as explained in the documentation. This allows for the MySQl.Data API code to handle connection pooling correctly.
See: mySQL documentation
So, for example, this code Selects data with the connection string passed in as a parameter.
The MySqlConnection object is created in the background:
DataSet dataset = new DataSet();
MySqlDataAdapter adapter = new MySqlDataAdapter("select * from cfx_jobs", _mySqlConnectionString);
adapter.Fill(dataset);
return dataset;
I have looked around and I cannot find an example of how to Insert into the database without explicitly creating a MySqlConnection object.
Which method should I use?
This is how to do it.
MySqlHelper.ExecuteNonQuery(_mySqlConnectionString, sqlStatement);
I have an Azure Web Site, using a free Azure SQL database, and I have installed Umbraco CMS 7.1.1 to develop the site with. I have also created a custom table using Azure's SQL Management feature and I have created a couple of test rows with dummy text. How can I connect to my custom table and display the data on a page?
Usually I work with MySQL and fetching data is relatively easy to do but I'm having trouble converting my code to work with Azure SQL. The following is my code which is almost identical to when I use MySQL, but with this snippet I get the error "Keyword not supported: 'flush interval'". Has anyone been able to fetch custom table data with Azure SQL?
ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["umbracoDbDSN"];
using(SqlConnection con = new SqlConnection(cs.ToString()))
{
string sql = "SELECT * FROM [dbo].[MyTable]";
con.Open();
using(SqlCommand cmd = new SqlCommand(sql,con))
{
SqlDataReader reader = cmd.ExecuteReader();
}
con.Close();
}
If you are accessing this in an Umbraco-based website and have the tables within the same database that Umbraco is using, you can get the connection string by accessing the ConnectionString property on the DatabaseContext:
using (var con = new SqlConnection(Umbraco.Core.ApplicationContext.Current.DatabaseContext.ConnectionString)) {
// Your code here
}
However, you may find it advantageous to use the built-in PetaPoco support that Umbraco offers. There's a good example of using PetaPoco here: http://creativewebspecialist.co.uk/2013/07/16/umbraco-petapoco-to-store-blog-comments/
I am writing a Front End program for a database using XAML for the UI & C# for the logic. I'm trying to read in the database by doing the following:
//Declaring VFWPost table
//Note: This table is used in the "VFW Posts" tab.
DataTable tblVFWPost = new DataTable();
string connString1 = ConfigurationManager.ConnectionStrings["C:\Users\Sam Brockmann\Documents\VSS Database.accdb"].ConnectionString;
string query1 = #"SELECT VFW Post #, Post Manager ID, Annual Due Date, Post Address, Post City, Post Zip Code,
Post Phone Number, Post Email FROM tblVFWPost";
//Fill the VFWPost Set with the data
using (SqlConnection conn1 = new SqlConnection(connString1))
{
SqlDataAdapter da1 = new SqlDataAdapter(query1, conn1);
da1.Fill(tblVFWPost);
}
//Setting VFWPost Listbox item source
List<DataRow> VFWPostList = tblVFWPost.AsEnumerable().ToList();
VFWPostListBox.ItemsSource = VFWPostList;
The problem is that Visual Studio refuses to recognize the ConfigurationManager class, despite having "using System.Configuration;" up with the other namespaces I am using. Are there alternatives to the ConfigurationManager class that could be used to pull database information into my program directly?
As the documentation states, that class comes from the System.Configuration assembly. You need to add that to your project as a reference from the global assembly cache.
How can I get a DataSet with all the data from a SQL Express server using C#?
Thanks
edit: To clarify, I do want all the data from every table. The reason for this, is that it is a relatively small database. Previously I'd been storing all three tables in an XML file using DataSet's abilities. However, I want to migrate it to a database.
You can use the GetSchema method to get all the tables in the database and then use a data adapter to fill a dataset. Something like this (I don't know if it compiles, I just paste some code and change it a bit):
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient");
DataTable tables = null;
DataSet database = new DataSet();
using (DbConnection connection = factory.CreateConnection())
{
connection.ConnectionString = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=True";
string[] restrictions = new string[4];
// Catalog
restrictions[0] = "Northwind";
// Owner
restrictions[1] = "dbo";
// Table - We want all, so null
restrictions[2] = null;
// Table Type - Only tables and not views
restrictions[3] = "BASE TABLE";
connection.Open();
// Here is my list of tables
tables = connection.GetSchema("Tables", restrictions);
// fill the dataset with the table data
foreach (DataRow table in tables.Rows)
{
string tableName = table["TABLE_NAME"].ToString();
DbDataAdapter adapter = factory.CreateDataAdapter();
DbCommand command = factory.CreateCommand();
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = "select * from [" + tableName + "]";
adapter.SelectCommand = command;
adapter.Fill(database, tableName);
}
}
EDIT:
Now I refactored it a bit and now it's working as it should. The use of DbConnection and DbProviderFactories is for database engine abstraction, I recommend using it so you can change the database engine changing this line and the connection string:
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OracleClient");
The GetSchema method will retrive all tables from your database to a DataTable and then we get all the data from each table to the DataSet using the DataAdapter.
I think you need to narrow down the question somewhat... All the data? You mean, all the data in every table in every database? Well, the only answer to that is, a lot of code.
To connect to and talk to a SQL Server Express database engine, use the classes in the System.Data.SqlClient namespace, namely:
SqlConnection: Connect to the database
SqlCommand: Talk to the database
SqlDataReader: Iterate over data retrieved from the database
You can check the MSDN pages for all of these classes by clicking on the links above.
Here are some overview-links with more information:
CodeProject: Beginners guide to accessing SQL Server through C#
DevHood: Accessing SQL Server Data in C# with ADO.NET
Note that by and large, you use a SQL Server Express database engine the same way as the full SQL Server product, the difference is more in the tools you get with it, and some limitations in the express engine. Other than that you can just use the classes and language that you would use for a normal SQL Server database engine installation.
If this post didn't answer your question, please elaborate, and you have a higher chance of getting the answer you seek.
This can be done by using dataAdapter class.