通过窗体按钮连接数据库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace 数据库连接
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection connection=new SqlConnection();
connection.ConnectionString= "Data Source=localhost;Initial Catalog=students;Integrated Security=SSPI;";
connection.Open();
if(connection.State == System.Data.ConnectionState.Open)
{
Response.Write("<script>alert('数据库连接成功!');</script>");
}
else
{
Response.Write("<script>alert('数据库连接失败!');</script>");
}
}
}
}
学生管理系统
头文件信息
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Windows.Forms;
1.首先创建一个学生的类,里面包含个人信息
namespace 学生管理系统
{
internal class students_inf
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string department { get; set; }
public int classnum { get; set; }
}
}
2.数据库内容载入界面
private void Form1_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
List<students_inf> list = new List<students_inf>();
string constr = "Data Source=localhost;Initial Catalog=students;Integrated Security=SSPI;";
using (SqlConnection con = new SqlConnection(constr))
{
string sql = "select * from students_inf";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
students_inf model = new students_inf();
model.ID = reader.GetInt32(0);
model.Name = reader.GetString(1);
model.Age = reader.GetInt32(2);
model.department = reader.GetString(3);
model.classnum = reader.GetInt32(4);
list.Add(model);
}
}
}
}
}
this.dataGridView1.DataSource = list;//数据绑定(属性)
}
3.插入学生数据(注意:双击设计界面的按钮修改对应代码才有效,否则引用为0)
private void button1_Click_1(object sender, EventArgs e)
{
string id= textBox1.Text.Trim();
string name = textBox2.Text.Trim();
string age = textBox3.Text.Trim();
string department = textBox4.Text.Trim();
string classnum = textBox5.Text.Trim();
string constr = "Data Source=localhost;Initial Catalog=students;Integrated Security=SSPI;";
using (SqlConnection con = new SqlConnection(constr))
{
string sql = string.Format("insert into students_inf values({0},'{1}',{2},'{3}',{4})",id,name,age,department,classnum);
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
int r = cmd.ExecuteNonQuery();
if (r > 0)
{
MessageBox.Show("插入成功");
}
else
{
MessageBox.Show("插入失败");
}
}
}
}
4.修改学生数据
private void button2_Click(object sender, EventArgs e)
{
string id = Box1.Text.Trim();
string name = Box2.Text.Trim();
string age = Box3.Text.Trim();
string department = Box4.Text.Trim();
string classnum = Box5.Text.Trim();
string constr = "Data Source=localhost;Initial Catalog=students;Integrated Security=SSPI;";
using (SqlConnection con = new SqlConnection(constr))
{
string sql = string.Format("update students_inf set Name ='{0}',Age={1},department='{2}',classnum={3} where ID={4}", name, age, department, classnum, id);
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
int r = cmd.ExecuteNonQuery();
if (r > 0)
{
MessageBox.Show("修改成功");
}
else
{
MessageBox.Show("修改失败");
}
}
}
}
5.查询数据库现状
private void button3_Click(object sender, EventArgs e)
{
List<students_inf> list = new List<students_inf>();
string constr = "Data Source=localhost;Initial Catalog=students;Integrated Security=SSPI;";
using (SqlConnection con = new SqlConnection(constr))
{
string sql = "select * from students_inf";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
students_inf model = new students_inf();
model.ID = reader.GetInt32(0);
model.Name = reader.GetString(1);
model.Age = reader.GetInt32(2);
model.department = reader.GetString(3);
model.classnum = reader.GetInt32(4);
list.Add(model);
}
}
}
}
}
this.dataGridView1.DataSource = list;//数据绑定(属性)
MessageBox.Show("查询成功");
}