using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _20190619
{
public partial class Form1 : Form
{
string strcon = @"Data Source=(localdb)\Projects;Initial Catalog=master;Integrated Security=True;";
DataSet myst = new DataSet();
SqlDataAdapter myda;
public Form1()
{
InitializeComponent();
btnSeach_Click(null, null);
}
private void btnInsert_Click(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection(strcon);
try
{
string stuID = StuID.Text.Trim();
string stuName = StuName.Text.Trim();
string stuXB;
if (RbtnMale.Checked)
{
stuXB = "男";
}
else
{
stuXB = "女";
}
string stuBirth = dateTimePicker1.Value.ToShortDateString().Split(' ')[0];
string stuMajor = StuMajor.Text.Trim();
string stuCredit = StuCredit.Text.Trim();
SqlCommand command = new SqlCommand();
command.CommandText = "INSERT INTO XSB(XH,XM,XB,CSRQ,ZY,ZYF)VALUES('" + stuID + "','" + stuName + "','" + stuXB + "','" + stuBirth + "','" + stuMajor + "','" + stuCredit + "')";
command.CommandType = CommandType.Text;
command.Connection = sqlcon;
sqlcon.Open();
myda = new SqlDataAdapter(command);
myda.Fill(myst, "XSB");
MessageBox.Show("插入成功!", "消息", MessageBoxButtons.OK);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sqlcon.Close();
}
}
private void btnSeach_Click(object sender, EventArgs e)
{
using (SqlConnection sqlcon = new SqlConnection(strcon))
{
sqlcon.Open();
string sql = "SELECT*FORM XSB";
SqlCommand command = new SqlCommand(sql, sqlcon);
myda = new SqlDataAdapter(command);
myst.Tables.Clear();
myda.Fill(myst, "XSB");
dgvAllStu.DataSource = myst.Tables["XSB"];
}
}
}
}