My foundation is not good, according to learn the knowledge, the textbook example revises, also does not know I understand whether conforms to the teacher request
Using C # to store data in a database, and to read data from a database, I use encapsulation for this project. I used a form to store the data in a data read.
I first created a database, the library name "Math Question Bank", and then created a table "TIKU1",
Add a class Class1
Class Class1
{
public string Strcon = @ "Data source=.;i Nitial catalog= Mathematics question Bank; Integrated Security=true ";
Public SqlConnection Sqlcon = new SqlConnection ();
Public SqlDataAdapter SDA = new SqlDataAdapter ();
Public DataSet ds = new DataSet ();
public datatable dt = new DataTable ();
Public SqlDataReader SDR;
public void Suanshu ()
{
Try
{
Sqlcon = new SqlConnection (Strcon);
}
catch (Exception e)
{
MessageBox.Show ("Database connection not successful:" + e.tostring ());
}
}
public void Suanfill (string selstr)
{
Dt. Clear ();
SDA = new SqlDataAdapter (Selstr, Strcon);
Sda. Fill (ds, "tiku1");
DT = ds. tables["TIKU1"];
}
public void Suanselect (string showtnfo)
{
Sqlcon. Open ();
SqlCommand sqlcmd = new SqlCommand (Showtnfo, Sqlcon);
SDR = sqlcmd. ExecuteReader ();
}
public void Suaninsert (string insertinfo)
{
Sqlcon. Open ();
SqlCommand sqlcmd = new SqlCommand (Insertinfo, Sqlcon);
Try
{
sqlcmd. ExecuteNonQuery ();
}
catch (Exception e)
{
MessageBox.Show ("Data insertion failed:" + e.tostring ());
}
Sqlcon. Close ();
}
public void Suangridviewupd ()
{
SqlCommandBuilder SCB = new SqlCommandBuilder (SDA);
DialogResult result;
result = MessageBox.Show ("OK to save the modified data?") "," Operation Tip ", Messageboxbuttons.okcancel, messageboxicon.question);
if (result = = DialogResult.OK)
{
DT = ds. tables["TUKU1"];
Sda. Update (DT);
Dt. AcceptChanges ();
}
}
public void Suandelete (string delstr)
{
Sqlcon. Open ();
SqlCommand sqlcmd = new SqlCommand (DELSTR, Sqlcon);
Try
{
sqlcmd. ExecuteNonQuery ();
MessageBox.Show ("Data deletion succeeded!") ");
}
catch (Exception ex)
{
MessageBox.Show ("Data deletion failed" + ex.) ToString ());
}
Sqlcon. Close ();
}
}
}
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
String selstr = @ "SELECT * from TIKU1";
Class1 db = new Class1 ();
private void Form1_Load (object sender, EventArgs e)
{
Db.suanshu ();
Db.suanfill (SELSTR);
combobox1.valuemember = "serial number";
combobox1.datasource = Db.dt.DefaultView;
}
private void Button4_Click (object sender, EventArgs e)
{
Db.suanshu ();
Db.suanfill (SELSTR);
Datagridview1.datasource = Db.dt;
}
private void Button1_Click (object sender, EventArgs e)
{
Db.suanshu ();
String insertinfo = "INSERT into TIKU1 (ordinal, first number, symbol one, second number, symbol two, third number, symbol three, result) values ('" + Combobox1.text + "', '" + Textbox1.tex T +
"', '" + TextBox2.Text +
"', '" + TextBox3.Text + "', '" + Textbox4.text + "', '" + Textbox5.text + "', '" + Textbox6.text +
"', '" + Textbox7.text +
"‘)";
Db.suaninsert (Insertinfo);
Db.suanfill (SELSTR);
Datagridview1.datasource = Db.dt;
}
private void ComboBox1_SelectedIndexChanged (object sender, EventArgs e)
{
String selinfo = "Select first number, symbol one, second number, symbol two, third number, symbol three, result from tiku1 where ordinal = '" + comboBox1.Text.ToString (). Trim () + "'";
Db.suanshu ();
Db.suanselect (Selinfo);
while (Db.sdr.Read ())
{
TextBox1.Text = db.sdr["First number"]. ToString ();
TextBox2.Text = db.sdr["Symbol One"]. ToString ();
TextBox3.Text = db.sdr["second number"]. ToString ();
Textbox4.text = db.sdr["Symbol two"]. ToString ();
Textbox5.text = db.sdr["third number"]. ToString ();
Textbox6.text = db.sdr["Symbol three"]. ToString ();
Textbox7.text = db.sdr["Result"]. ToString ();
}
Db.sdr.Close ();
}
private void Button3_Click (object sender, EventArgs e)
{
Db.suanshu ();
String strupd = "Delete from tiku1 where ordinal = '" + ComboBox1.Text.Trim () + "'";
Db.suandelete (STRUPD);
Db.suanfill (SELSTR);
datagridview1.datasource = Db.dt;
}
private void Button2_Click (object sender, EventArgs e)
{
Db.suanshu ();
DB.SUANGRIDVIEWUPD ();
}
}
}
Description
Each problem I have edited the serial number, can be selected according to the number of topics, change the question, (serial number I set the drop-down list)
1, click Query can query the database table data
2. Click Add to add Data
3. Delete data, update data
Use. NIT storing data in a database, reading a database (using encapsulation) teacher Chen's homework