现在的位置: 首页 > 综合 > 正文

带参数的简单SQL语句 ExcuteSQLParm(ExecuteNonQuery方法)

2013年12月06日 ⁄ 综合 ⁄ 共 928字 ⁄ 字号 评论关闭

        //初始化参数
        SqlParameter myparm = new SqlParameter();
        //获取参数的名字
        myparm.ParameterName = TxtParm.Text;
        //设置变量的类型和长度
        myparm.SqlDbType = SqlDbType.NVarChar;
        myparm.Size = 20;
        //获取参数的值
        myparm.Value = TxtValue.Text;
        //获取要执行的命令
        string sql = TxtSql.Text;
        SqlCommand cmd = new SqlCommand();
        //定义对象资源保存的范围,一旦using范围结束,将释放对方所占的资源
        using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
        {
            //打开连接
            conn.Open();
            //调用执行方法
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, myparm);
           
            Response.Write("<font color=red>操作完成!请检查数据库!</font>");
        }

前台

参数名:<asp:TextBox ID="TxtParm" runat="server" Text="id"></asp:TextBox>
 参数值:<asp:TextBox ID="TxtValue" runat="server" Text="2"></asp:TextBox>
要执行的语句:<asp:TextBox ID="TxtSql" runat="server"  Text="update myTable set name='myName' where id=@id"></asp:TextBox

抱歉!评论已关闭.