OleDb MSAccess Insert ???

  • Thread starter Thread starter Michel R.
  • Start date Start date
M

Michel R.

What`s wrong with this ??? The insert does not add into the database.

Using a Winform and
Database is sc.mdb
Filename is supplier
Fields SuppNbr Int64
SuppName String
SuppPhone Int64

Here is the insert Class:

public void insert_data()
{
string source = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\sc.mdb";

string command1 = string.Format("select count(*) from supplier where
SuppNbr = {0}", Int64.Parse(textBox1.Text));

string command2 = string.Format("insert into supplier (
SuppNbr,SuppName,SuppPhone) values({0},'{1}',{2})", Int64.Parse
(textBox1.Text), textBox2.Text, Int64.Parse(textBox3.Text));

OleDbCommand iCommand = new OleDbCommand();
OleDbConnection iConnection = new OleDbConnection(source);

{
iConnection.Open();

iCommand.Connection = iConnection;
iCommand.CommandText=command1;
OleDbDataReader iReader1;

iReader1 = iCommand.ExecuteReader();
iReader1.Read();

{

if(iReader1.GetInt32(0) != 0)
{
iReader1.Close();
MessageBox.Show("Already exists");
iReader1.Close();
iConnection.Close();
}
else
{
iReader1.Close();
iCommand.CommandText=command2;
MessageBox.Show("Insert Done");
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
iConnection.Close();
}

}
}

}
 
from your code, I can't see where you use command 2, which will insert data
to the database. also your code is not completed, right?
 
Back
Top