Newbie question about inserting data

  • Thread starter Thread starter Mika M
  • Start date Start date
M

Mika M

Hello!

I think this must be simple newbie question. I'm using VB.NET 2003, and
making WinForm application. I need only insert row into database table,
which is some kind of log table, and not need to retrieve any data of it in
this part of my application. It is easy to make using sql like 'INSERT INTO
MyTable (...) VALUES (...) ', but I assume it's better to use databinding or
is it? Why I'm asking this is because if I use databinding, is the
application then retrieving content of the database table into workstations
memory? Because retrieving data is not needed, I want avoid it because this
database log table may become big size later. I would like to 'hear' others
opinions, and if possible see some example code or web link.
 
Mika

You really don't need data binding at all. All you need to do is to use ADO.NET to insert a new record to your database. Databinding is useful to display data, which is not necessary in your case

Cheers
Donald Xi

----- Mika M wrote: ----

Hello

I think this must be simple newbie question. I'm using VB.NET 2003, an
making WinForm application. I need only insert row into database table
which is some kind of log table, and not need to retrieve any data of it i
this part of my application. It is easy to make using sql like 'INSERT INT
MyTable (...) VALUES (...) ', but I assume it's better to use databinding o
is it? Why I'm asking this is because if I use databinding, is th
application then retrieving content of the database table into workstation
memory? Because retrieving data is not needed, I want avoid it because thi
database log table may become big size later. I would like to 'hear' other
opinions, and if possible see some example code or web link

-
Thanks in advance

Mik
 
Hi Mika,

You do not have to choose for the method, you can more, when you only want
to add a record, you use the insert with the commandexecute.

When you want to view some of those records you can make a nice dataset with
only the records you want to view using a nice select statemenent for that
using the dataadapter.

And when you do want to print them all or put them on a special loging back
up media, you can use the datareader.

Just what you need for the problem you have to make a solution for.

Just my thought,

Cor
 
Thanks Cor for your reply,

.... but sorry I don't figure out the following ...
... when you only want to add a record, you use the insert with the
commandexecute.

Could it be possible me to get tiny code example just to figure out this,
please ?
 
Hi Mika,

I did mean in this situation the command.executenonQuery which is one of the
command.executeXXX 's

However here is some code for that.

I hope that this helps?

Cor
\\\\\
Dim Conn As New SqlConnection("Server=xxxxxx;DataBase=Mydatbase;Integrated
Security=SSPI")
Try
Dim strSQL As String = _
"INSERT INTO tblMessages (Problem, Answer) VALUES (@Problem,
@Answer)"
Dim cmd As New SqlCommand(strSQL, Conn)
cmd.Parameters.Add(New SqlParameter("@Problem", SqlDbType.NVarChar,
200)).Value = Problem
cmd.Parameters.Add(New SqlParameter("@Answer", SqlDbType.NVarChar,
2500)).Value = Answer
Conn.Open()
cmd.ExecuteNonQuery()
Catch sqlExc As SqlException
MessageBox.Show(sqlExc.ToString)
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
Conn.Close()
End Try
////////
 
Thanks Cor!

Well - in fact I already made almost like this, but wasn't sure if it was
the right choise :) There are so much where to select these days.

-- Mika
 
Cor
\\\\\
Dim Conn As New SqlConnection("Server=xxxxxx;DataBase=Mydatbase;Integrated
Security=SSPI")
Try
Dim strSQL As String = _
"INSERT INTO tblMessages (Problem, Answer) VALUES (@Problem,
@Answer)"
Dim cmd As New SqlCommand(strSQL, Conn)
cmd.Parameters.Add(New SqlParameter("@Problem", SqlDbType.NVarChar,
200)).Value = Problem
cmd.Parameters.Add(New SqlParameter("@Answer", SqlDbType.NVarChar,
2500)).Value = Answer
Conn.Open()
cmd.ExecuteNonQuery()
Catch sqlExc As SqlException
MessageBox.Show(sqlExc.ToString)
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
Conn.Close()
End Try
////////

Here is a better way (in my opinion anyways):

Dim conn As SqlClient.SqlConnection = New
SqlClient.SqlConnection(ConnectionStringHere)

conn.Open

Dim selectCmd As SqlClient.SqlCommand = New SqlClient.SqlCommand("SELECT * FROM
[tblMessages] WHERE 0 = 1", conn)
Dim adapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(selectCmd)
Dim cmdBuilder As SqlClient.SqlCommandBuilder = New
SqlClient.SqlCommandBuilder(adapter)

adapter.InsertCommand = cmdBuilder.GetInsertCommand()

Dim ds As MyTypedDataSet = New MyTypedDataSet()
Dim row As MyTypedDataRow = ds.tblMessages.NewtblMessagesRow()

row.Problem = Problem
row.Answer = Answer

ds.tblMessages.Rows.Add(row)

adapter.Update(ds)
conn.close

--------------

This should give an idea and should be used to view the 'other' way to do an
update/insert/delete/select using the command builder class instead of
manipulating with your own sql statements. Also note, it is not wise to use the
SqlCommandBuilder class when you are using Stored Procedures. There are other
ways though, which make these commands more usable and the code more readable.
Look into Microsoft Data Access Application Block found at the following
location:

(watch for wrap)...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/daab-rm.asp

Hope this helps :)

Mythran
 
Back
Top