📄 pretestform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace CTestApp
{
public partial class PreTestForm : Form
{
protected SqlConnection conn;
public PreTestForm()
{
InitializeComponent();
}
private void PreTestForm_Load(object sender, EventArgs e)
{
try
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TestLibraryConnectionString"].ConnectionString);
conn.Open();
this.LoadTestInfo();
this.LoadQuestionInfo();
}
catch (Exception exp)
{
MessageBox.Show("无法建立数据连接:" + exp.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
finally
{
if (conn != null && conn.State != ConnectionState.Closed)
conn.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
TestForm frm1 = new TestForm((int)comboBox1.SelectedValue);
frm1.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
if (numericUpDown1.Value == 0 && numericUpDown2.Value == 0 && numericUpDown3.Value == 0 && numericUpDown4.Value == 0 && numericUpDown5.Value == 0 && numericUpDown6.Value == 0)
{
MessageBox.Show("试题数量不能小于1", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
SqlCommand cmd1 = new SqlCommand("GenTest", conn);
cmd1.CommandType = CommandType.StoredProcedure;
SqlParameter par1 = new SqlParameter("@name", SqlDbType.NVarChar, 50);
par1.Value = textBox1.Text;
cmd1.Parameters.Add(par1);
par1 = new SqlParameter("@sQuestions1", SqlDbType.Int);
par1.Value = (int)numericUpDown1.Value;
cmd1.Parameters.Add(par1);
par1 = new SqlParameter("@sQuestions2", SqlDbType.Int);
par1.Value = (int)numericUpDown2.Value;
cmd1.Parameters.Add(par1);
par1 = new SqlParameter("@sQuestions3", SqlDbType.Int);
par1.Value = (int)numericUpDown3.Value;
cmd1.Parameters.Add(par1);
par1 = new SqlParameter("@mQuestions1", SqlDbType.Int);
par1.Value = (int)numericUpDown4.Value;
cmd1.Parameters.Add(par1);
par1 = new SqlParameter("@mQuestions2", SqlDbType.Int);
par1.Value = (int)numericUpDown5.Value;
cmd1.Parameters.Add(par1);
par1 = new SqlParameter("@mQuestions3", SqlDbType.Int);
par1.Value = (int)numericUpDown6.Value;
cmd1.Parameters.Add(par1);
par1 = new SqlParameter("@testID", SqlDbType.Int);
par1.Direction = ParameterDirection.Output;
cmd1.Parameters.Add(par1);
try
{
conn.Open();
cmd1.ExecuteNonQuery();
this.LoadTestInfo();
}
catch (Exception exp)
{
MessageBox.Show("数据访问错误:" + exp.Message);
return;
}
finally
{
if (conn != null && conn.State != ConnectionState.Closed)
conn.Close();
}
TestForm frm1 = new TestForm((int)par1.Value);
frm1.ShowDialog();
}
/// <summary>
/// 载入现有测试列表
/// </summary>
protected void LoadTestInfo()
{
SqlCommand cmd1 = new SqlCommand("GetTestInfo", conn);
cmd1.CommandType = CommandType.StoredProcedure;
DataTable table1 = new DataTable();
table1.Load(cmd1.ExecuteReader());
if (table1.Rows.Count == 0) //无现有测试
{
button1.Enabled = false;
}
else
{
comboBox1.DataSource = table1;
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "Information";
}
}
/// <summary>
/// 载入现有试题数量信息
/// </summary>
protected void LoadQuestionInfo()
{
SqlCommand cmd1 = new SqlCommand("GetQuestionInfo", conn);
cmd1.CommandType = CommandType.StoredProcedure;
SqlDataReader reader1 = cmd1.ExecuteReader();
if (reader1.Read())
{
numericUpDown1.Maximum = (int)reader1[0];
numericUpDown2.Maximum = (int)reader1[1];
numericUpDown3.Maximum = (int)reader1[2];
numericUpDown4.Maximum = (int)reader1[3];
numericUpDown5.Maximum = (int)reader1[4];
numericUpDown6.Maximum = (int)reader1[5];
}
reader1.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -