I use visual basic 2008 and a SQL Server database. I want to write the information stock in the database and I want to be rank like in php I use table to rank information what the method to do that in vb. This code write only last information into the table tbl_cars
Dim da As SqlDataAdapter
Dim dt As DataTable
Dim sqlconn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\m7md\Documents\Visual Studio 2008\Projects\Cars\Cars\cars.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
dt = New DataTable
da = New SqlDataAdapter
Dim sql As String = ("select * from tbl_cars ")
da = New SqlDataAdapter(sql, sqlconn)
sqlconn.Open()
da.Fill(dt)
For Each drRow As DataRow In dt.Rows
Label6.Text= (drRow.Item("db_type").ToString)
Label7.Text = (drRow.Item("db_gender").ToString)
Label8.Text = (drRow.Item("db_color").ToString)
Label9.Text = (drRow.Item("db_price").ToString)
Next
End Sub
Related
Attempting to fill a data table using SqlDataAdapter with a WHERE clause but it returns nothing. The SQL command itself returns data from SSMS and removing just the WHERE clause in code fills data table as expected.
Dim adapter As New SqlDataAdapter
Dim datatable As New DataTable
Using cnn As New SqlConnection(connectionString)
cnn.Open()
adapter = New SqlDataAdapter("SELECT * FROM [MyProjectDtbl] WHERE zipcode = 22021", cnn)
adapter.Fill(datatable)
adapter.Dispose()
End Using
Digging through the Select command EventSink, I found:
Unable to cast object of type 'System.Data.SqlClient.SqlInternalConnectionTds' to type 'System.Data.SqlClient.SqlInternalConnectionSmi'
Try it with parameters.
Private Sub OPCode()
Dim dt As New DataTable
Using cnn As New SqlConnection(connectionString),
cmd As New SqlCommand("SELECT * FROM [MyProjectDtbl] WHERE zipcode = #zip", cnn)
cmd.Parameters.Add("#zip", SqlDbType.Int).Value = 22021
cnn.Open()
dt.Load(cmd.ExecuteReader)
End Using
Debug.Print(dt.Rows.Count.ToString)
End Sub
i'm geek in vb.net, I already make vb.net from application with ms access data base. now i need to use this application for multi user and i want to use SQL server, i use this code to show data from access data base to listview :
con.ConnectionString = "provider=microsoft.ace.oledb.12.0; data source = |datadirectory|\noorapp.accdb;"
con.Open()
Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter("select * from " & Year(Now) & " where cmonth='" & m & "' order by cdate DESC", con)
da.Fill(dt)
Dim myrow As DataRow
For Each myrow In dt.Rows
ListView1.Items.Add(myrow.Item(0)).ToString()
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(2))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(3))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(4))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(5))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(6))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(7))
Next
any solution to how make this work for sql server also?
thanks.
change your connection string see here. Also change your OleDbConnection object to SQLConnection and your OleDbDataAdapter to SQLDataAdapter
Your database must already exist in the SQL Server for this to work
I use Visual Basic 2010 and Microsoft SQL Server 2008. I have my database and my table and i made the connection (at least i think i did) in VB using only the interface.
What i want to know is how to get data from the database and use it into my VB project. I have of course searched for solutions already but the differences i find only confuse me more. What i need to know are the basics, the tools/objects and procedures to retrieve the data.
What i try to do at the moment is make a simple selection and put that data into a listbox right when the program starts, like this:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SqlConnection1.Open()
SqlConnection1.Close()
End Sub
End Class
1) Create your connection string
Dim connectionString As String = "Data Source=localhost;........."
2) Connect to your Database
Dim connection As New SqlConnection(connectionString)
conn.Open()
3) Create a Command and the query
Dim command As New SqlCommand("SELECT * FROM Product", connection)
Dim reader As SqlDataReader = command.ExecuteReader() //Execute the Query
4) Retrieve your result. There are several ways
Dim dt As New DataTable()
dt.Load(reader)
'Close the connection
connection.Close()
5) Bind to your list box
myListBox.ItemSource = dt
Full code here
Using connection As New SqlConnection(connectionString)
Dim command As New SqlCommand("Select * from Products", connection)
command.Connection.Open()
SqlDataReader reader = command.ExecuteReader()
End Using
For more info
SQLCommand
SqlConnection1.Open()
using table As DataTable = New DataTable
using command as SqlCommand = New SqlCommand("SELECT blah blah", SqlConnection1)
using adapter As SqlDataAdapter = new SqlDataAdapter(command)
adapter.Fill(table)
end using
end using
for each row As DataRow in table.Rows
' add each listbox item
listbox1.Items.Add(row("column name"))
next
end using
SqlConnection1.Close()
I'm connecting to database using ADO .NET:
Dim conn As SqlConnection
Dim sqlcmd As SqlCommand
Dim da As SqlClient.SqlDataAdapter
Dim table As DataTable
conn = New SqlConnection(Utilities.ConnectionString)
sqlcmd = New SqlClient.SqlCommand()
sqlcmd.Connection = conn
sqlcmd.CommandType = CommandType.StoredProcedure
sqlcmd.CommandText = "mySP"
sqlcmd.Parameters.Add(New SqlClient.SqlParameter("#param", param1))
da = New SqlClient.SqlDataAdapter()
da.SelectCommand = sqlcmd
table = New DataTable()
da.Fill(table)
conn.Close()
sqlcmd.Connection.Close()
That works good.
When I launch on SQL Server the command:
EXEC SP_WHO2
For each call made from the previous code in ADO .NET, I have in the field Command the value: "AWAITING COMMAND", and in the field Status the value is "sleeping".
What does this means? The connection to database is still active? What should I do in order to close db connection?
The fact that after few hours you receive errors about connections exausted means that somewhere in your code you don't dispose correctly of your connection.
The code above seems correct, but what happen if you have exceptions? Are you sure to handle correctly the situation when your code exits unexpectedly from a method, due to exceptions?
The correct approach to this situation is refactoring your code.
Introduce everywhere the Using pattern. So your code above become:
Dim conn As SqlConnection
Dim sqlcmd As SqlCommand
Dim da As SqlClient.SqlDataAdapter
Dim table As DataTable
Using conn = New SqlConnection(Utilities.ConnectionString)
Using sqlcmd = New SqlClient.SqlCommand()
sqlcmd.Connection = conn
sqlcmd.CommandType = CommandType.StoredProcedure
sqlcmd.CommandText = "mySP"
sqlcmd.Parameters.Add(New SqlClient.SqlParameter("#param", param1))
da = New SqlClient.SqlDataAdapter()
da.SelectCommand = sqlcmd
table = New DataTable()
da.Fill(table)
End Using
End Using
This approach will ensure that your disposable objects (connection and command) will be released correctly and you will remove the subtle problem of connection leakings.
I made a simple program with a database connection:
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet 'holds table data
Dim da As OleDb.OleDbDataAdapter 'connection to database connectionobject
Dim sql As String
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = C:/JIMMY.mdb"
con.Open()
sql = "select * from TURNING"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "RECORDS")
con.Close()
Now I heard from someone that in order to make an application with a built in database, I should place the file inside the project.
C:\Users\User\documents\visual studio 2010\Projects\myProject\JIMMY.MDB
How do I make the directory dynamic? So, wherever I place the published application, will it work?
You can make it relative to the application, as explained in HOW TO: Determine the Executing Application's Path:
Dim path As String
path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
dbSource = "Data Source = " + Path.Combine(path, "jimmy.mdb")