ASPX插入MySQL数据库

时间:2017-04-22 19:17:50

标签: c# mysql asp.net visual-studio parameters

我对编程完全不熟悉并且我无法将数据insert转换为表ticket此代码中是否存在语法或逻辑错误,应该插入票证信息?我没有收到任何错误但它也没有插入数据

using (MySqlCommand newercmd = new MySqlCommand("INSERT INTO ticket (StudentID, ClassID, TicketTitle, TicketStatus, Module, ExcerciseNumber, TCode, StartingTime) VALUES (Select StudentID from student where GlobalID = @GlobalID, @ClassID, @TicketTitle, '1', @Module, @Excercise, @TCode, NOW()", con))
{
    newercmd.Parameters.AddWithValue("GlobalID", globalbox.Text);
    newercmd.Parameters.AddWithValue("ClassID", ClassBox.Text);
    newercmd.Parameters.AddWithValue("TicketTitle", TitleBox.Text);
    newercmd.Parameters.AddWithValue("Module", ModuleBox.Text);
    newercmd.Parameters.AddWithValue("Excercise", ExcerciseBox.Text);
    newercmd.Parameters.AddWithValue("TCode", TcodeBox.Text);

    newercmd.ExecuteNonQuery();
    con.Close();
}

完整的aspx.cs代码:

protected void Button1_Click(object sender, EventArgs e)
{

    using (MySqlConnection con = new MySqlConnection(@"............"))
    {

        string checkglobal = "select count(*) from student where GlobalID= @GlobalID";
        MySqlCommand cmd = new MySqlCommand(checkglobal, con);

        con.Open();

        cmd.Parameters.AddWithValue("@GlobalID", globalbox.Text);
        var objectuserexists = cmd.ExecuteScalar();
        int userexists = Convert.ToInt32(objectuserexists);

        // if exists do nothing as we already have them stored!
        if (userexists > 0)
        { }
        else
        {
            // does not exists insert into student table
            using (MySqlCommand newcmd = new MySqlCommand("INSERT INTO student (GlobalID) values (@GlobalID)", con))
            {
                newcmd.Parameters.AddWithValue("GlobalID", globalbox.Text);

                newcmd.ExecuteNonQuery();

            }


            {

                //insert into ticket table
                using (MySqlCommand newercmd = new MySqlCommand("INSERT INTO ticket (StudentID, ClassID, TicketTitle, TicketStatus, Module, ExcerciseNumber, TCode, StartingTime) VALUES (Select StudentID from student where GlobalID = @GlobalID, @ClassID, @TicketTitle, '1', @Module, @Excercise, @TCode, NOW()", con))
                {
                    newercmd.Parameters.AddWithValue("GlobalID", globalbox.Text);
                    newercmd.Parameters.AddWithValue("ClassID", ClassBox.Text);
                    newercmd.Parameters.AddWithValue("TicketTitle", TitleBox.Text);
                    newercmd.Parameters.AddWithValue("Module", ModuleBox.Text);
                    newercmd.Parameters.AddWithValue("Excercise", ExcerciseBox.Text);
                    newercmd.Parameters.AddWithValue("TCode", TcodeBox.Text);

                    newercmd.ExecuteNonQuery();
                    con.Close();
                }

            }

        }
    }
}

0 个答案:

没有答案