Google Mobile Ads

Friday, May 24, 2013

VB.NET ile SQL Server'a Bağlanmak.

VB.NET ile SQL Server'a bağlanmak ve sorgu sonucunu datatable'a atmak.

'Imports System.Data.SqlClient
Dim con As New SqlClient.SqlConnection Dim strCon As String = "Data Source=SERVERNAME;Initial Catalog=DATABASENAME;Integrated Security=SSPI;Connection Timeout=10;" 'NT Authentication 'For SQL Authentication Dim strCon As String = "Data Source=SERVERNAME;Initial Catalog=DATABASENAME;User ID=USERNAME;Password=PASSWORD;Connection Timeout=50;" Dim strCommand As String = "SELECT * FROM TABLE" Dim command As SqlCommand Dim da As SqlDataAdapter Dim dt As New DataTable Try con.ConnectionString = strCon command = New SqlCommand(strCommand, con) command.CommandTimeout = 3000
da = New SqlDataAdapter(command) da.Fill(dt) MsgBox(dt.Rows.Count.ToString())
'FOR INSERT, UPDATE, DELETE, USE BELOW
'Dim count as Integer = command.ExecuteNonQuery()
'count is the affected row number
Catch ex As Exception MessageBox.Show(ex.Message, "Error") Finally If con.State = ConnectionState.Open Then con.Close() End If End Try

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...