哪位高人看一下
我要用ASP.NET连接MYSQL,并且想在网页上显示数据
按照书上写的
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "Default.aspx.cs " Inherits= "_Default " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head id= "Head1 " runat= "server ">
<title> 无标题页 </title>
</head>
<%@ Import Namespace= "System.Data.Odbc " %>
<script language= "C# " runat= "server ">
protected void Page_Load(object sender, EventArgs e)
{
String connstr = @ "DSN=MYSQL; ";
OdbcConnection myConnection = new OdbcConnection(connstr);
myConnection.Open();
OdbcCommand myCommand = new OdbcCommand( "select * from student ", myConnection);
MyDataGrid.DataSource = myCommand.ExecuteReader();
MyDataGrid.DataBind();
myConnection.Close();
}
</script>
<body>
<aspataGrid ID= "MyDataGrid " runat= "server " Height= "166px " Width= "447px ">
</aspataGrid>
</body>
</html>
.但是运行时网页变成下面这样子
“/WebSite21”应用程序中的服务器错误。
--------------------------------------------
ERROR [3D000] [MySQL][ODBC 3.51 Driver][mysqld-5.0.18-nt]No database selected
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Data.Odbc.OdbcException: ERROR [3D000] [MySQL][ODBC 3.51 Driver][mysqld-5.0.18-nt]No database selected
源错误:
------解决方案--------------------MyDataGrid.DataSource = myCommand.ExecuteReader();
Datagrid 的source不能是reader的数据,应该是dataset
所以你的错误信息是
:][mysqld-5.0.18-nt]No database selected