开发者

insert multiple ids from a department

I am creating a web form on开发者_开发技巧 asp.net that will allow the end user to assign multiple users bassed on a selected department to a quiz..

the database is mysql database since I use joomla

the tables on mysql are:

jos_users_quizzes with the following columns:
id
quiz_id
user_id

I have a second is called called  jos_users with this columns

id
name
username
department

the user_id on first table is going to be euqal to the id of second table jos_users

so quiz_id = id (jos_users)

How can build a query to multiple insert the ids of a selected department into the jos_users_quizzes table... IN ONE CLICK, I have a dropdownlist for the quiz ids and one for the userids , departments

I need to select all user ids from selected department and insert those id into the user_quizzes table.

Code from and ASP.NET form to insert ....

 private void InsertRecords(StringCollection sc)
    {
        string ConnectionString = @"driver={MySQL ODBC 5.1 Driver};server=localhost;database=db_dhruniversity;uid=root;pwd=;";
        OdbcConnection conn = new OdbcConnection(ConnectionString);

        try
        {

            conn.Open();


            string quizidselected = DropDownList1.SelectedValue;
            string deptselected = ListBox2.SelectedValue;
           // OdbcCommand cmd = new OdbcCommand("INSERT INTO jos_jquarks_users_quizzes (user_id, quiz_id)    SELECT uid, ' " + quizidselected + " ' FROM dhruprofile  WHERE department = ' " + deptselected + " '");             
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;

            cmd.ExecuteNonQuery();
            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Records Successfuly Saved!');", true);

        }

        catch (System.Data.SqlClient.SqlException ex)
        {

            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);

        }

        finally
        {

            conn.Close();

        }

    }


Try to run:

Insert into jos_users_quizzes(quiz_id, user_id) select (your_quiz_id, user_id) from   jos_users where department='selectd_depatment'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜