using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public SqlConnection GetConnection()
{
  string myStr = ConnectionManager.AppSettings["ConnectiongString"].ToString();
  SqlConnection myConn = new SqlConnection(myStr);
  return myConn;
}这里有错误,提示:应输入 class、delegate、enum、interface 或 struct很多字节这么提示public partial class ado : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {    }
protected void  Button1_Click(object sender, EventArgs e)
{
    SqlConnection myConn = GetConnection();
    myConn.Open();
    string sqlStr ="select * from netstore where UserName =@Name";
    SqlCommand myCmd = new SqlCommand(sqlStr,myConn);
    myCmd.Parameters.Add("@Name",SqlDbType.VarChar,20).Value =
        this.txtName.Text.Trim();
    SqlDataAdapter myDa = new SqlDataAdapter(myCmd);
    DataSet myDs = new DataSet();
    myDa.Fill(myDs);
    if (myDs.Tables[0].Rows.Count>0)
    {
        GridView1.DataSource = myDs;
        GridView1.DataBind();
    }
    else
    {
        Response.Write("<script>alert('没有相关记录')</script>");
    }
    myDa.Dispose();
    myDs.Dispose();
    myConn.Close();}
    else
      this.bind();
}
以上是我写的一个代码,但是错误啊。

解决方案 »

  1.   

    这里有错误,提示:应输入 class、delegate、enum、interface 或 struct很多字节这么提示 
    public partial class ado : System.Web.UI.Page 

        protected void Page_Load(object sender, EventArgs e) 
        { 
    GetConnection();
        } 
    }public SqlConnection GetConnection() 

      string myStr = ConnectionManager.AppSettings["ConnectiongString"].ToString(); 
      SqlConnection myConn = new SqlConnection(myStr); 
      return myConn; 

      

  2.   

    这个方法要放在一个类里面。
    C#是面向对象的,没有全局的东西。。
    public class MyClass
    {
     public SqlConnection GetConnection() 
     { 
      string myStr = ConnectionManager.AppSettings["ConnectiongString"].ToString(); 
      SqlConnection myConn = new SqlConnection(myStr); 
      return myConn; 
     }
    }
    方法调用:
    public partial class ado : System.Web.UI.Page 

        protected void Page_Load(object sender, EventArgs e) 
        {     } 
    protected void  Button1_Click(object sender, EventArgs e) 

       MyClass myclass = new MyClass(); 
      SqlConnection myConn = myclass.GetConnection();
       .......
      }}
      

  3.   

    我是在.net中间挖,是在.CS文件中的。搞糊涂了,我是书上抄下来的,好像这个书也有问题啊,郁闷