回 帖 发 新 帖 刷新版面

主题:我这段数据库代码连接不上,请问高手如何解决?

<%@ Page Language=“VB”ContentType="text/html"Debug="true"%>
<%@ Import Namespace="System.Data.sqlClient"%>
<%@ Import Namespace="System.Data"%>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="学生基本信息.aspx.vb" Inherits="WebApplication1.学生基本信息"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
        <title>学生基本信息</title>
        <meta http-equiv="Content-Type" content="text/html;charset=gb2312">
        <script language="vb" runat="serve">
        dim StuSql as string
        
        Function MakeSelectSql()as string
              dim SqlStr as string
              if Trim(编号.Text)<>""then
                  SqlStr="where 编号like '%"+Trim(编号.Text)+"%"
              else
                  SqlStr+="where 编号 like'%"
              end if
              if Trim(编号.Text)<>""then
                  SqlStr+="and 编号like '%"+Trim(姓名.Text)+"%"
              end if
              if Trim(籍贯.Text)<>""then
                  SqlStr+="and 编号like '%"+Trim(籍贯.Text)+"%"
              end if
              MakeSelectSql=SqlStr
        End Function
        
        Sub BindGrid()
            dim SqlConn as new sqlConnedtion()
            dim SqlConn as new sqlCommand()
            dim SqIDs as new DataSet()
            dim SqlAdp as new sqlDataAdaper()
            dim ConnStr as string
            ConnStr="Server=COMPUTER;Database=谢庄实验室管理;UID=sa;PWD=;"
            SqlConn.ConnectionString=ConnStr
            SqlConn.Open()
            SqlCmd.Connection=SqlConn
            StuSql=MakeSelectSql()
            SqlCmd.CommandText="Select学生姓名 as 编号,姓名,性别,籍贯,毕业论文
                from 学生基本信息,学生学习情况”
            SqlAdp.SelectCommd=SqlCmd
            SqlAdp.fill(SqlIDs,"学生基本信息")
            StuDataGrid.Datasource=SqlDs.Tables"学生基本信息".defaultview
            StuDataGrid.DataBind()
            SqlComm.Close()
        End Sub
        
        Sub page_load(sender as Object,e as eventArgs)
            if Not IsPostBack then
               StuSql=""
               BindGrid()
            End if
        End Sub
        
        Sub AtPageIndexChanged(sender as Object,e as DataGridPageChangedEventArgs)
            StuSql=MakeSelectSql()
            StuDataGrid.CurrentPageIndex=e.NewPageIndex
            BindGrid()
        End Sub
        
        Sub SearchStu(sender as Object,e as EventArgs)
            StuDataGrid.CurrentPageIndex=0
            StuSql=MakeSelectSql()
            BindGrid()
        End Sub
        </script>
        <style type="text/css">
        .style1 { FONT-SIZE: 24px; FONT-FAMILY: "楷体_GB2312" }
        .style2 { COLOR: #0000ff }
        </style>
</HEAD>

回复列表 (共2个回复)

沙发

其它的我觉得没有错误
但这句有问题StuDataGrid.Datasource=SqlDs.Tables"学生基本信息".defaultview
应该是StuDataGrid.Datasource=SqlDs.Tables("学生基本信息").defaultview
我遇到过这样的问题

板凳

1楼正确,另外:
     1、既然你的DataGrid支持分页,应该在BindGrid()过程中设置CurrentIndex属性,否则永远只能显示第一页;
     2、连接字符串中"编号"和"Like"应该用空格格开。
     3、BindGrid()过程中的最后一行代码中sqlcomm改成sqlconn
     4、建议你使用代码分离,那样VS.NET会自动检查你的代码,你的代码里面很多地方拼写错误。

我来回复

您尚未登录,请登录后再回复。点此登录或注册