--try
<add key="InfoCon" value="Provider=SQLOLEDB;Server=localhost;database=数据库;user id=sa;password=" />

解决方案 »

  1.   

    try:this.sqlConnection1.ConnectionString ="server=RALINLEI;uid=sa;pwd=sa;database=pubs";
      

  2.   

    private void BindGrid()
    {
    SqlConnection conn=new SqlConnection("server=localhost;user id=sa;password=sa;database=助学系统"); 
    SqlDataAdapter da=new SqlDataAdapter("select * from users,roles where users.roleid=roles.roleid",conn); 
    DataSet ds=new DataSet(); 
    da.Fill(ds,"table1"); 
    DataGrid1.DataSource=ds.Tables["table1"]; 
    DataGrid1.DataBind(); 
        
    }
    数据库可以访问了,但是我另外一个程序却报sqlserver不存在,或者拒绝访问。
    提示行在这里
    DataSet ds=new DataSet(); 
    da.Fill(ds,"table1"); 
    DataGrid1.DataSource=ds.Tables["table1"]; 这又是什么问题啊,我在学校都用得好好的!
      

  3.   

    en ,对阿!因为上面这个访问有问题,后来我就创建了一个,用得sqldataadaper来,就可以,我用自己编写的代码却不行,但是在学校却可以,不知道为什么,郁闷!
      

  4.   

    对,就是在我的电脑上,你看看连接字符串,没有什么问题的,数据库中都有这个字符串,在那个用代码编写的工程里面,同一个文件里面,用sqldataadapter,可以在数据集中看到显示数据,都是同样的数据库,但是用代码编写却出了这个问题!
      

  5.   

    你的连接字符串不一样?workstation id=RALINLEI;packet size=4096;user id= sa;password =saserver=localhost;user id=sa;password=sa;database=助学系统
      

  6.   

    为什么不用OLEDB呢???看看查询分析器怎么连的~~再帮你顶一下
      

  7.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;namespace WebProgram
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button btnAdd;
    protected System.Web.UI.WebControls.TextBox txtName;
    protected System.Web.UI.WebControls.TextBox txtPassword;
    protected System.Web.UI.WebControls.RadioButtonList RadBtn1;
    protected System.Web.UI.WebControls.DataGrid DataGrid2;
    protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
    protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
    protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
    protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
    protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
    protected System.Web.UI.WebControls.DataGrid DataGrid1;

    private void Page_Load(object sender, System.EventArgs e)
    {

    // 在此处放置用户代码以初始化页面
    if(!IsPostBack)
    {
    BindGrid();
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
    this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
    this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
    this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
    this.DataGrid2.SelectedIndexChanged += new System.EventHandler(this.DataGrid2_SelectedIndexChanged);
    // 
    // sqlDataAdapter1
    // 
    this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
    this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
    this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
    this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    /// <summary>
    /// 邦定DataGrid
    /// </summary>
    private void BindGrid()
    {
    SqlConnection conn=new SqlConnection("server=localhost;user id=sa;password=sa;database=助学系统"); 
    SqlDataAdapter da=new SqlDataAdapter("select * from users,roles where users.roleid=roles.roleid",conn); 
    DataSet ds=new DataSet(); 
    da.Fill(ds,"users"); 
    DataGrid1.DataSource=ds.Tables["users"]; 
    DataGrid1.DataBind(); 
        
    }
    /// <summary>
    ///邦定DropDownList
    /// </summary>
    public SqlDataReader BindRoles()
    {
    SqlConnection myConnection =new SqlConnection("server=localhost;user id=sa;password=sa;database=助学系统");
    SqlCommand cmd =new SqlCommand("select rolename from roles,users where users.roleid=roles.roleid",myConnection);
    myConnection.Open();
    return cmd.ExecuteReader(CommandBehavior.CloseConnection);

    }

    protected void ddlrole_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    // Response.Write( ((DropDownList)sender).SelectedItem.Text);
    //
    // //寻找第二个 DropDownList 而进行相关的操作,如绑定或其它
    // DropDownList ddlrole = this.DataGrid1.Items[this.DataGrid1.EditItemIndex].FindControl("ddlrole") as DropDownList;
    //
    // if( null != ddlrole)
    // ddlrole.Items.Add(new ListItem("ccc")); }

    /// <summary>
    /// 
    /// </summary>
    private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    SqlConnection conn=new SqlConnection("server=localhost;user id=sa;password=sa;database=助学系统"); 
    SqlDataAdapter da=new SqlDataAdapter("select * from roles",conn); 
    DataSet ds=new DataSet(); 
    da.Fill(ds,"table1"); 
    //
    // if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.EditItem)
    // {
    // DropDownList ddl =(DropDownList)e.Item.FindControl("ddlrole");
    // ddl.DataSource = ds.Tables["table1"];
    // ddl.DataTextField="rolename"; 
    // ddl.DataValueField="rolename"; 
    // ddl.DataBind(); 
    // ddl.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"roleid"))).Selected = true;
    //
    // }
    }

    /// <summary>
    /// 更新命令
    /// </summary>
    private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    SqlConnection conn = new SqlConnection("server=localhost;user id=sa;password=sa;database=助学系统");
    SqlCommand comm =new SqlCommand("update users set userid = @id,userpassword = @password ");
    SqlDataAdapter da=new SqlDataAdapter("select * from users",conn); 
    DataSet ds=new DataSet(); 
    da.Fill(ds);  string id;
    string pw;
    TextBox tb;
    tb = (TextBox)(e.Item.Cells[0].Controls[0]);
    id = tb.Text;
    tb = (TextBox)(e.Item.Cells[1].Controls[0]);
    pw = tb.Text;
    }
    /// <summary>
    /// 删除命令
    /// </summary>

    private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    if(e.CommandName == "del")
    {
    SqlConnection conn=new SqlConnection("server=localhost;user id=sa;password=sa;database=助学系统"); 
    SqlCommand comm=new SqlCommand("delete from users where userid=@id",conn); 
    SqlParameter parm1=new SqlParameter("@id",SqlDbType.Char); 
    parm1.Value=this.DataGrid1.DataKeys[e.Item.ItemIndex]; 
    comm.Parameters.Add(parm1); 
    conn.Open(); 
    comm.ExecuteNonQuery(); 
    conn.Close(); 
    BindGrid();  }
    }
    /// <summary>
    /// 编辑命令
    /// </summary>
    private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {

    DataGrid1.EditItemIndex = e.Item.ItemIndex ;
    BindGrid();
    }
    /// <summary>
    /// 取消命令
    /// </summary>
    private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    DataGrid1.EditItemIndex = -1;
    BindGrid();
    }

    /// <summary>
    /// 添加数据
    /// </summary>
    private void btnAdd_Click(object sender, System.EventArgs e)
    {
    // SqlConnection myConnection =new SqlConnection("server=localhost;uid=sa;pwd=sa;database=助学系统");
    // string t1;
    // string t2;
    // string t3;
    //
    // SqlCommand cmd = new SqlCommand("insert into users(userid,password,roleid) values ('&t1&','&t2&','&t3&')",myConnection);
    // myConnection.Open();
    // cmd.ExecuteNonQuery();
    // myConnection.Close();
    //     BindGrid();
    DataSet ds = new DataSet();
    SqlConnection myConnection = new SqlConnection("server=localhost;user id=sa;password=sa;database=助学系统");
    DataRow dr = ds.Tables["users"].NewRow();
    dr["userid"] = "thinkbank";
    dr["password"] = "thinkbank";
    dr["roleid"] = "3";
    //ds.Tables.Rows.Add(dr);
    } private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
    {

    } private void DataGrid2_SelectedIndexChanged(object sender, System.EventArgs e)
    {

    } }
    }
      

  8.   

    数据库在同一台机器上,用WINDOWS身份验证。
    server=localhost;Integrated Security=SSPI;database=你的数据库
      

  9.   

    da.Fill(ds,"table1"); 显示为红色是吧?
    我以前也出现过这个问题。是因为我的数据库设置为WINDOWS身份验证,后来重装(笨办法)改为双验证就行了。
      

  10.   

    “我在控制面板里面的用户创建了sa用户”
    你这个是WINDOWS登陆用户,不是SQL的登陆帐户(企业管理器——》安全-》登陆)
      

  11.   

    我上面有sa用户,在sql登录帐户里,但是还是提示不行啊