无法将类型'WebApplication4.SqlConnection'隐式转换为'System.Data.SqlClient.SqlConnection'~AddUser.aspx.cs

时间:2013-04-21 18:27:01

标签: c# asp.net sql sql-server

我尝试了很多类型,我不知道如何插入。

sql server有什么问题吗?

请快点帮助我..

SqlConnection con = new SqlConnection( "Data Source=localhost/SQLEXPRESS.Polaris.dbo;Initial Catalog=Polaris;Integrated Security=True;Pooling=False");

protected void Page_Load(object sender, EventArgs e)
{
        con.Open();
}

protected void Button3_Click(object sender, EventArgs e)
{
    con.Open();

    SqlDataReader rdr = null;
    //string s1 = "insert into Login values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DropDownList1.SelectedItem.Value + "'";
    SqlCommand cmd1 = new SqlCommand("insert into Login values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DropDownList1.SelectedItem.Value + "'");
    cmd1.Connection = con;
    rdr = cmd1.ExecuteReader();
    Label2.Visible = true;
    //EndEventHandler.RemoveAll();
 }

 protected void Button2_Click(object sender, EventArgs e)
 {
        Response.Redirect("WebForm1.aspx");
 }

2 个答案:

答案 0 :(得分:1)

  

无法隐式转换类型'WebApplication4.SqlConnection'   'System.Data.SqlClient.SqlConnection'

根据错误,您在WebApplication4名称空间中有一个名为SqlConnection的类。您可能错误地生成了该类。您需要先删除该类,然后添加对System.Data.SqlClient

的引用

答案 1 :(得分:0)

以下是您可以做的事情:

SqlConnection con = new SqlConnection( "Data Source=localhost/SQLEXPRESS.Polaris.dbo;Initial Catalog=Polaris;Integrated Security=True;Pooling=False");
protected void Button3_Click(object sender, EventArgs e)
{
    con.Open();
    string s1 = "insert into Login values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DropDownList1.SelectedItem.Value + "'";
    SqlCommand cmd = new SqlCommand(s1, con);
    cmd.ExecuteNonQuery();
}