访问数据库的类
class MyMeans
{
public static int id = 0;
public static string Login_ID = "";
public static string Login_Name = "";
public static string Mean_SQL = "", Mean_Table = "", Mean_Field = "";
public static SQLiteConnection My_con;
public static string M_str_sqlcon = @"Data Source=sypfexam.db;Version=3";
public static int Login_n = 0;
public static string AllSql = "SELECT * from type";
public static int examid = 0;
public static int chuanpiaoid = 0;
public static int kaoshiorsuiji = 0;
Form3 frm3=new Form3();
Form6 frm6 = new Form6();
public static SQLiteConnection getcon()
{
My_con = new SQLiteConnection(M_str_sqlcon);
My_con.Open();
return My_con;
}
public void con_close()
{
if (My_con.State == ConnectionState.Open)
{
My_con.Close();
My_con.Dispose();
}
}
public SQLiteDataReader getcom(string SQLstr)
{
getcon();
SQLiteCommand My_com=My_con.CreateCommand();
My_com.CommandText=SQLstr;
SQLiteDataReader My_read=My_com.ExecuteReader();
return My_read;
}
public void getsqlcom(string SQLstr)
{
getcon();
SQLiteCommand SQLcom = new SQLiteCommand(SQLstr, My_con);
SQLcom.ExecuteNonQuery();
SQLcom.Dispose();
con_close();
}
public void typeexam()
{
if (examid <= 14000)
{
SQLiteDataReader reader = getcom("SELECT * from type where id=" + examid);
while (reader.Read())
{
frm3.Controls["label6"].Text = reader.GetString(0);
frm3.Controls["label6"].Refresh();
frm3.Controls["label7"].Text = reader.GetString(1);
frm3.Controls["label7"].Refresh();
frm3.Controls["label8"].Text = reader.GetString(2);
frm3.Controls["label8"].Refresh();
frm3.Controls["label9"].Text = reader.GetString(3);
frm3.Controls["label9"].Refresh();
frm3.Controls["label10"].Text = reader.GetString(4);
frm3.Controls["label10"].Refresh();
frm3.Refresh();
/**/
}
frm3.Show();
con_close();
examid += 1;
}
}
public void chuanpiao()
{
SQLiteDataReader reader = getcom("SELECT * from chuanpiao where tao=" + chuanpiaoid);
while (reader.Read())
{
}
frm6.Show();
con_close();
}
private void QueryAllCourse()
{
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(textBox1.Text) < 13001)
{
MyMeans m = new MyMeans();
MyMeans.examid = Convert.ToInt32(textBox1.Text);
MyMeans.kaoshiorsuiji = 2;
this.Close();
m.typeexam();
}
else
{
label4.Text = "请按照要求输入\t\n正确的数字";
}
}
打字程序,一样的计分,不一样的变红,多余的变红
class Type_Code
{
public RichTextBox type;
public Label cankao;
public KeyPressEventArgs e;
public double score = 0;
public static double defen = 0;
public Label xianshi;
public void Type_Show()
{
if (type.TextLength <= cankao.Text.Length)
{
for (int t = 1; t < type.TextLength + 1; t++)
{
if (type.Text[t - 1] != cankao.Text[t - 1])
{
type.Select(t - 1, 1);
type.SelectionColor = System.Drawing.Color.Red;
type.Select(t, 0);
}
else
{
type.Select(t - 1, 1);
type.SelectionColor = System.Drawing.Color.Blue;
type.Select(t, 0);
}
}
}
else
{
for (int t = 1; t < cankao.Text.Length + 1; t++)
{
if (type.Text[t - 1] != cankao.Text[t - 1])
{
type.Select(t - 1, 1);
type.SelectionColor = System.Drawing.Color.Red;
}
else
{
type.Select(t - 1, 1);
type.SelectionColor = System.Drawing.Color.Blue;
}
}
type.Select(cankao.Text.Length, type.TextLength);
type.SelectionColor = System.Drawing.Color.Red;
type.Select(type.TextLength, 0);
}
}
public double GetScore(int i)
{
if (type.Text == cankao.Text)
{
switch (i)
{
case 6:
score = 0.5;
break;
case 7:
score = 2;
break;
case 8:
score = 3;
break;
case 9:
score = 0.5;
break;
case 10:
score = 0.5;
break;
}
}
else
if (i == 9 && type.Text == cankao.Text + "mj")
{
score = 13;
}
else if (i == 9 && type.Text == cankao.Text + "s")
{
score = 1;
}
else { score = 0; }
return score;
}
public void Type_Next()
{
if (e.KeyChar == (char)Keys.Enter)
{
type.Enabled= false;
}
}
}
private void richTextBox3_TextChanged(object sender, EventArgs e)
{
Type_Code r = new Type_Code();
r.cankao = label8;
r.type = richTextBox3;
r.Type_Show();
}
private void richTextBox3_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
richTextBox4.Enabled = true;
Type_Code r = new Type_Code();
r.e = e;
r.type = richTextBox3;
r.cankao = label8;
Type_Code.defen += r.GetScore(8);
richTextBox3.Enabled = false;
richTextBox4.Clear();
richTextBox4.Select(0, 1);
label18.Text = Type_Code.defen.ToString();
}
}
在GridView上打字-小键盘录入 事件为
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int rowindex = e.RowIndex;
int Comindex = e.ColumnIndex;
if (Comindex==2&&kaoshikaishi == true && zb == false)
{
panduan = dataGridView1.Rows[rowindex].Cells[1].Value.Equals(dataGridView1.Rows[rowindex].Cells[2].Value);
if (rowindex < 99)
{
if (panduan == true)
{
dataGridView1.Rows[rowindex].Cells[3].Value = "正确";
fen += 1;
label4.Text = fen.ToString();
panduan = false;
}
else
{ dataGridView1.Rows[rowindex].Cells[3].Value = "错误"; }
}
else
{
if (panduan == true)
{
dataGridView1.Rows[rowindex].Cells[3].Value = "正确";
fen += 1;
label4.Text = fen.ToString();
panduan = false;
timer1.Stop();
dataGridView1.ReadOnly = true;
dataGridView1.Rows[0].Cells[2].Selected = true;
button1.Text = "考试结束";
}
else
{
dataGridView1.Rows[rowindex].Cells[3].Value = "错误";
timer1.Stop();
dataGridView1.ReadOnly = true;
dataGridView1.Rows[0].Cells[0].Selected = true;
button1.Text = "考试结束";
}
}
``'
```csharp
在这里插入代码片
gridview 访问数据库
private void Form6_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
string connectionString = @"Data Source=sypfexam.db;Version=3";
SQLiteConnection dbConnection = new SQLiteConnection(connectionString);
dbConnection.Open();
string sqlCommandString = "SELECT * from chuanpiao where tao = " + MyMeans.chuanpiaoid;
SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqlCommandString, dbConnection);
DataTable dt = new DataTable();
adapter.Fill(dt);
dbConnection.Close();
dbConnection.Dispose();
dataGridView1.DataSource = dt;
dataGridView1.Columns["Column2"].DataPropertyName = dt.Columns["rmb"].ToString();
for (int i = 0; i< 100; i += 1)
{
dataGridView1.Rows[i].Cells[0].Value = i+1;
}
dataGridView1.ReadOnly = true;
}
**
程序打包本地sqlite数据库文件
** 在添加文件时候添加两遍 第一次把所有文件添加进去, 第二次在添加一遍文件(找到debug里放入的数据库本地文件和一些dll)
|