Ejemplo n.º 1
0
        // 改
        private void btn_info_Click(object sender, EventArgs e)
        {
            int currIndex = dataGridView1.CurrentRow.Index; // 当前行
            // 第0列是checkbox
            int journalId = Convert.ToInt32(dataGridView1.Rows[currIndex].Cells[1].Value);

            using (OdbcConnection conn = new OdbcConnection(connStr))
            {
                conn.Open();
                string sqlStr = "SELECT J.journal_id, J.journal_name, J.new_publish_date, C.catalog_name, T.typee_name\n" +
                                "FROM Journal AS J, Editor AS E, Typee AS T, Catalogg AS C\n" +
                                "WHERE journal_id = " + journalId.ToString() + "\n" +
                                "AND J.editor_id = E.editor_id\n" +
                                "AND J.typee_id = T.typee_id\n" +
                                "AND T.catalog_id = C.catalog_id;";
                OdbcDataReader reader    = new OdbcCommand(sqlStr, conn).ExecuteReader(); // 执行查询语句
                Hashtable      infoTable = new Hashtable();                               // 准备给对话框传参
                if (reader.Read())
                {
                    infoTable.Add("id", Convert.ToInt32(reader[0]));
                    infoTable.Add("name", Convert.ToString(reader[1]));
                    infoTable.Add("publish_date", Convert.ToDateTime(reader[2]));
                    infoTable.Add("catalog_name", Convert.ToString(reader[3]));
                    infoTable.Add("typee_name", Convert.ToString(reader[4]));
                    // 显示
                    DJournalInfo dJournal = new DJournalInfo(infoTable, connStr);
                    if (dJournal.ShowDialog() == DialogResult.OK)
                    {
                        infoTable = dJournal.getNewInfo();
                        // 更新数据,并刷新页面
                        // 1. 查询typee
                        sqlStr = "SELECT typee_id\n" +
                                 "FROM Typee\n" +
                                 "WHERE typee_name = \'" + infoTable["typee_name"] + "\';";
                        reader = new OdbcCommand(sqlStr, conn).ExecuteReader(); // 执行查询语句
                        reader.Read();
                        string typeeid = Convert.ToString(reader[0]);
                        // 2. 更新
                        sqlStr = "UPDATE Journal\n" +
                                 "SET journal_name = \'" + infoTable["name"] + "\',\n" +
                                 "new_publish_date = \'" + infoTable["publish_date"] + "\',\n" +
                                 "typee_id = \'" + typeeid + "\'\n" +
                                 "WHERE journal_id = " + journalId + ";";
                        noQuery(sqlStr);
                        updateGridView();
                    }
                }
            }
        }
Ejemplo n.º 2
0
        // 增
        private void btn_add_Click(object sender, EventArgs e)
        {
            int currIndex = dataGridView1.CurrentRow.Index; // 当前行
            int journalId = Convert.ToInt32(dataGridView1.Rows[currIndex].Cells[1].Value);

            Hashtable      infoTable = new Hashtable();
            OdbcDataReader reader;

            using (OdbcConnection conn = new OdbcConnection(connStr))
            {
                conn.Open();
                string sqlStr;
                // 先给一个journal_id以防重复
                sqlStr = "SELECT MAX(journal_id)\n" +
                         "FROM Journal\n";
                reader = new OdbcCommand(sqlStr, conn).ExecuteReader();
                reader.Read();
                infoTable["id"] = Convert.ToInt32(reader[0]) + 1;

                DJournalInfo dJournal = new DJournalInfo(infoTable, connStr);
                if (dJournal.ShowDialog() == DialogResult.OK)
                {
                    infoTable = dJournal.getNewInfo();
                    // 更新数据,并刷新页面
                    // 1. 查询typee
                    sqlStr = "SELECT typee_id\n" +
                             "FROM Typee\n" +
                             "WHERE typee_name = \'" + infoTable["typee_name"] + "\';";
                    reader = new OdbcCommand(sqlStr, conn).ExecuteReader(); // 执行查询语句
                    reader.Read();
                    string typeeid = Convert.ToString(reader[0]);
                    // 2. 揷数据
                    sqlStr = "INSERT INTO Journal (journal_id, journal_name, new_publish_date, editor_id, typee_id)\n" +
                             string.Format("VALUES ('{0}', '{1}', '{2}', {3}, '{4}');",
                                           infoTable["id"], infoTable["name"], infoTable["publish_date"], 3, typeeid);
                    noQuery(sqlStr);
                    updateGridView();
                }
            }
        }