none
请教高手 RRS feed

  • 问题

  •  

    在做个简单的window项目时,有代码不懂,望高手能赐教

    谢谢

    在下面的代码中在出现4个问号的是不明白的地方,能高手能指点迷津 谢谢

     

      SqlClass sqlclass = new SqlClass();//????
           /// 删除部门信息
            /// </summary>
            /// <param name="strDeptname">部门名称</param>
            public void SqlComDept(string strDeptname)
            {
                SqlConnection con =sqlclass.SqlConBind();
                con.Open();
                SqlCommand scd = new SqlCommand("delete from tb_dept where DeptName=@DeptName", con);
                SqlParameter para = new SqlParameter("@DeptName", SqlDbType.VarChar, 50);//????
                para.Value = strDeptname;//????
                scd.Parameters.Add(para);//????
                scd.ExecuteNonQuery();
                con.Close();
            }

     

     /// <summary>
            /// 添加部门信息
            /// </summary>
            /// <param name="strDeptName">部门名称</param>
            /// <param name="strDeptDirector">部门主管</param>
            /// <param name="strDeptTel">部门电话</param>
            /// <param name="strParentIndex">上级部门索引</param>
            public void SqlComDeptAdd(string strDeptName,string strDeptDirector,string strDeptTel,string strParentIndex)
            {
                SqlConnection con = sqlclass.SqlConBind();//????
                con.Open();
                SqlCommand scd = new SqlCommand("insert into tb_Dept (DeptName,DeptDirector,DeptTel,ParentIndex)values(@DeptName,@DeptDirector,@DeptTel,@ParentIndex)", con);
                SqlParameter para = new SqlParameter("@DeptName", SqlDbType.VarChar, 50);//????
                para.Value = strDeptName;//????
                scd.Parameters.Add(para);//????
                para = new SqlParameter("@DeptDirector", SqlDbType.VarChar, 50);//????
                para.Value = strDeptDirector;//????
                scd.Parameters.Add(para);//????
                para = new SqlParameter("@DeptTel", SqlDbType.VarChar, 50);//????
                para.Value = strDeptTel;//????
                scd.Parameters.Add(para);//????
                para = new SqlParameter("@ParentIndex", SqlDbType.VarChar, 50);//????
                para.Value = strParentIndex;//????
                scd.Parameters.Add(para);//????
                scd.ExecuteNonQuery();
                con.Close();
            }

     

       /// <summary>
            /// 判断部门名称是否重复
            /// </summary>
            /// <param name="strDeptName">部门名称</param>
            /// <param name="strType">操作类型</param>
            /// <returns>返回bool型</returns>
            public bool RepeatName(string strDeptName,string strType)
            {
                bool blRepeat;
                SqlConnection con = sqlclass.SqlConBind();
                con.Open();
                SqlCommand scd = new SqlCommand("select count(*) from tb_Dept where DeptName=@DeptName",con);
                SqlParameter para = new SqlParameter("@DeptName", SqlDbType.VarChar, 50);//????
                para.Value = strDeptName;//????
                scd.Parameters.Add(para);//????
                int intCount=Convert.ToInt32(scd.ExecuteScalar());//????
                if (intCount > 0)
                {
                    if (strType == "edit" && intCount == 1)//????
                    {
                        blRepeat = false;
                    }
                    else
                    {
                        blRepeat = true;
                    }
                }
                else
                {
                    blRepeat = false;
                }
                con.Close();
                return blRepeat;
            }

     

      /// <summary>
            /// 判断部门是否有下级部门
            /// </summary>
            /// <param name="strDeptID">部门ID</param>
            /// <returns>返回bool型</returns>
            public bool InferiorDept(string strDeptID)
            {
                bool blRepeat;
                SqlConnection con = sqlclass.SqlConBind();
                con.Open();
                SqlCommand scd = new SqlCommand("select count(*) from tb_Dept where ParentIndex=@DeptID", con);
                SqlParameter para = new SqlParameter("@DeptID", SqlDbType.VarChar, 50);//????
                para.Value = strDeptID;//????
                scd.Parameters.Add(para);//????
                int intCount = Convert.ToInt32(scd.ExecuteScalar());//????
                if (intCount > 0)
                {
                    blRepeat = true;
                }
                else
                {
                    blRepeat = false;
                }
                con.Close();
                return blRepeat;
            }

     


     /// <summary>
            /// 添加基本信息
            /// </summary>
            /// <param name="strName">名称</param>
            /// <param name="strTable">表名</param>
            public void AddNew(string strName,string strTable)
            {
                SqlConnection con = sqlclass.SqlConBind();
                con.Open();
                SqlCommand scd = new SqlCommand("insert into " + strTable + " (Name)values(@Name)", con);//????
                SqlParameter para = new SqlParameter("@Name", SqlDbType.VarChar, 50);//????
                para.Value = strName;//????
                scd.Parameters.Add(para);//????
                scd.ExecuteNonQuery();
                con.Close();
            }

     


      /// <summary>
            /// 删除基本信息
            /// </summary>
            /// <param name="strTable">表名</param>
            /// <param name="strid">名称ID</param>
            public void del(string strTable,string strid)
            {
                SqlConnection con = sqlclass.SqlConBind();
                con.Open();
                SqlCommand scd = new SqlCommand("delete from  " + strTable + " where id=@strid", con);//????
                SqlParameter para = new SqlParameter("@strid", SqlDbType.VarChar, 50);//????
                para.Value = strid;//????
                scd.Parameters.Add(para);//????
                scd.ExecuteNonQuery();
                con.Close();
            }

    2007年12月6日 6:28

答案

  • 这是设置SqlCommand的参数的标准做法啊。

    首先创建一个Sql参数,设置参数名称和类型以及长度。然后给参数赋值,然后把这个参数加入到Command的参数集合里面。

    建议看看ADO.NET的文章,网上多的是。

    2007年12月7日 6:19
    版主