程序运行到“cn.open();” 就报错了
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.Configuration;
using System.Data.SqlClient;
namespace TMS
{
public class _Default : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox tbx_uid;
protected System.Web.UI.WebControls.TextBox tbx_upassword;
protected System.Web.UI.WebControls.Button btn_login;
protected System.Web.UI.WebControls.Label lbl_message;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
private void btn_login_Click(object sender, System.EventArgs e)
{
string strconn= ConfigurationSettings.AppSettings["dsn"];
SqlConnection cn= new SqlConnection (strconn);
cn.Open ();
string mysql= "select * from Users where Uid='"+tbx_uid.Text +"' and UPassword='"+tbx_upassword.Text +"'";
SqlCommand cm=new SqlCommand (mysql,cn);
SqlDataReader dr=cm.ExecuteReader ();
if(dr.Read ())
{
lbl_message.Text= "";
Session["uid"]=dr["uid"].ToString ();
Session["upower"]=dr["upower"].ToString ();
lbl_message.Text="欢迎您!"+Session["uid"];
if(Session["upower"].ToString ()=="1")
{
Response.Redirect ("contractcount.aspx");
}
else if(Session["upower"].ToString ()=="2")
{
Response.Redirect ("contractlist.aspx");
}
}
else
{
lbl_message.Text ="对不起,您的用户名/密码不正确,请重新输入";
}
//关闭连接
cn.Close();
web.config代码:
<appSettings>
<add key="dsn" value="persist security info=False;Integrated Security=SSPI;server=localhost;Trusted_Connection=true;DATABASE=TMS"/>
</appSettings>
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;user id=sa;password="
cookieless="false"
timeout="20"
/>