Friday, March 23, 2007

Connection strings in Web.Config to connect different databases.

1) SQL Server

appSettings
add key="SqlConnectionString" value="Server=ServerName;
DataBase=DataBaseName;User Id=sa;Password = sa"

/appSettings

Or

appSettings
add key="SqlConnectionString"
value="Server=(local);Database=DBName2;UID=sa;PWD= "

/appSettings

Or
appSettings add key ="SqlConnectionString" value="Provider=SQLOLEDB; Data Source=server_name_or_address; Initial Catalog=database_name; User ID=username; Password=password"
/appSettings

User System.Data.SqlClient namespace to open the connection

2) MS Access

appSettings
add key = "MsAccessConnectionString"
Value = "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=c:\DB\Volume.mdb;"
/appSettings


User System.Data.Oledb namespace to open the connection

3) Oracle


appSettings
add key="OracleConnectionString"
Value="Provider=OraOLEDB.Oracle.1;
Password=mypass;
Persist Security Info=True;
User ID=me;
Data Source=DatabaseName;"
/appSettings


User System.Data.Oledb namespace to open the connection

4) LDAP


appSettings
add key="LDAPConnectString"
value="
LDAP://Domain.com/DC=Domain,
DC=com"
/appSettings


5) DB2

appSettings

add key="connMainDB1" value="data source=ipaddy;
initial catalog=db1;persist security info=False;
user id=blah;password=blah;workstation id=webserver;
packet size=4096"
/appSettings


4) Microsoft Access
Provider=MSDASQL; Driver={Microsoft Access Driver (*.mdb)}; DBQ=C:\path\filename.mdb;

5) Microsoft Excel
Provider=MSDASQL; Driver={Microsoft Excel Driver (*.xls)}; DBQ=C:\path\filename.xls;

6) Microsoft Text
Provider=MSDASQL; Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=C:\path\;

DSN Connection


Provider=MSDASQL; DSN=data_source_name; UID=username; PWD=password;

Sunday, March 04, 2007

DataTable already belongs to another DataSet & DataRow already belongs to another DataRow.

Error: DataTable already belongs to another DataSet
Try in this way to copy data table from data set to another data set.
DataSet ds = new DataSet();
ds = GetDataSet();
ds.Tables[0].TableName = "Old";
DataSet dsNew = new DataSet();
DataTable dt = ds.Tables[0];
ds.Tables.Remove(dt);
dt.TableName = "New";
dsNew.Tables.Add(dt);
Error : DataRow already belongs to another DataRow.
Copy data Row from one data table to another data table
DataTable dt = new DataTable();
Use ImportRow Instead of Row.Add (dt.Rows.Add(dataRow);)
dt.ImportRow(dataRow);
Get Updated Row state
This GetChanges method will give the updated row from original dataset.
And RowState method will give the state “Updated”,“Deleted”,”Inserted”

dsDataSet.GetChanges()
dsDataSet.Table[0].GetChages()
string updateMode = dsDataSet.Tables[0].Rows[rowNumber].RowState.ToString();
Create DataSet from DataRow.
private DataSet GetFilterChildRecords(DataSet dsMainDataSet,string whereCondition)
{
try
{
DataRow[] dr = dsMainDataSet.Tables[1].Select(whereCondition);
dsSetActCode_Details = dsMainDataSet.Clone();
for (int i = 0 ;i<dr.Length ;i++)
{
DataRow drNew = dsSetActCode_Details.Tables[1].NewRow();
dsSetActCode_Details.Tables[1].Rows.Add(drNew);
for (int j = 0; j < dr[i].ItemArray.Length; j++)
{
dsSetActCode_Details.Tables[1].Rows[i][j] = dr[i][j];
}
}
return dsSetActCode_Details;
}
catch(Exception Ex)
{
throw Ex;}
}