using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OleDbCommand ocmnd;
OleDbConnection oconn = null;
string ConectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data source=C:\TestDB.mdb";
private void btnOrder_Click(object sender, EventArgs e)
{
int intID = Int32.Parse(this.txtID.Text);
decimal decAmount = Decimal.Parse(this.txtAmount.Text);
oconn = new OleDbConnection(ConectionString);
string query1 = "Select * from table1 where ID = NULL OR ID =" + intID;
ocmnd = new OleDbCommand(query1, oconn);
try
{
oconn.Open();
DataSet DS = new DataSet();
OleDbDataAdapter myDA = new OleDbDataAdapter(ocmnd);
myDA.Fill(DS);
DataTable DT = DS.Tables[0];
if (DT.Rows.Count > 0)
{
foreach (DataRow DR in DT.Rows)
{
int IDValue = Int32.Parse(DR.ItemArray[0].ToString());
decimal AmountValue = Decimal.Parse(DR.ItemArray[1].ToString());
if (IDValue == intID)
{
AmountValue += decAmount;
ocmnd.CommandText = "Update table1 set Amount =" + AmountValue + " Where ID = " + IDValue;
ocmnd.ExecuteNonQuery();
}
}
}
else
{
ocmnd.CommandText = "Insert Into table1(ID,Amount) values (" + intID + "," + decAmount + ")";
ocmnd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
oconn.Close();
}
}
}
}