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;"

    评论

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知