Create Record in Database.

S

Shapper

Hello,

I need to create a new record in a database.
The database has 3 fields: [id] (autonumber), [title] and [text]
(strings)

When I create the record how should I create the [id] value?

Does the database insert it automatically?

Do I need to create a random number?
How can I do it to not repeat the id's already exist in the table?

My code is not working but it seems fine to me:

Dim connectionString As String =
System.Configuration.ConfigurationSettings.AppSettings("connectionString")
Dim dbConnection As System.Data.IDbConnection = New
System.Data.OleDb.OleDbConnection(connectionString)

Dim queryString As String = "INSERT INTO [t_news] ([title], [text])
VALUES (@title, @text)"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_title As System.Data.IDataParameter = New
System.Data.OleDb.OleDbParameter
dbParam_title.ParameterName = "@title"
dbParam_title.Value = title
dbParam_title.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_title)

Dim dbParam_text As System.Data.IDataParameter = New
System.Data.OleDb.OleDbParameter
dbParam_text.ParameterName = "@text"
dbParam_text.Value = text
dbParam_text.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_text)

Dim rowsAffected As Integer = 0
dbConnection.Open
Try
rowsAffected = dbCommand.ExecuteNonQuery
Finally
dbConnection.Close
End Try

Thanks,
Miguel
 
K

Karl Seguin

Miguel:
your connection.Open() should be in the try but that isn't your problem.
The id should be automatically generated. What isn't working? Are you
getting an error? is the row simply not getting added? Is this access?

Karl
 
S

Shapper

It seems I add an answer to this post but my newsreader says "This
article is no longer available on the server".

Why is that?

Thanks,
Miguel
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top