C#2005实现的简单解题系统,数据库课程设计准备知识。实现数据绑定,记录定位,更新数据,查询数据等功能。其他功能可以在此基础上增加。
方便起见使用ACCESS建立数据库;建立两张表: questions(序号,内容),序号:自动编号 answers(id,学号,姓名,问题ID,答案,提交时间),id:自动编号,问题ID参照questions(序号)。
功能代码为
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.OleDb; namespace testIt { public partial class frmTest : Form { public frmTest() { InitializeComponent(); } private OleDbConnection cnn; private OleDbDataAdapter da; private DataSet ds; private OleDbCommand cmd; private BindingManagerBase bm; private OleDbCommandBuilder cb; private void bm_PositionChanged(object sender, EventArgs e) { txtPosition.Text = "Rec " + (bm.Position + 1) + " of " + bm.Count; } private void frmTest_Load(object sender, EventArgs e) { cnn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "d://test//tm.mdb"); cnn.Open(); da = new OleDbDataAdapter("select 序号,内容 from questions", cnn); ds = new DataSet(); da.Fill(ds, "questions"); OleDbDataAdapter daAnswer = new OleDbDataAdapter("select 问题ID from answers", cnn); daAnswer.Fill(ds, "answers"); cboContests.DataSource = ds.Tables["questions"]; cboContests.DisplayMember = "内容"; cboContests.ValueMember = "序号"; cboContests.DataBindings.Add("SelectedValue", ds, "answers.问题ID"); dgvContents.DataSource = ds; dgvContents.DataMember = "questions"; bm = this.BindingContext[ds,"questions"]; bm.CurrentChanged += new EventHandler(bm_PositionChanged); txtPosition.Text = "Rec " + (bm.Position + 1) + " of " + bm.Count; rtxtContents.DataBindings.Add(new Binding("text", ds, "questions.内容")); cmd = new OleDbCommand(); cmd.Connection = cnn; cb = new OleDbCommandBuilder(da); } private void btnNext_Click(object sender, EventArgs e) { bm.Position++; } private void btnLast_Click(object sender, EventArgs e) { bm.Position = bm.Count; } private void btnPrevious_Click(object sender, EventArgs e) { bm.Position--; } private void btnFirst_Click(object sender, EventArgs e) { bm.Position = 0; } private void btnCommit_Click(object sender, EventArgs e) { cmd.CommandType = CommandType.Text; OleDbCommand cmdTemp=new OleDbCommand("select 序号 from questions where 内容='" + rtxtContents.Text + "'",cnn); OleDbDataReader dr=cmdTemp.ExecuteReader(); dr.Read(); string id=dr.GetValue(0).ToString(); string strSql = "insert into answers(学号,姓名,问题ID,答案,提交时间) values('" + txtNo.Text + "','" + txtName.Text + "'," + id + ",'" + rtxtAnswer.Text + "','" + DateTime.Now + "')"; cmd.CommandText = strSql; int cnt=cmd.ExecuteNonQuery(); if (cnt > 0) { MessageBox.Show("提交成功!"); } } private void btnInsert_Click(object sender, EventArgs e) { bm.AddNew(); } private void btnSave_Click(object sender, EventArgs e) { bm.EndCurrentEdit(); da.Update(ds, "questions"); ds.AcceptChanges(); } private void btnDelete_Click(object sender, EventArgs e) { bm.RemoveAt(bm.Position); da.Update(ds, "questions"); } private void btnCancel_Click(object sender, EventArgs e) { bm.CancelCurrentEdit(); } private void btnLocate_Click(object sender, EventArgs e) { int i; for (i = 0; i < bm.Count; i++) { bm.Position = i; DataRowView curRow = (DataRowView)bm.Current; if (curRow["内容"].ToString() == txtInput.Text) break; } if (i >= bm.Count) MessageBox.Show("找不到!"); } } }
//连接MS Sql Server数据库,要修改连接串如下(test为数据库名,登录名与登录密码都是sa) 。 using System.Data.OleDb; OleDbConnection cnn = new OleDbConnection("Provider=SqlOledb.1;Server=(local);database=test;uid=sa;pwd=sa"); cnn.Open(); //或 using System.Data.SqlClient; SqlConnection cnn = new SqlConnection("Server=(local);database=test;uid=sa;pwd=sa"); cnn.Open();
//界面设计代码为
namespace testIt { partial class frmTest { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.txtNo = new System.Windows.Forms.TextBox(); this.txtName = new System.Windows.Forms.TextBox(); this.btnCommit = new System.Windows.Forms.Button(); this.lblContents = new System.Windows.Forms.Label(); this.dgvContents = new System.Windows.Forms.DataGridView(); this.rtxtContents = new System.Windows.Forms.RichTextBox(); this.btnNext = new System.Windows.Forms.Button(); this.btnLast = new System.Windows.Forms.Button(); this.btnPrevious = new System.Windows.Forms.Button(); this.btnFirst = new System.Windows.Forms.Button(); this.txtPosition = new System.Windows.Forms.TextBox(); this.rtxtAnswer = new System.Windows.Forms.RichTextBox(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.btnDelete = new System.Windows.Forms.Button(); this.btnInsert = new System.Windows.Forms.Button(); this.btnLocate = new System.Windows.Forms.Button(); this.txtInput = new System.Windows.Forms.TextBox(); this.btnSave = new System.Windows.Forms.Button(); this.btnCancel = new System.Windows.Forms.Button(); this.cboContests = new System.Windows.Forms.ComboBox(); ((System.ComponentModel.ISupportInitialize)(this.dgvContents)).BeginInit(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(3, 24); this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(32, 16); this.label1.TabIndex = 0; this.label1.Text = "Sno"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(194, 24); this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(48, 16); this.label2.TabIndex = 1; this.label2.Text = "Sname"; // // txtNo // this.txtNo.Location = new System.Drawing.Point(40, 14); this.txtNo.Margin = new System.Windows.Forms.Padding(4); this.txtNo.MaxLength = 8; this.txtNo.Name = "txtNo"; this.txtNo.Size = new System.Drawing.Size(132, 26); this.txtNo.TabIndex = 2; // // txtName // this.txtName.Location = new System.Drawing.Point(242, 14); this.txtName.Margin = new System.Windows.Forms.Padding(4); this.txtName.Name = "txtName"; this.txtName.Size = new System.Drawing.Size(132, 26); this.txtName.TabIndex = 3; // // btnCommit // this.btnCommit.Location = new System.Drawing.Point(394, 299); this.btnCommit.Name = "btnCommit"; this.btnCommit.Size = new System.Drawing.Size(75, 32); this.btnCommit.TabIndex = 4; this.btnCommit.Text = "Commit"; this.btnCommit.UseVisualStyleBackColor = true; this.btnCommit.Click += new System.EventHandler(this.btnCommit_Click); // // lblContents // this.lblContents.AutoSize = true; this.lblContents.Location = new System.Drawing.Point(96, 98); this.lblContents.Name = "lblContents"; this.lblContents.Size = new System.Drawing.Size(0, 16); this.lblContents.TabIndex = 6; // // dgvContents // this.dgvContents.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dgvContents.Location = new System.Drawing.Point(394, 12); this.dgvContents.Name = "dgvContents"; this.dgvContents.RowTemplate.Height = 23; this.dgvContents.Size = new System.Drawing.Size(245, 254); this.dgvContents.TabIndex = 7; // // rtxtContents // this.rtxtContents.Location = new System.Drawing.Point(12, 77); this.rtxtContents.Name = "rtxtContents"; this.rtxtContents.Size = new System.Drawing.Size(171, 254); this.rtxtContents.TabIndex = 8; this.rtxtContents.Text = ""; // // btnNext // this.btnNext.Location = new System.Drawing.Point(216, 337); this.btnNext.Name = "btnNext"; this.btnNext.Size = new System.Drawing.Size(83, 32); this.btnNext.TabIndex = 10; this.btnNext.Text = "Next"; this.btnNext.UseVisualStyleBackColor = true; this.btnNext.Click += new System.EventHandler(this.btnNext_Click); // // btnLast // this.btnLast.Location = new System.Drawing.Point(305, 337); this.btnLast.Name = "btnLast"; this.btnLast.Size = new System.Drawing.Size(83, 32); this.btnLast.TabIndex = 11; this.btnLast.Text = "Last"; this.btnLast.UseVisualStyleBackColor = true; this.btnLast.Click += new System.EventHandler(this.btnLast_Click); // // btnPrevious // this.btnPrevious.Location = new System.Drawing.Point(394, 337); this.btnPrevious.Name = "btnPrevious"; this.btnPrevious.Size = new System.Drawing.Size(83, 32); this.btnPrevious.TabIndex = 12; this.btnPrevious.Text = "Previous"; this.btnPrevious.UseVisualStyleBackColor = true; this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click); // // btnFirst // this.btnFirst.Location = new System.Drawing.Point(483, 337); this.btnFirst.Name = "btnFirst"; this.btnFirst.Size = new System.Drawing.Size(83, 32); this.btnFirst.TabIndex = 13; this.btnFirst.Text = "First"; this.btnFirst.UseVisualStyleBackColor = true; this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click); // // txtPosition // this.txtPosition.Location = new System.Drawing.Point(12, 343); this.txtPosition.Name = "txtPosition"; this.txtPosition.Size = new System.Drawing.Size(198, 26); this.txtPosition.TabIndex = 14; // // rtxtAnswer // this.rtxtAnswer.Location = new System.Drawing.Point(193, 77); this.rtxtAnswer.Name = "rtxtAnswer"; this.rtxtAnswer.Size = new System.Drawing.Size(181, 254); this.rtxtAnswer.TabIndex = 15; this.rtxtAnswer.Text = ""; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(2, 58); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(72, 16); this.label3.TabIndex = 16; this.label3.Text = "Question"; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(190, 58); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(96, 16); this.label4.TabIndex = 17; this.label4.Text = "Your Answer"; // // button1 // this.button1.Location = new System.Drawing.Point(483, 299); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 32); this.button1.TabIndex = 18; this.button1.Text = "Update"; this.button1.UseVisualStyleBackColor = true; // // btnDelete // this.btnDelete.Location = new System.Drawing.Point(564, 299); this.btnDelete.Name = "btnDelete"; this.btnDelete.Size = new System.Drawing.Size(75, 32); this.btnDelete.TabIndex = 19; this.btnDelete.Text = "Delete"; this.btnDelete.UseVisualStyleBackColor = true; this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click); // // btnInsert // this.btnInsert.Location = new System.Drawing.Point(645, 299); this.btnInsert.Name = "btnInsert"; this.btnInsert.Size = new System.Drawing.Size(75, 32); this.btnInsert.TabIndex = 20; this.btnInsert.Text = "Insert"; this.btnInsert.UseVisualStyleBackColor = true; this.btnInsert.Click += new System.EventHandler(this.btnInsert_Click); // // btnLocate // this.btnLocate.Location = new System.Drawing.Point(726, 299); this.btnLocate.Name = "btnLocate"; this.btnLocate.Size = new System.Drawing.Size(75, 32); this.btnLocate.TabIndex = 21; this.btnLocate.Text = "Locate"; this.btnLocate.UseVisualStyleBackColor = true; this.btnLocate.Click += new System.EventHandler(this.btnLocate_Click); // // txtInput // this.txtInput.Location = new System.Drawing.Point(619, 345); this.txtInput.Name = "txtInput"; this.txtInput.Size = new System.Drawing.Size(127, 26); this.txtInput.TabIndex = 22; // // btnSave // this.btnSave.Location = new System.Drawing.Point(807, 299); this.btnSave.Name = "btnSave"; this.btnSave.Size = new System.Drawing.Size(75, 32); this.btnSave.TabIndex = 23; this.btnSave.Text = "Save"; this.btnSave.UseVisualStyleBackColor = true; this.btnSave.Click += new System.EventHandler(this.btnSave_Click); // // btnCancel // this.btnCancel.Location = new System.Drawing.Point(807, 337); this.btnCancel.Name = "btnCancel"; this.btnCancel.Size = new System.Drawing.Size(75, 32); this.btnCancel.TabIndex = 24; this.btnCancel.Text = "Cancel"; this.btnCancel.UseVisualStyleBackColor = true; this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click); // // cboContests // this.cboContests.FormattingEnabled = true; this.cboContests.Location = new System.Drawing.Point(690, 12); this.cboContests.Name = "cboContests"; this.cboContests.Size = new System.Drawing.Size(121, 24); this.cboContests.TabIndex = 25; // // frmTest // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(894, 388); this.Controls.Add(this.cboContests); this.Controls.Add(this.btnCancel); this.Controls.Add(this.btnSave); this.Controls.Add(this.txtInput); this.Controls.Add(this.btnLocate); this.Controls.Add(this.btnInsert); this.Controls.Add(this.btnDelete); this.Controls.Add(this.button1); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.rtxtAnswer); this.Controls.Add(this.txtPosition); this.Controls.Add(this.btnFirst); this.Controls.Add(this.btnPrevious); this.Controls.Add(this.btnLast); this.Controls.Add(this.btnNext); this.Controls.Add(this.rtxtContents); this.Controls.Add(this.dgvContents); this.Controls.Add(this.lblContents); this.Controls.Add(this.btnCommit); this.Controls.Add(this.txtName); this.Controls.Add(this.txtNo); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.Margin = new System.Windows.Forms.Padding(4); this.Name = "frmTest"; this.Text = "Database&C#"; this.Load += new System.EventHandler(this.frmTest_Load); ((System.ComponentModel.ISupportInitialize)(this.dgvContents)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox txtNo; private System.Windows.Forms.TextBox txtName; private System.Windows.Forms.Button btnCommit; private System.Windows.Forms.Label lblContents; private System.Windows.Forms.DataGridView dgvContents; private System.Windows.Forms.RichTextBox rtxtContents; private System.Windows.Forms.Button btnNext; private System.Windows.Forms.Button btnLast; private System.Windows.Forms.Button btnPrevious; private System.Windows.Forms.Button btnFirst; private System.Windows.Forms.TextBox txtPosition; private System.Windows.Forms.RichTextBox rtxtAnswer; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button btnDelete; private System.Windows.Forms.Button btnInsert; private System.Windows.Forms.Button btnLocate; private System.Windows.Forms.TextBox txtInput; private System.Windows.Forms.Button btnSave; private System.Windows.Forms.Button btnCancel; private System.Windows.Forms.ComboBox cboContests; } }