laola的故事 2014-12-12 11:25 采纳率: 25%
浏览 1986

给数据库中添加数据,一直没成功,网页不 跳转。急!!

string connectionString = @"Data Source=DELL-PC;Initial Catalog = GXBYXS;User ID=sa;Password=123456 "; //定义字符串
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString); //创建Sqlconnection对象
// string sql = "insert into zhuce values('" + DropDownList1.SelectedValue + "," + txtname.Text + "," + txtpassword.Text + "," + txtSpassword.Text + "," + txtemail.Text + ")";
string sql=string.Format("insert into zhuce(Usertype,Username,Usermima,Usersmima,Useremail) values('{0}','{1}','{2}','{3}','{4}')", DropDownList1.SelectedValue .ToString(),
txtname.Text.ToString(), txtpassword.Text.ToString(),txtSpassword.Text.ToString(),txtemail.Text.ToString()) ;
try
{

        connection.Open();    //打开数据库的连接
        //  SqlCommand command=new SqlCommand(sql,connection);     //创建SqlCommand对象
        SqlCommand command = connection.CreateCommand();
        command.CommandText = sql;
        command.CommandType = CommandType.Text;


         int result =command.ExecuteNonQuery();
        if (result == 1)
        {

            Page.Response.Redirect("login.aspx");
        }
    }
  • 写回答

5条回答 默认 最新

  • threenewbee 2014-12-12 12:12
    关注

    if (result == 1)
    你的插入语句不会返回1。
    除非你在string sql = "..."
    下面加上sql += ";select 1;"

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog