Visual Basic SQL queries
Hi
I want to access my SQL database [i've got it configured in datasource], within the code. I want to do it this way:
user selects few options in program, i build a query in the code, send it to sql database, recieve the answer and put it in two places [one will be datagrid, second the listbox].
How should i do it?
P.s.
I totally forgot... i am using vb 2005 beta 2
You have a few options, Krystian. You can use a Command object to pass a dynamically structured SQL String to your database. Here is the code:
Dim dataCommand As New SqlCommand
dataCommand.CommandType = CommandType.Text
'This code could be generated using String.Format!
dataCommand.CommandText = "SELECT * FROM Customers"
Dim sqlConnection1 As New SqlConnection("Your Connection String")
dataCommand.Connection = sqlConnection1
sqlConnection1.Open()
Dim myAdapter As SqlDataAdapter
myAdapter.SelectCommand = dataCommand
Dim myDataSet As DataSet
myAdapter.Fill(myDataSet)
sqlConnection1.Close()
Then you could go the new route, and use a TableAdapter. It's a more point-and-click method, and there are instructions at
http://msdn2.microsoft.com/library/kda44dwy(en-us,vs.80).aspx
Once you have a dataset, you can bind both the DataGrid and the ListBox using the DataSource propoerty and the DataBind method.
Hope this helps!