PDA

View Full Version : Kết nối tới sql bằng vb.net



vuhoa
08-08-2007, 18:48
Em đang mò vb.net, xem trong sách và làm đúng như vậy nhưng ko tạo ketnoi toi sql được. Đây là cách em làm

Vào sql tạo 1 database tên test, mật khẩu sa là 123456
Dùng VS 2003 tạo project mới, dblick lên webform gõ code sau


Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page


#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
'Khai bao bien chuoi ket noi
Dim cnnStr As String
'Khai bao doi tuong ket noi thuoc SQL
Dim cnn As SQLConnection
'Tao chuoi ket noi toi CSDL SQL server
cnnStr = "server=(local);iud=sa;pwd=123456;database=test"
'Tao doi tuong ket noi CNN
cnn = New SQLConnection
'mo ket noi
cnn.Open()
End Sub

End Class


hết ạ
Xong run thử lỗi tè le


Line 34: cnn = New SQLConnection
Line 35: 'mo ket noi
Line 36: cnn.Open()
Line 37: End Sub
Line 38:

Giúp em với, chắc mua sách khác wa' hixhix

th2013
08-08-2007, 19:05
Thường khi tạo một Web Project thì kết nối cơ sở dữ liệu sẽ được thực hiện trong File Web,config. Sau đó cứ thế mà dùng...
<appSettings>
.....
<add key="ConnectionString" value="server=(local);Connect Timeout=90000;database=MyDatabase;connection reset=true;Max Pool Size=7500;Min Pool Size=1;uid=sa;pwd=sa"/>
......
</appSettings>

Cái User và Password được tạo lúc cài SQL thì thường chọn là sa,sa
bạn thử xem nhé.

vuhoa
09-08-2007, 11:04
thanks, em gà quá nên chưa được hixxhixx
Ai làm ơn hướng dẫn chi tiết chút đi :)

saigon1990
10-08-2007, 05:35
2 chỗ bị sai.
1> iud=sa, uid mới đúng
2> cnn = New SQLConnection(cnnStr)

chào,
tn