 |
|
VB.NET Connection กับ Database SQL Server แล้วมัน Error ต้องแก้ไขอย่างไรคะ |
|
 |
|
|
 |
 |
|
Connection กับ database sql แล้วมัน error ตรงบรรทัดที่ไฮไล์ ต้องแก้ไขอย่างไรคะ
Code
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conStr As String = "Data Source=.\OSIRUS;" & _
"Initial Catalog=SA_TEST1;Integrated Security=SSPI;"
Dim connection As New SqlConnection(conStr)
Dim sql As String = "SELECT * FROM SaleTable"
Dim command As New SqlCommand(sql, connection)
Dim adapter As New SqlDataAdapter(command)
Dim data As New DataSet()
adapter.Fill(data, "SaleTable")
DataGridView1.DataSource = data.Tables("SaleTable")
Dim headers() As String = {"รหัส", "ชื่อหมวด", "คำอธิบาย", "รูปภาพ", "5", "6"}
For i = 0 To headers.Length - 1
DataGridView1.Columns(i).HeaderText = headers(i)
Next
connection.Close()
End Sub
End Class
ส่วนรูปด้านล่างเป็น sql ที่ใช้เก็บข้อมูลค่ะ

Tag : .NET, Ms SQL Server 2008, VS 2010 (.NET 4.x)
|
|
 |
 |
 |
 |
Date :
2013-04-23 13:40:57 |
By :
sukus |
View :
3261 |
Reply :
8 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันerror ว่าอะไร copy ให้ดูหน่อย
|
 |
 |
 |
 |
Date :
2013-04-23 13:47:47 |
By :
chaloemrat32 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตรง
Code (VB.NET)
Dim conStr As String = "Data Source=.\OSIRUS;" & _
"Initial Catalog=SA_TEST1;Integrated Security=SSPI;"
ลองตัวนี้
Code (VB.NET)
Dim conStr As String=@"Data Source=.\OSIRUS;Initial Catalog=SA_TEST1;Integrated Security=SSPI;"
|
 |
 |
 |
 |
Date :
2013-04-23 13:53:41 |
By :
chaloemrat32 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตามด้านล่างนะคะ


ขอบคุณค่ะ
|
 |
 |
 |
 |
Date :
2013-04-23 13:56:39 |
By :
sukus18 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำไมมีปัญหาเรื่อง Connection กันบ่อยจังครับ ผมเปลี่ยนใช้แบบ SQL Authentication Mode ครับ จบปัญหาพวกนี้เลยครับ 
|
 |
 |
 |
 |
Date :
2013-04-23 13:58:29 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเปลี่ยนตามที่คุณเป้าแก้ไขให้แต่ error เหมือนเดิมเลยค่ะ
|
 |
 |
 |
 |
Date :
2013-04-23 14:01:53 |
By :
sukus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำตาม พี่ TC Admin
Code (VB.NET)
myconn = @"Data Source=server01;Initial Catalog=StoreDB;User Id=sa;Password=sapassword;
ตัวอย่างประมาณนี้ คือมี่User กับpass
|
 |
 |
 |
 |
Date :
2013-04-23 14:12:56 |
By :
chaloemrat32 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณสำหรับทุกท่านที่ให้ความรู้นะคะ ตอนนี้ connect ได้แล้วเลยอยาก share เผื่อเป็นประโชน์กับท่านอื่นๆ ค่ะ
Code (VB.NET)
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conStr As String = "Data Source=OSIRUS\SA;Initial Catalog=SA_TEST1;User ID=sa;Password=12345"
'Data Source = ชื่อเครื่องของ server
'Initial Catalog = ชื่อของ database
'User ID = user ของ sql
'Password = Password ของ sql
Dim connection As New SqlConnection(conStr)
Dim sql As String = "SELECT * FROM SaleTable"
Dim command As New SqlCommand(sql, connection)
Dim adapter As New SqlDataAdapter(command)
Dim data As New DataSet()
adapter.Fill(data, "SaleTable")
DataGridView1.DataSource = data.Tables("SaleTable")
connection.Close()
End Sub
End Class
|
 |
 |
 |
 |
Date :
2013-04-26 21:57:10 |
By :
sukus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2013-04-27 11:06:44 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|