C#(提示错误SqlDataReader dr = cmd.ExecuteReader();处lConnection尚未初始化)
publicpartialclassadmin_Geren_xiugai:System.Web.UI.Page{stringname;DataBased=newDataB...
public partial class admin_Geren_xiugai : System.Web.UI.Page
{
string name;
DataBase d = new DataBase();
Dao_Geren a = new Dao_Geren();
Model_Jianli g = new Model_Jianli();
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null)
Response.Redirect("../login.aspx");
else
{
name = Session["username"].ToString();
a.QueryByUsername(name);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
this.TextBox1.Text = dr[1].ToString();
this.TextBox2.Text = dr[2].ToString();
this.TextBox3.Text = dr[6].ToString();
this.TextBox4.Text = dr[7].ToString();
this.TextBox6.Text = dr[10].ToString();
this.TextBox7.Text = dr[5].ToString();
this.TextBox8.Text = dr[8].ToString();
}
dr.Close();
}
}
请各位大大帮忙修改下代码 谢谢 展开
{
string name;
DataBase d = new DataBase();
Dao_Geren a = new Dao_Geren();
Model_Jianli g = new Model_Jianli();
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null)
Response.Redirect("../login.aspx");
else
{
name = Session["username"].ToString();
a.QueryByUsername(name);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
this.TextBox1.Text = dr[1].ToString();
this.TextBox2.Text = dr[2].ToString();
this.TextBox3.Text = dr[6].ToString();
this.TextBox4.Text = dr[7].ToString();
this.TextBox6.Text = dr[10].ToString();
this.TextBox7.Text = dr[5].ToString();
this.TextBox8.Text = dr[8].ToString();
}
dr.Close();
}
}
请各位大大帮忙修改下代码 谢谢 展开
2个回答
展开全部
真不知道你代码是怎么写的。
SqlConnection con = new SqlConnection("Server=.;DataBase=数据库名;uid=sa;pwd=密粗正毕码");//岩芹此处你实例化SqlConnection对象时没有用到连接字符串。
SqlCommand cmd = new SqlCommand("select * from 表名",con);/清毁/此处你实例化SqlCommand对象里没有指定这个对象的Connection属性,也没有Sql查询语句;
接着你才可以用SqlDataReader dr = cmd.ExecuteReader();
SqlConnection con = new SqlConnection("Server=.;DataBase=数据库名;uid=sa;pwd=密粗正毕码");//岩芹此处你实例化SqlConnection对象时没有用到连接字符串。
SqlCommand cmd = new SqlCommand("select * from 表名",con);/清毁/此处你实例化SqlCommand对象里没有指定这个对象的Connection属性,也没有Sql查询语句;
接着你才可以用SqlDataReader dr = cmd.ExecuteReader();
追问
SqlConnection con = new SqlConnection("Server=.;DataBase=rencai;uid=sa;pwd=123");
SqlCommand cmd = new SqlCommand("select * from jianli",con);此处提示错误
错误提示(A field initializer cannot reference the non-static field, method, or property 'admin_Geren_xiugai.con'
)我是个C#初学者,麻烦帮改下,非常感谢
追答
SqlConnection con = new SqlConnection("Server=.;DataBase=rencai;uid=sa;pwd=123");
//此处加上con.Open();试试
SqlCommand cmd = new SqlCommand("select * from jianli",con);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询