Want help on SQLDataAdapter

liunx

Guest
Hi Experts,
I am using sqldataadapter for inserting and updating the records.
Below is the code i m using... but this has no effect on the table...

I am reading data from Excel and storing it in Dataset. Now i want to use this dataset to update my table in SQLServer... but it just dont update/insert any thing and not even throwing any exception... please help me out...

Here is the code.


public static void LoadAndUpdateBUTables(string strFileName)
{
DataSet ds = new DataSet(); // to be used to add tables.
SqlConnection conn = new SqlConnection(clsDataAccess.ConnectionString());
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "sproc_BUServerGetScanConfig";
conn.Open();
SqlDataReader drdScan = comm.ExecuteReader();

string sConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; " +
"Data Source = " + strFileName + "; " + "Extended Properties = Excel 8.0;";
while(drdScan.Read())
{
OleDbDataAdapter oledbda = new OleDbDataAdapter();
OleDbConnection oledbConn = new OleDbConnection(sConnectionString);
for(int iCount=1; iCount<=4; iCount++)
{
OleDbCommand oledbCmd = new OleDbCommand("Select distinct " + drdScan["ScanField"].ToString() + " from Scan" + iCount.ToString(), oledbConn);
oledbda.SelectCommand = oledbCmd;
oledbda.Fill(ds, drdScan["TableName"].ToString()); //check if updates or overwites
}

//New Command object for the InsertCommand
SqlCommand sqlInsertComm = new SqlCommand();
SqlConnection sconn = new SqlConnection(clsDataAccess.ConnectionString());
SqlDataAdapter sqlda = new SqlDataAdapter();
sqlInsertComm.Connection = sconn;
sqlInsertComm.CommandType = CommandType.StoredProcedure;
sqlInsertComm.CommandText = drdScan["SProc_Name"].ToString();
//Parameters ',' dilimited to be added. if identity column not null then direction is OUT
string strPrams = drdScan["ScanField"].ToString();
string [] arrstrParam = strPrams.Split(',');
for(int iPCount=0; iPCount<arrstrParam.Length; iPCount++)
{
string strparamName = "@" + arrstrParam[iPCount];
SqlParameter sqlParam = new SqlParameter();
sqlParam.ParameterName = strparamName;
switch(drdScan["Type"].ToString().ToUpper())
{
case "VARCHAR":
sqlParam.SqlDbType = SqlDbType.VarChar;
break;

case "CHAR":
sqlParam.SqlDbType = SqlDbType.Char;
break;

case "INT":
sqlParam.SqlDbType = SqlDbType.Int;
break;

case "DATETIME":
sqlParam.SqlDbType = SqlDbType.DateTime;
break;
}
sqlParam.SourceColumn = arrstrParam[iPCount];
sqlInsertComm.Parameters.Add(sqlParam);

if(drdScan["IdentityField"] != null)
{
SqlParameter sqlParam1 = new SqlParameter();
sqlParam1.ParameterName = "@" + drdScan["IdentityField"].ToString();
sqlParam1.SqlDbType = SqlDbType.Int;
//sqlParam1.SourceColumn = arrstrParam[iPCount];
sqlParam1.Direction = ParameterDirection.Output;
sqlInsertComm.Parameters.Add(sqlParam1);
}
}
sqlda.InsertCommand = sqlInsertComm;

//DataRow dr = ds.Tables[drdScan["TableName"].ToString()].Rows[0];
//dr["Region"] = "NA";

try
{
//sconn.Open();
//sqlInsertComm.ExecuteNonQuery();
int j = sqlda.Update(ds, drdScan["TableName"].ToString());
j = Convert.ToInt16(sqlda.InsertCommand.Parameters["@ID"].Value);
//sconn.Close();
//dt.AcceptChanges();
//sqlda.Update(ds,drdScan["TableName"].ToString());
//ds.Tables[drdScan["TableName"].ToString()].AcceptChanges();
}
catch (Exception e)
{
int i=0;
}
}
}


--Also attaching the code as text file.

Thanks and Regards,
Amtidumpty
 
Back
Top