using System;
using System.Data.SqlClient;public partial class conado : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection myCon = new SqlConnection("initial catalog=project2; data source = localhost; User Id = sa; pwd = sa");
            try
            {
                myCon.Open();
                SqlCommand myCmd;
                myCmd = new SqlCommand("select * from admin", myCon);
                SqlDataReader reader = myCmd.ExecuteReader();
                this.myrepeater.DataSource = reader;
                this.myrepeater.DataBind();
            }
            finally
            {
                myCon.Close();
            }
        }
    }
    private void myrepeater_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs s)
    {
        this.lbBuy.Text = s.CommandArgument.ToString();
    }
}<%@ Page Language="C#" AutoEventWireup="true" CodeFile="conado.aspx.cs" Inherits="conado" Debug="true"%> 
<!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> 
<body> 
    <form id="form1" runat="server" method="post"> 
    <div> 
    <asp:Repeater ID="myrepeater" runat="server"> 
    <HeaderTemplate> 
        <table border="1"> 
            <tr> 
                <td>作者</td> 
                <td>地址</td> 
                <td>联系</td> 
            </tr> 
         
    </HeaderTemplate> 
    <ItemTemplate> 
        <tr> 
            <td><%# DataBinder.Eval(Container.DataItem, "adminID")%></td> 
            <td><%# DataBinder.Eval(Container.DataItem, "adminName")%></td> 
            <td><asp:Button ID="btBuy" Text="联系" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"adminType") %>' runat="server"> 
            </asp:Button></td> 
        </tr> 
    </ItemTemplate> 
    <FooterTemplate> 
        </table> 
    </FooterTemplate> 
    </asp:Repeater> 
    <asp:Label ID="lbBuy" runat="server"></asp:Label> 
    </div> 
    </form> 
</body> 
</html>  这是我单独页面来连接..可是他会提示
:用户 'root' 登录失败。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 用户 'sa' 登录失败。源错误: 
行 11:             try
行 12:             {
行 13:                 myCon.Open();
行 14:                 SqlCommand myCmd;
行 15:                 myCmd = new SqlCommand("select * from admin", myCon);
 
为什么会这样呢..我用的工具是vs2008和mysql数据库..

解决方案 »

  1.   

    initial catalog=project2; data source = localhost; User Id = sa; pwd = sa改成server=你的SQL服务器的名字;database=你的数据库的名称;uid=登陆该数据库的用户名;pwd=登陆该数据库的密码
      

  2.   

    例如:server=.;uid=sa;pwd=123;database=Mydb就是: 本地服务器;
    数据库是 Mydb
    用户名和密码分别是 sa 和 123
      

  3.   

    SqlConnection myCon = new SqlConnection("initial catalog=project2; data source = localhost; User Id = sa; pwd = sa"); 改成SqlConnection myCon = new SqlConnection("server=(local);uid=sa;pwd=sa;database=project2;"); 
      

  4.   

    SqlConnection myCon = new SqlConnection("server=(local);uid=sa;pwd=sa;database=project2;"); 改成这样还是提示上面提示的错误登陆.密码帐户都没有错的..
      

  5.   

    1、检查 用户名 密码。 
    注意:连接数据字符串“ server=你的SQL服务器的名字;”,.net最好不要用server,可能会报错。
    2、Sqldatareader 可以用来绑定,绑定到datalist或者datagrid,可是可以,不好。   
     Sqldatareader是一个只读向前格式,所以他无法像dataset一样分页,最好是使用dataset .
        
      

  6.   

    如果你是用的SQL2000,请先用查询分析器,以SQL server身份验证的方式登录一下,看能否成功登录,如果不能,说明你的sa账户可能有问题,如密码错误,或者说你安装的时候没有设置密码,以空密码登录试试,如果你用的是SQL 2005 ,用sql server management studio 采用同样的方式偿试登录。登录,默认的数据库应为project2,如果不是,或者无法登录,说明SQL设置有问题,检查再重新设置,一定要能正常登录。如果能登录,说明账户状态正常,但无法显示页面,或者提示同样的错误,检查你的连字符串。建设如下格式。connectionString="Data Source=(local);Initial Catalog=project2;Persist Security Info=True;User ID=sa;Password=sa" providerName="System.Data.SqlClien"
      

  7.   

    Lz不是sa登录的吧!
    换成sa/sa登录试试看!