真正实现将注册的账户密码写入数据库(就是程序运行后再去数据库中能查看到数据),而不是写入内存中,具体的语句是什么?我是新手,求大家详细指点指点,非常感谢。
        //连接字符串
        ConnectionStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\360data\重要数据\我的文档\用户注册信息.mdf;
        Integrated Security=True;Connect Timeout=30;User Instance=True";
        //创建连接对象
        SqlConnection sqlCon = new SqlConnection(ConnectionStr);        //建立插入命令字符串
        insertStr = "INSERT INTO 注册信息保存 (username,password,e-mail) VALUES ('"
            + TextBox1.Text + "','" + TextBox2.Text
            + "','"+TextBox4.Text+"')";
        try
        {
            //打开数据
            sqlCon.Open();
            if (sqlCon.State == ConnectionState.Open)
            {
                //创建命令对象
                SqlCommand sqlComm = new SqlCommand(insertStr, sqlCon);               
            }
        }
        catch
        {
            if (sqlCon.State != ConnectionState.Open)
            {            }
        }
        finally
        {
            //关闭数据库
            sqlCon.Close();
        }
在这代码中少了什么语句?求大家详细指点指点,非常感谢。

解决方案 »

  1.   

    SqlCommand sqlComm = new SqlCommand(insertStr, sqlCon);
    sqlComm.ExecuteNonQuery();
      

  2.   

    本帖最后由 net_lover 于 2012-05-20 09:42:49 编辑
      

  3.   

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "" || TextBox2.Text == "" || TextBox3.Text == "" || TextBox4.Text == "")
            {
                Label1.Text = "注册信息没有填写完整";
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
            string ConnectionStr, insertStr;
            
                //连接字符串
                ConnectionStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\360data\重要数据\我的文档\用户注册信息.mdf;
            Integrated Security=True;Connect Timeout=30;User Instance=True";
                //创建连接对象
                SqlConnection sqlCon = new SqlConnection(ConnectionStr);            //建立插入命令字符串
                insertStr = "INSERT INTO 注册信息保存 ([username],[password],[e-mail]) VALUES (@username,@password,@email)";
                try
                {
                    //打开数据                if (sqlCon.State != ConnectionState.Open)
                    {
                        sqlCon.Open();
                        //创建命令对象
                        SqlCommand sqlComm = new SqlCommand(insertStr, sqlCon);
                        sqlComm.Parameters.AddWithValue("@username", TextBox1.Text);
                        sqlComm.Parameters.AddWithValue("@password", TextBox2.Text);
                        sqlComm.Parameters.AddWithValue("@email", TextBox4.Text);
                        sqlComm.ExecuteNonQuery();                }
                }
                catch
                {            }
                finally
                {
                    //关闭数据库
                    sqlCon.Close();
                }
            }
    是不是我语句位置放错了?应该放哪?
      

  4.   

    参数化查询是为了避免SQL注入攻击
      

  5.   

    catch (System.Exception ex)
         {                     
               MessageBox.Show(ex.Message);                 
          }
    最好catch把错误信息提示出来,这样哪里错了,你比较好找
      

  6.   


    在我们这里,你这种代码的性质是“不懂sql语法”的。因为t-sql语法规定了,必须转换字符串常量中的单引号为两个两个单引号。你的代码转换了吗?如果代码写成这样,是不能上岗编写数据库操纵程序的,可以重新回学校学习sql语法。
      

  7.   

    这要写成insertStr = "INSERT INTO 注册信息保存 (username,password,e-mail) VALUES ('"
    + TextBox1.Text.Replace("'","''") + "','" + TextBox2.Text.Replace("'","''")
    + "','"+TextBox4.Text.Replace("'","''")+"')";才对。但是这本来应该是常识,你把它另类说成是“麻烦的“‘ ”’符号”那就体现出非常缺乏必要的sql知识了。一定要记住这个基本常识。
      

  8.   

    另外,写 try...catch...finally毫无必要。using(){...}语法本来就是干这个用的,使用优雅的语法可以帮助你把精力放在更重要的事情上。例如你可以写using(var sqlCon = new SqlConnection(ConnectionStr))        try
    {
        sqlCon.Open();
        SqlCommand sqlComm = new SqlCommand(insertStr, sqlCon);
        sqlComm.ExecuteNoneQuery();
    }足够了。