created a table/datagriview when a button is pressed. then how to connect/add to SQL

Joined
Aug 8, 2011
Messages
20
Location
Batangas City, Philippines
Programming Experience
Beginner
i have a button.

when i click the button, it will automatically create a table..

----------------
i have a code that will create a table
----------------



Public [COLOR=#009900 !important][FONT=Verdana !important]Class[/FONT][/COLOR] Form1 Inherits System.Windows.Forms.Form

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
Dim Table1 As DataTable
Table1 = New DataTable("Customers")
'creating a table named Customers
Dim Row1, Row2, Row3 As DataRow
'declaring three rows for the table
Try
Dim Name As DataColumn = New DataColumn("Name")
'declaring a column named Name
Name.DataType = System.Type.GetType("System.String")
'setting the datatype for the column
Table1.Columns.Add(Name)
'adding the column to table
Dim Product As DataColumn = New DataColumn("Product")
Product.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(Product)
Dim Location As DataColumn = New DataColumn("Location")
Location.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(Location)

Row1 = Table1.NewRow()
'declaring a new row
Row1.Item("Name") = "Reddy"
'filling the row [COLOR=#009900 !important][FONT=Verdana !important]with values[/FONT][/COLOR]. Item property is used to set the field value.
Row1.Item("Product") = "Notebook"
'filling the row with values. adding a product
Row1.Item("Location") = "Sydney"
'filling the row with values. adding a location
Table1.Rows.Add(Row1)
'adding the completed row to the table
Row2 = Table1.NewRow()
Row2.Item("Name") = "Bella"
Row2.Item("Product") = "Desktop"
Row2.Item("Location") = "[COLOR=#009900 !important][FONT=Verdana !important]Adelaide[/FONT][/COLOR]"
Table1.Rows.Add(Row2)
Row3 = Table1.NewRow()
Row3.Item("Name") = "Adam"
Row3.Item("Product") = "PDA"
Row3.Item("Location") = "Brisbane"
Table1.Rows.Add(Row3)
Catch
End Try

Dim ds As New DataSet()
ds = New DataSet()
'creating a dataset
ds.Tables.Add(Table1)
'adding the table to dataset
DataGrid1.SetDataBinding(ds, "Customers")
'binding the table to datagrid
End Sub

End Class


------------
it will create a table... but is it possible that the table... will be saved automatically to my DATABASE in SQL?

im using sql 2008
and vb.net 2008
 
use Entity Framework, Model first Approach.
Can't offer any code, learning the object model myself.
Check if out, Im sure it will do the job.
 
You can also use ADO.net to help you. Open a connection to you database.
Then you loop through your dataset and get each element of the dataset. Each time you loop you must call an insert query to save the data into your database.
This is maybe the last solution because if you have so many rows, it will use memory since you will have to do the process as many times as you have the rows.
Let me know if it works.
 

Latest posts

Back
Top