注册 登录
编程论坛 ASP.NET技术论坛

ConnectionString 属性尚未初始化

肖亭 发布于 2013-10-24 09:33, 754 次点击
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class Default2 : System.Web.UI.Page
{
    protected bool fangfa(string k)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["con"]);
       con.Open();
        string sqlsel = "select count(*) from tb_User where Name='" + k + "'";
        SqlCommand com = new SqlCommand(sqlsel, con);
        if (Convert.ToInt32(com.ExecuteScalar()) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "")
        {
            if (fangfa(TextBox1.Text))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('用户名已存在')</script>");
            }
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('可以注册')</script>");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('用户名不能为空')</script>");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string A = TextBox1.Text;
        string B = TextBox2.Text;
        string C = TextBox3.Text;
        string D = TextBox4.Text;
        string E = TextBox5.Text;
        string F = TextBox6.Text;
        if (!fangfa(TextBox1.Text))
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["con"]);
            con.Open();
            string sql = "insert into tb_User values('" + A + "','" + B + "','" + C + "','" + D + "','" + E + "','" + F + "','" + DateTime.Now.ToString() + "')";
            SqlCommand com = new SqlCommand(sql, con);
            if (Convert.ToInt32(com.ExecuteScalar()) > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "true", "<script>alert('注册成功')</script>");
                fangfa2(A, B, F);
                TextBox1.Text = TextBox2.Text = TextBox1.Text = TextBox3.Text = TextBox4.Text = TextBox5.Text = TextBox6.Text = "";
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "false", "<script>alert('注册失败')</script>");
            }
            con.Open();
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('用户名已存在')</script>");
        }

    }
    protected bool fangfa2(string A, string B,string F)
    {
      
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["con"]);
        con.Open();
        string sqls = "select count(*) from tb_User where Name='" + A + "'and Pass='" + B + "'and Email='"+F+"'";
        SqlCommand com = new SqlCommand(sqls, con);
        if (Convert.ToInt32(com.ExecuteScalar()) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
   
}
3 回复
#2
qq6454851652013-11-12 16:52
你的con和你的webconfiger里一样吗?
#3
wangnannan2014-01-03 09:02
很多朋友都或许遇到过"ConnectionString属性尚未初始化"的提示,根本原因是没有能读到key的值。这时需要校正下配置文件中"con",即确认配置文件路径、名称、密码等输入是否正确

 

#4
hafoking2014-02-10 08:17
回复 3楼 wangnannan
正解
1