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 TestCURD
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=SCHOOL;User ID=sa;Password=***");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.studentTableAdapter1.Fill(this.sCHOOLDataSet.Student);
}
private void buttonInsert_Click(object sender, EventArgs e)
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
String StuSex = textBox3.Text.Trim();
String StuSdept = textBox5.Text.Trim();
int StuAge = Convert.ToInt16(textBox4.Text.Trim());
try
{
con.Open();
string insertStr = "INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) " +
"VALUES ('" + StuID + "','" + StuName + "','" + StuSex + "','" + StuSdept + "'," + StuAge + ")";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Dispose();
}
this.studentTableAdapter1.Fill(this.sCHOOLDataSet.Student);
}
private void button2_Click(object sender, EventArgs e)
{
try
{
con.Open();
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
string delete_by_id = "delete from Student where Sno=" + select_id;
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("请正确选择行!");
}
finally
{
con.Dispose();
}
this.studentTableAdapter1.Fill(this.sCHOOLDataSet.Student);
}
private void button3_Click(object sender, EventArgs e)
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
try
{
con.Open();
string insertStr = "UPDATE Student SET Sname = '" + StuName + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Dispose();
}
this.studentTableAdapter1.Fill(this.sCHOOLDataSet.Student);
}
private void button4_Click(object sender, EventArgs e)
{
String StuID = textBox1.Text.Trim();
String conn = "Data Source=.;Initial Catalog=SCHOOL;User ID=sa;Password=***";
SqlConnection sqlConnection = new SqlConnection(conn);
try
{
sqlConnection.Open();
String select_by_id = "select * from Student where Sno='" + StuID + "'";
SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!");
}
finally
{
sqlConnection.Close();
}
}
}
}
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=SCHOOL;User ID=sa;Password=***");
string myConnString = "Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=密码";
SqlConnection sqlConnection = new SqlConnection(myConnString);
sqlConnection.Open(); sqlConnection.Close();
string sql = "select userid,password from usertable where userid = '" + username + "' and password = '" + password + "'";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);