นี่ code ครับ เดี๋ยวผมลองรันก่อนระครับ เพราะตอนนี้ sql server 2008 ของผม error
Code (VB.NET)
Imports System.Data.SqlClient
Imports System.Data
Imports System.Data.OleDb
Imports System
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As New SqlClient.SqlConnection
Dim strCon As String = "Data Source=SPAREIT-02;Initial Catalog=test ;user id = sa ; password= pong1234 ;Integrated Security=SSPI;Connection Timeout=10;" 'NT Authentication
Dim strCommand As String = "SELECT * FROM setid"
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
MessageBox.Show("เชื่อมต่อได้แล้ว")
da = New SqlDataAdapter(command)
da.Fill(dt)
MsgBox(dt.Rows.Count.ToString())
MsgBox(dt.Columns.Count.ToString())
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
Finally
If con.State = ConnectionState.Open Then
con.Close()
End If
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strid As String = MaskedTextBox1.Text
Dim strun As String = MaskedTextBox1.Text
Dim strpw As String = MaskedTextBox1.Text
Dim connection As SqlConnection = Nothing
Dim objcmd As OleDbCommand = Nothing
Dim strcon As String, strSql As String
strcon = "Data Source=SPAREIT-02;Initial Catalog=test ;user id = sa ; password= pong1234 ;Integrated Security=SSPI;Connection Timeout=10;"
connection = New SqlConnection(strcon)
connection.ConnectionString = strcon
connection.Open()
strSql = "insert into user (id,username, password )values(?,?,?)"
objcmd = New OleDbCommand(strSql)
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@username", strid))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@username", strun))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@username", strpw))
objcmd.ExecuteNonQuery()
connection.Close()
' Response.Write()
End Sub
End Class
Tag : MySQL, Ms SQL Server 2005, Ms SQL Server 2008, Ms SQL Server 2012, VB.NET