使用存储过程将文本框值转换为数据库

时间:2014-10-10 19:27:03

标签: c# sql sql-server stored-procedures

我正在学习业务逻辑和数据逻辑以及程序可读性。

我有1个带有所有存储过程的DL(数据逻辑)类,然后我有一个连接DL和BL的BL(业务逻辑)。 然后我有一个表单类,我可以在其中添加新用户到数据库。 如何连接表单,BL和DL? DL类存储过程:

public DataSet InsertClubRoles(int RoleID, string Roledesc, string Createdby)
        {
            DataSet mydataset = new DataSet();
            SqlCommand cmdclubroles = new SqlCommand();
            //get stored procedure
            cmdclubroles.CommandText = "usp_insertrlubroles";
            cmdclubroles.CommandType = CommandType.StoredProcedure;     
            //connect
            cmdclubroles.Connection = jadcoreConnection.GetConnection();
            //parameters
            cmdclubroles.Parameters.AddWithValue("@RoleID", Convert.ToInt32(RoleID));
            cmdclubroles.Parameters.AddWithValue("@Roledesc", Roledesc);
            cmdclubroles.Parameters.AddWithValue("@Createdby", Createdby);

            SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(cmdclubroles);
            mySqlDataAdapter.Update(mydataset); 

            return mydataset;
        }

继承业务逻辑:

public DataSet insertingclubroles(int RoleID, string Roledesc, string Createdby)
{
    DataSet ids = new DataSet();

    ids = new GetClubRoles().InsertClubRoles(RoleID, Roledesc, Createdby);

    return ids;

}

最后,这里提交按钮并以另一种形式保存角色方法:

private void Savebtn_Click(object sender, EventArgs e)
{
    try
    {
        saverole();
        MessageBox.Show("Club role saved successfuly");                     
    }
    catch (Exception er)
    {
        MessageBox.Show(er.Message.ToString());
    }
}

public void saverole()
{
    ClubRoles cr = new ClubRoles();
    cr.insertingclubroles(2,"as","me");

    //need text for description
    //need select which client is selected
}

如何连接这三个?我知道文本框值需要分配给存储过程参数,但是如何?

提前致谢,对不起,如果这太久了,试着尽可能地解释它。

1 个答案:

答案 0 :(得分:0)

要实际 INSERT 数据,您应该在.ExecuteNonQuery上使用SqlCommand方法 - 类似这样的内容:

public void InsertClubRoles(int RoleID, string Roledesc, string Createdby)
{
    using (SqlCommand cmdclubroles = new SqlCommand()) 
    {
        // get stored procedure
        cmdclubroles.CommandText = "usp_insertrlubroles";
        cmdclubroles.CommandType = CommandType.StoredProcedure;     

        // connection
        cmdclubroles.Connection = jadcoreConnection.GetConnection();

        // parameters
        cmdclubroles.Parameters.AddWithValue("@RoleID", Convert.ToInt32(RoleID));
        cmdclubroles.Parameters.AddWithValue("@Roledesc", Roledesc);
        cmdclubroles.Parameters.AddWithValue("@Createdby", Createdby);

        // execute the INSERT statement
        cmdclubroles.Connection.Open();
        cmdclubroles.ExecuteNonQuery();
        cmdclubroles.Connection.Close();
    }
}

更新:将文本框中的值传入SaveRole方法 - 为其添加一些参数!

public void SaveRole(int RoleID, string RoleDesc, string CreatedBy)
{
    ClubRoles cr = new ClubRoles();
    cr.insertingclubroles(RoleID, RoleDesc, CreatedBy);

    //need text for description
    //need select which client is selected
}

然后从表单的保存方法中设置这些参数:

private void Savebtn_Click(object sender, EventArgs e)
{
    try
    {
        SaveRole(Convert.ToInt32(tbxRoleID.Text), tbxRoleDesc.Text, tbxCreatedBy.Text);
        MessageBox.Show("Club role saved successfully");
    }
    catch (Exception er)
    {
        MessageBox.Show(er.Message.ToString());
    }
}