主题:vb+sql各位高手,路过的就伸伸援助之手!!!
lgail
[专家分:0] 发布于 2006-03-27 19:32:00
我用VB编了一个中学生学籍管理系统,后台是SQL SERVER 2000做数据库,在VB中调试通过后,想输入数据看看和数据库通不通,结果我输入了一个学生的相关信息后,提示框显示“数据库语句访问失败”。各位高手,请问:有可能是什么原因造成的呢? [em2]
回复列表 (共6个回复)
沙发
无法阻挡 [专家分:890] 发布于 2006-03-27 23:26:00
把你的连接语句发上来看看
板凳
aniude [专家分:430] 发布于 2006-03-29 13:59:00
连接?确定用户名密码是否正确
3 楼
lgail [专家分:0] 发布于 2006-04-01 10:12:00
谢谢两位高手,以下是数据库连接的模块,不知道有没有问题
'== 声明odbc32.dll中定义的ODBC API函数 ==
Public Declare Function SQLAllocEnv Lib "odbc32.dll" (phenv&) As Integer
Public Declare Function SQLAllocConnect Lib "odbc32.dll" (ByVal Henv&, phdbcd&) As Integer
Public Declare Function SQLAllocStmt Lib "odbc32.dll" (ByVal Hdbc&, phstmt&) As Integer
Public Declare Function SQLConnect Lib "odbc32.dll" (ByVal Hdbc&, ByVal szDSN$, _
ByVal cbDSN%, ByVal szUID$, ByVal cbUID%, ByVal szPWD$, ByVal cbPWD%) As Integer
Public Declare Function SQLColAttributes Lib "odbc32.dll" (ByVal Hstmt&, ByVal icol%, _
ByVal fDescType%, ByVal rgbDesc As String, ByVal cbDescMax%, pcbDesc%, pfDesc&) As Integer
Public Declare Function SQLDisconnect Lib "odbc32.dll" (ByVal Hdbc&) As Integer
Public Declare Function SQLExecDirect Lib "odbc32.dll" (ByVal Hstmt&, ByVal szSqlStr$, _
ByVal cbSqStr&) As Integer
Public Declare Function SQLFetch Lib "odbc32.dll" (ByVal Hstmt&) As Integer
Public Declare Function SQLFreeConnect Lib "odbc32.dll" (ByVal Hdbc&) As Integer
Public Declare Function SQLFreeEnv Lib "odbc32.dll" (ByVal Henv&) As Integer
Public Declare Function SQLFreeStmt Lib "odbc32.dll" (ByVal Hstmt&, ByVal fOption%) As Integer
Public Declare Function SQLGetData Lib "odbc32.dll" (ByVal Hstmt&, ByVal icol%, ByVal fCType%, _
ByVal rgbValue As String, ByVal cbValueMax&, pcbValue%) As Integer
Public Declare Function SQLNumResultCols Lib "odbc32.dll" (ByVal Hstmt&, pccol%) As Integer
Public Declare Function SQLGetDiagRec Lib "odbc32.dll" (ByVal HandleType%, ByVal Handle&, _
ByVal RecNumber%, Sqlstate%, NativeErrorPtr%, MessageText As String, _
ByVal Bufferlenchgth%, TextlenchgthPtr%)
'== 标记数据库是否连接 ==
Private IsConnect As Boolean
'== 标记执行Connect()函数后,访问数据库的次数 ==
Private Connect_Num As Integer
Private Henv As Long '环境句柄
Private Hdbc As Long '连接句柄
Private Rc As Long
Public Hstmt As Long
'连接数据库
Private Sub Connect()
Dim TmpStat As Long 'SQLConnect()函数的返回值
'如果连接标记为真,则返回。否则会出错
If IsConnect = True Then
Exit Sub
End If
'分配环境句柄,保存在变量Henv中
If SQLAllocEnv(Henv) Then
MsgBox "无法初始化ODBC环境!", , "ODBC API执行错误"
End
End If
'根据环境句柄,分配连接句柄,保存在变量Hdbc中
If SQLAllocConnect(Henv, Hdbc) Then
MsgBox "无法连接ODBC!", , "ODBC API执行错误"
End
End If
'根据连接句柄、数据源、用户名和密码连接指定的数据库
TmpStat = SQLConnect(Hdbc, DSN, Len(DSN), DB_USER_NAME, _
Lench(DB_USER_NAME), DB_PASSWORD, Len(DB_PASSWORD))
'如果连接不成功则退出程序
If TmpStat <> SQL_SUCCESS And _
TmpStat <> SQL_SUCCESS_WITH_INFO Then
MsgBox "无法获得连接句柄!", , "ODBC API执行错误"
IsConnect = True
Disconnect
End
End If
'设置连接标记
IsConnect = True
End Sub
'断开与数据库的连接
Private Sub Disconnect()
Dim Rc As Long
'如果连接标记为假,标明已经断开连接,则直接返回
If IsConnect = False Then
Exit Sub
End If
'断开连接
Rc = SQLDisconnect(Hdbc)
'释放连接句柄
Rc = SQLFreeConnect(Hdbc)
'释放环境句柄
Rc = SQLFreeEnv(Henv)
IsConnect = False
End Sub
'使用Connect_Num控制数据库连接
Public Sub DB_Connect()
Connect_Num = Connect_Num + 1
Connect
End Sub
Public Sub DB_Disconnect()
If Connect_Num >= CONNECT_LOOP_MAX Then
Connect_Num = 0
Disconnect
End If
End Sub
'强制关闭api方式访问的数据库,计数器复位
Public Sub DBapi_Disconnect()
Connect_Num = 0
Disconnect
End Sub
'执行ODBC数据库操作语句
Public Sub OdbcExt(ByVal TmpSQLstmt As String)
'根据连接句柄,分配语句句柄
If SQLAllocStmt(Hdbc, Hstmt) Then
MsgBox "句柄分配失败", , "ODBC API执行错误"
DBapi_Disconnect
End
End If
'执行SQL语句,Lench是用户自定义函数,计算包含汉字的字符串长度
If SQLExecDirect(Hstmt, TmpSQLstmt, Lench(TmpSQLstmt)) Then
MsgBox "数据库访问语句执行失败", , "ODBC API执行错误"
MsgBox TmpSQLstmt
DBapi_Disconnect
End
End If
End Sub
'使用Data控件连接数据库,将执行指定的SQL语句
Public Sub DBdataExt(TmpData As Data, ByVal TmpSource As String)
'关闭已有的ODBC连接
DBapi_Disconnect
'Data控件连接数据库
TmpData.Connect = "ODBC;DATABASE=" + DATABASE _
+ ";UID=" + DB_USER_NAME + ";PWD=" _
+ DB_PASSWORD + ";DSN=" + DB_NAME
TmpData.RecordSource = TmpSource
TmpData.Refresh
End Sub
我是按时参考书做的“中学生学籍管理系统”,我并没有做登录窗体,会影响软件的运行吗?
4 楼
lgail [专家分:0] 发布于 2006-04-01 17:04:00
我设计的这个“中学生学籍管理系统”中输入班级信息和输入课程信息这两个模块都没有问题,唯有输入学生信息时,显示“数据库语句访问失败”,进而提示“INSERT INTO Students(......)Values(......),中间的字段语句我没有写。问题出在哪里呢?我急啊!高手们帮帮忙!
5 楼
xj01132 [专家分:150] 发布于 2006-04-02 18:41:00
不会吧!这么多代码呀!
我的也是用样的问题不知道什么问题!代码如下!
Dim con As String = "server=FU;uid=sa;pwd=fuxiaowen;database=book"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim scnd As String = "select * from book"
Dim a As Integer
Dim tablename As String = "book"
Dim dtdata As New Data.DataTable
Getdatabase(scnd, tablename, dtdata)
If dtdata.Rows.Count > 0 Then
' ListBox1.DataSource = dtdata
For a = 0 To dtdata.Rows.Count - 1
Label1.Text &= Trim(dtdata.Rows(a)("bookname") & ",")
Next
End If
End Sub
Public Sub Getdatabase(ByVal sqlstr As String, ByVal tablename As String, ByRef dtdata As Data.DataTable)
Dim dsdata As New Data.DataSet
Dim cmd As New SqlCommand
Dim cnd As New SqlConnection(strsql)
Try
cnd.Open()
cmd.Connection = cnd
cmd.CommandType = CommandType.Text
Dim rs As New SqlDataAdapter(sqlstr, cnd)
rs.Fill(dsdata, "book")
dtdata = dsdata.Tables("book")
Catch ex As Exception
cnd.Close()
End Try
End Sub
6 楼
xj01132 [专家分:150] 发布于 2006-04-02 18:43:00
VB2005,帮帮忙,谢谢!
我来回复