#region Insert Data in MS Access Database
public int InsertDataInAccess(DataTable dtAfterPCPandTempMerge, string strMWPath)
{
int iRCount = 0;
string[] strSplitedArr = strMWPath.Split('\\');
strSplitedArr = strSplitedArr.Where(w => w != strSplitedArr[3]).ToArray();
string strMapPath = strSplitedArr[0] + "\\" + strSplitedArr[1] + "\\" + strSplitedArr[2];
strMapPath = strMapPath + "\\Plugins";
try
{
OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
strMapPath + @"\MWSWAT\mwswat.mdb;;Persist Security Info=False;");
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
string cmdText2 = "SELECT Count(*) FROM wgn_US";
var command2 = new OleDbCommand(cmdText2, myConnection);
var RCount = command2.ExecuteScalar();
var cmdText1 = "INSERT INTO wgn_US ([OID]) VALUES( " + (Convert.ToInt32(RCount) + 1) + ")";
iRCount = (Convert.ToInt32(RCount) + 1);
var command1 = new OleDbCommand(cmdText1, myConnection);
command1.ExecuteNonQuery();
int ColCount = dtAfterPCPandTempMerge.Columns.Count;
String colName = string.Empty;
string value = string.Empty;
for (int i = 1; i < ColCount; i++)
{
colName = dtAfterPCPandTempMerge.Columns[i].ColumnName;
value = Convert.ToString(dtAfterPCPandTempMerge.Rows[0][i]);
var cmdText = "UPDATE wgn_US SET " + colName + " = " + "'" + value + "'" + " WHERE OID= " + (Convert.ToInt32(RCount) + 1);
var command = new OleDbCommand(cmdText, myConnection);
int iCmd = command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
LoggerClass.ErrorException(ex.Message, ex);
}
return iRCount;
}
#endregion
public int InsertDataInAccess(DataTable dtAfterPCPandTempMerge, string strMWPath)
{
int iRCount = 0;
string[] strSplitedArr = strMWPath.Split('\\');
strSplitedArr = strSplitedArr.Where(w => w != strSplitedArr[3]).ToArray();
string strMapPath = strSplitedArr[0] + "\\" + strSplitedArr[1] + "\\" + strSplitedArr[2];
strMapPath = strMapPath + "\\Plugins";
try
{
OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
strMapPath + @"\MWSWAT\mwswat.mdb;;Persist Security Info=False;");
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
string cmdText2 = "SELECT Count(*) FROM wgn_US";
var command2 = new OleDbCommand(cmdText2, myConnection);
var RCount = command2.ExecuteScalar();
var cmdText1 = "INSERT INTO wgn_US ([OID]) VALUES( " + (Convert.ToInt32(RCount) + 1) + ")";
iRCount = (Convert.ToInt32(RCount) + 1);
var command1 = new OleDbCommand(cmdText1, myConnection);
command1.ExecuteNonQuery();
int ColCount = dtAfterPCPandTempMerge.Columns.Count;
String colName = string.Empty;
string value = string.Empty;
for (int i = 1; i < ColCount; i++)
{
colName = dtAfterPCPandTempMerge.Columns[i].ColumnName;
value = Convert.ToString(dtAfterPCPandTempMerge.Rows[0][i]);
var cmdText = "UPDATE wgn_US SET " + colName + " = " + "'" + value + "'" + " WHERE OID= " + (Convert.ToInt32(RCount) + 1);
var command = new OleDbCommand(cmdText, myConnection);
int iCmd = command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
LoggerClass.ErrorException(ex.Message, ex);
}
return iRCount;
}
#endregion
No comments:
Post a Comment