System.InvalidOperationException: ConnectionString 属性尚未初始化。 在 System.Data.SqlClient.SqlConnection.PermissionDemand() 在 System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection) 在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) 在 System.Data.SqlClient.SqlConnection.Open() 在 GradeMis.Login.userLogin() 位置 H:\GradeMis\Login.aspx.cs:行号 41 是什么意思啊?
高手快救救我啊!!
高手快救救我啊!!
private void userLogin()
{
string sql = string.Empty;
string user = this.txtUsername.Text;
string pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtPassword.Text,"MD5");
sql = string.Format("select username,password,[group] from admin where username='{0}' and password='{1}'",user,pwd);
SqlDataReader dr;
SqlCommand cmd = new SqlCommand(sql,conn);
try
{
SqlConnection Conn = new SqlConnection(Conn);
conn.Open(); dr = cmd.ExecuteReader();
if(dr.Read())
{
Session["username"] = dr["username"].ToString();
Session["group"] = dr["group"].ToString();
Response.Redirect("Main.aspx");
}
else
{
Response.Write("<script>alert('用户名或者密码错误,请重新输入!')</script>");
}
}
catch(Exception ex)
{
Response.Write(ex);
}
finally
{
conn.Close();
}
}
#endregion
哪里有错吗?
try
{
SqlConnection Conn = new SqlConnection(Conn);
conn.Open();
写反了 没连接你就先执行了
conn.Open();
SqlDataReader dr;
SqlCommand cmd = new SqlCommand(sql,conn);
先打开,再执行
这句有问题,SqlConnection的参数应该是连接数据库的字符串。
"Data Source='计算机名称、也可以使IP地址、如果是本机可以用'.'表示';Initial Catalog=数据库名称;Integrated Security=True"如果非windows登陆、 还需要账号和密码、 在上面的字符串后面加、'User ID=sa;pwd=123'
希望能帮到你、
你的程序对这两个变量声明没有,有的话贴出来大家看看。给楼主一个网站如何编写连接字符串的网站很好www.connectionstrings.com
public class Login : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox txtPassword;
protected System.Web.UI.WebControls.TextBox txtUsername;
private static readonly string connestr = ConfigurationManager.ConnectionStringS["GradeSysConnectionString"].ToString();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region
private void userLogin()
{
string sql = string.Empty;
string user = this.txtUsername.Text;
string pwd =System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtPassword.Text,"MD5");
sql = string.Format("select username,password,[group] from admin where username='{0}' and password='{1}'",user,pwd);
SqlConnection Conn = new SqlConnection(Connestr);
SqlDataReader dr;
SqlCommand cmd = new SqlCommand(sql,Conn);
try
{
Conn.Open(); dr = cmd.ExecuteReader();
if(dr.Read())
{
Session["username"] = dr["username"].ToString();
Session["group"] = dr["group"].ToString();
Response.Redirect("Main.aspx");
}
else
{
Response.Write(" <script>alert('用户名或者密码错误,请重新输入!') </script>");
}
}
catch(Exception ex)
{
Response.Write(ex);
}
finally
{
conn.Close();
}
}
#endregion
SqlDataReader dr;
SqlCommand cmd = new SqlCommand(sql,Conn);
try
{
Conn.Open(); dr = cmd.ExecuteReader();
把那个conn.open()写到
SqlConnection Conn = new SqlConnection(Connestr);
Conn.Open(); 、、、、、、、、、、、、、、、、、、。。
SqlDataReader dr;
SqlCommand cmd = new SqlCommand(sql,Conn);
try
{
SqlConnection Conn = new SqlConnection(Connestr); 需要先把Connestr赋值