I'll give you a little pointer in the modern direction, with the hope that it will change the way you do db access forever:
Dont ever use string concatenation to build an SQL. We just had a major, public financial website fall over because a guy came along and put his name in as Michael O'Connor. 6 months ago, I gave up arguing with the nitwit that wrote the site, as to why he should use parameterized statements.. he didnt, and some user input broke his code.
It would have been impossible for that to happen with a properly written SQL
Your SQL should look like this:
myDbCommand.CommandText = "SELECT * FROM table WHERE tran_date >= @start_date AND tran_date <= @end_date"
you then add parameters to the command that you run:
next time you want to run it, dont clear the parameters and add them again etc.. just changes the values and run it..
-
Now that you know about stuff like this, and youre using 2.0, take a read of DW2 link in my signature, and youll go through some tutorials to a whole ne, proper way of doing database access that is good OO design too.. it will stop all the messy, ugly db code in button handlers etc that plagues vb programs the world over..
OK so this is what I have attached to my button handler:
VB.NET:
Dim taTableAdapter As New SqlClient.SqlDataAdapter("SELECT * FROM transactions WHERE void = 'True' AND tran_date BETWEEN @DateFrom AND @DateTo", glb_cnMM2007)
Dim dtTable As New DataTable
taTableAdapter.SelectCommand.Parameters.Add("@DateFrom", SqlDbType.DateTime)
taTableAdapter.SelectCommand.Parameters.Add("@DateTo", SqlDbType.DateTime)
taTableAdapter.SelectCommand.Parameters("@DateFrom").Value = Me.dtmDeleteVoidFrom.Value.Date
taTableAdapter.SelectCommand.Parameters("@DateTo").Value = Me.dtmDeleteVoidTo.Value.Date
taTableAdapter.Fill(dtTable)
MessageBox.Show(dtTable.Rows.Count.ToString)
Now this works fine and allows me to deal with the records that I want, however it does require me to create a new tableadapter, datatable and setup etc.
It does not allow me to usethe previous attempt to obtain an array of datarows from an existing tableadapter?
Also if i hav my main tableadapter and i start adding parameters this will modify the tabladapter so it does not remain intact for the rest of the application.
therefore should i be using independant tableadapters for each starnd of processsing?
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.