0

I am a relative newbie and trying to insert multiple rows (and data from textboxes) from one table into another and am stuck.

This SQL identifies the data to be inserted into the table

strsql = "SELECT '" & textbox1.text & "', '" & Textbox2.text & "', "
strsql = strsql & " a.TaskNum, a.StartDay, a.NumofDays FROM VETTimeLines as a"
strsql = strsql & " ORDER BY a.StartDay"

I started out along the lines of -> Insert into StudentProgram Values() code shown above, after 3 days of trying I now look forward to your advice.

Many thanks in anticipation Peter

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Peter Renwick
  • 51
  • 1
  • 1
  • 3
  • Did you successfully establish a connection with your database? What is the primary key column? Do you have the current insert code? Also what DBMS (MS-Access, SQL Server, etc.)? – NoChance Jan 06 '14 at 04:18
  • 1
    What you're doing is dangerous and will get your databases hacked, probably sooner rather than later. Read up on sql injection attacks and parameterized queries before doing anything else here. – Joel Coehoorn Jan 06 '14 at 04:21

1 Answers1

0

Inserting data using C# can be performed in variety of ways such as using Entity Framework or using ADO.NET, as you have chosen to do in this case.

Using ADO.NET you either write the insert as you did or by using DataAdapter approach. DataAdapter is capable of creating the SQL code for you, amongst other things. For example see:SQL DataAdapter. It is a good idea to not build a SQL string as you did because of sql injection threat as @Joel Coehoorn indicates in his comment above.

One way to overcome this is by using Parameters as shown in the above link. If you decide to provide the SQL for insert yourself with parameters, here is a good example:StackOverFlow-Insert using ADO.

The idea behind all of the above code is to create a connection object, create a parameter object, create a command object, open the database connection, execute the command and close the connection.

Try one of the above approaches and let's know your specific issue if any arises.

Community
  • 1
  • 1
NoChance
  • 5,632
  • 4
  • 31
  • 45