insert a record rise an exception

A

authorking

I create the databse with the follwing code. I really don't know why the
insert operation can't be completed.
databse creation code:
ADOX.CatalogClass cat = new ADOX.CatalogClass();//define a Jet database
class
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data
Source=history_data.mdb;" + "Jet OLEDB:Engine Type=5");//create a Jet
database
System.Data.OleDb.OleDbConnection hist_cnn = new
System.Data.OleDb.OleDbConnection();
System.Data.OleDb.OleDbCommand hist_command = new
System.Data.OleDb.OleDbCommand();
hist_command.CommandText="CREATE TABLE hist_data_record(record_id char(8)
NOT NULL PRIMARY KEY , ball_1 smallint NOT NULL , ball_2 smallint NOT NULL ,
ball_3 smallint NOT NULL , ball_4 smallint NOT NULL , ball_5 smallint NOT
NULL , ball_6 smallint NOT NULL)";
hist_cnn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;" + "Data
Source=history_data.mdb;" + "Jet OLEDB:Engine Type=5";
hist_cnn.Open();
hist_command.Connection=hist_cnn;
hist_command.ExecuteNonQuery();
hist_cnn.Close();

record inersting code:
System.Data.OleDb.OleDbConnection hist_cnn = new
System.Data.OleDb.OleDbConnection();
System.Data.OleDb.OleDbCommand hist_command = new
System.Data.OleDb.OleDbCommand();
System.Data.DataSet hist_data = new System.Data.DataSet();
hist_command.CommandText="insert into hist_data_record (record_id)
values('2003') ";
hist_cnn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=history_data.mdb;" + "Jet OLEDB:Engine Type=5";

hist_cnn.Open();

hist_command.Connection=hist_cnn;

hist_command.ExecuteNonQuery();//rise a exception and the
operation ended
 
N

Nick Malik

You are getting an error because you are creating a table where each row
must have seven values... you defined each value as NOT NULL, which means
that you WANT the database to throw an error and reject any record where any
of the seven columns is null.

Then, you use an insert statement that only has one value. The rest of the
values are null.
The database is responding with an error, just like you told it to.

Either pass values for the other fields, or define the table so that the
other values are allowed to be stored as null. (Do what makes sense for
your application).

--- Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top