日期:2014-05-20  浏览次数:21098 次

sql server与c# 一个小问题,搞了一个晚上,汗死.请高手指点迷津!!!
<%@   Import   Namespace= "System.Data "   %>
<%@   Import   Namespace= "System.Data.SqlClient "   %>
<html>
<head   runat= "server ">
        <title> 无标题页 </title>
</head>

<script   language= "c# "   runat= "server ">
        public   void   Page_Load(Object   sender,   EventArgs   e)
        {
                SqlCommand   Comm;
                DataSet   ds;
                SqlDataAdapter   adap;
                SqlConnection   objConnection;
           

                string   strcomm   =   "SELECT*FROM   Score ";
                string   strConnection   =   "Database=geiwei.mdf;Server=.;Trusted_Connection=SSPI;Connect   Timeout=30 ";


                objConnection   =   new   SqlConnection(strConnection);
                adap   =   new   SqlDataAdapter();


                Comm   =   new   SqlCommand(strcomm,   objConnection);
                ds   =   new   DataSet();
                adap.Fill(ds,   "score ");
                dg.DataSource   =   ds;
                dg.DataBind();


        }
       


</script>

<body>
        <form   id= "form1 "   runat= "server ">
                <asp:DataGrid   ID= "dg "   runat= "server ">
                </asp:DataGrid>
        </form>
</body>
</html>

就这么短一段代码     运行时候出错信息如下:
“/”应用程序中的服务器错误。
--------------------------------------------

在调用“Fill”前,SelectCommand   属性尚未初始化。  
说明:   执行当前   Web   请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。  

异常详细信息:   System.InvalidOperationException:   在调用“Fill”前,SelectCommand   属性尚未初始化。

源错误:  

只有在调试模式下进行编译时,生成此未处理异常的源代码才会显示出来。若要启用此功能,请执行以下步骤之一,然后请求   URL:  

1.   在产生错误的文件的顶部添加一条“Debug=true”指令。例如:  

    <%@   Page   Language= "C# "   Debug= "true "   %>

或:

2.   将以下的节添加到应用程序的配置文件中:

<configuration>
      <system.web>
              <compilation   debug= "true "/>
      </system.web>
</configuration>

请注意,第二个步骤将使给定应用程序中的所有文件在调试模式下进行编译;第一个步骤仅使该特定文件在调试模式下进行编译。