Multiple Data bases syntax in ASP.NET

Posted by Srinathgb under ASP.NET on 7/6/2013 | Points: 10 | Views : 1852 | Status : [Member] | Replies : 5
Dear Team

What are the changes in syntax to be taken care in writing code in ASP.NET to work in different databases like SQL Server, my sql, oracle.
I want to write single code with if condition if reqd to take care of all databases.
what are the list of syntax to be considered to handle multiple databases.

Srinath




Responses

Posted by: Raj.Trivedi on: 7/7/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hello Srinath

There is no single line of code that will help you to connect to multiple database or data source.You will have to write the code for each datasource and database connection

Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved

Srinathgb, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Srinathgb on: 7/7/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Raj

Thanks for reply. Can i have the various syntax which will be different to all 3 databases. (like any specific datatypes etc)
like syntax in date etc.

Srinath


Srinathgb, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raj.Trivedi on: 7/7/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hello srinath,

I did not get you this time, means you want that the syntax for all the database operations to be different is this what are you looking for.

Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved

Srinathgb, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Srinathgb on: 7/7/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Raj

Yes, You are right, what are the datatypes mainly to be taken care.
(if any syntax in all 3 databases at a glance)
srinath



Srinathgb, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raj.Trivedi on: 7/7/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hello Srinath,

The data types depends upon the datasource which you are using.There is not much difference it depends upon which application language you are writing the code for

See here

This is the way to connect Oracle

Add a reference in the bin folder

Right Click bin folder -> Add Reference -> .NET -> Oracle.DataAccessClient

using Oracle.DataAccess.Client;

private void LoadData()
{
try
{
string ConString = "Data Source=XE;User Id=system;Password=*****;";
using (OracleConnection con = new OracleConnection(ConString))
{
OracleCommand cmd = new OracleCommand("SELECT * FROM table", con);
OracleDataAdapter oda = new OracleDataAdapter(cmd);
DataSet ds = new DataSet();
oda.Fill(ds);
if (ds.Tables.Count > 0)
{
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

Now same can be done fpr sql by just changing the Class of Oracle to SQL

import sql class

using System.Data.SqlClient;
using System.data;

then

private void LoadData()
{
try
{
string ConString = "Data Source=XE;User Id=system;Password=*****;";
using (SQLConnection con = new SQLConnection(ConString))
{
SQLCommand cmd = new SQLCommand("SELECT * FROM table", con);
SQLDataAdapter oda = new SQLDataAdapter(cmd);
DataSet ds = new DataSet();
oda.Fill(ds);
if (ds.Tables.Count > 0)
{
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}


There is not much difference

Hope this helps

Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved

Srinathgb, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response