--try
<add key="InfoCon" value="Provider=SQLOLEDB;Server=localhost;database=数据库;user id=sa;password=" />
<add key="InfoCon" value="Provider=SQLOLEDB;Server=localhost;database=数据库;user id=sa;password=" />
调试欢乐多
{
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"]; 这又是什么问题啊,我在学校都用得好好的!
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)
{
} }
}
server=localhost;Integrated Security=SSPI;database=你的数据库
我以前也出现过这个问题。是因为我的数据库设置为WINDOWS身份验证,后来重装(笨办法)改为双验证就行了。
你这个是WINDOWS登陆用户,不是SQL的登陆帐户(企业管理器——》安全-》登陆)