创建数据库Practice
CREATE DATABASE Practice;
创建数据表Student //包含学号,姓名,性别,年龄
CREATE TABEL Student(
Student_No varchar(5) NOT NULL,
Student_Name varchar(8) NOT NULL,
Student_Sex varchar(2) NOT NULL,
Student_Age int
);
插入数据
INSERT INTO Student(Student_No,Student_Name,Student_Sex,Student_Age) VALUES
('0001','张三','男',21),
('0002','李四','男',22),
('0003','小红','女',20);
打开Visual Studio 2012,新建项目,选择Visual C# ,双击Windows窗体应用程序,如图
点击左侧“工具箱”,点击“所有Windows窗体”,选择相应控件进行布局,如图,分别选择Button,Label,TextBox,DataGridView
(若左侧没有工具箱,点击视图,选择工具箱即可)
对Form1的控件编辑属性
双击Lable1、Lable2、Lable3、Lable4,分别修改Text值为Student_No、Student_Name、Student_Sex、Student_Age;
双击Button1、Button2、Button3、Button4,分别修改Text值为Add、Delete、Update、Search;
将DataGridView链接到数据
点击确定后,再点击下一步,直到“选择数据库对象”,勾选“表”,点击完成即可,如图为建立完成的图形界面
打开SQL Server Management Studio ,复制数据库名称
打开Visual Studio 2012,选择左侧服务器资源管理器(如果没有,点击视图找到添加即可),右击数据连接,添加连接
粘贴服务器名,选择相应数据库名,确定,即连接成功
如图即为连接成功
双击"Add",进入编辑程序界面,对“增加操作”输入以下程序
private void button1_Click(object sender, EventArgs e) //Add 增加数据
{
string Student_No = textBox1.Text;
string Student_Name = textBox2.Text;
string Student_Sex = textBox3.Text;
string Student_Age = textBox4.Text;
SqlConnection con = new SqlConnection("Data Source=USER-20190503GJ;Initial Catalog=Practice;Integrated Security=True");
con.Open();
SqlCommand cmd = con.CreateCommand();//利用链接创建Command,执行SQL语句
cmd.CommandText = "insert into Student values('" + Student_No + "','" + Student_Name + "','" + Student_Sex + "','" + Student_Age + "')";
int count = cmd.ExecuteNonQuery();//将返回值赋值给count 判断执行是否成功,ExecuteNonQuery的返回值为受影响的行数
if (count > 0)
{
MessageBox.Show("Success!");
}
else
MessageBox.Show("Error!");
}
双击“Delete”,对“删除操作”输入以下程序
private void button2_Click(object sender, EventArgs e)//Delete 删除数据
{
string Student_No = textBox1.Text;
SqlConnection con = new SqlConnection("Data Source=USER-20190503GJ;Initial Catalog=Practice;Integrated Security=True");
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "delete from Student where Student_No=" + Student_No;
int count = cmd.ExecuteNonQuery();
if (count > 0)
MessageBox.Show("Success!");
else
MessageBox.Show("Error!");
}
双击“Update”,对“更新操作”输入以下程序
private void button3_Click(object sender, EventArgs e)//Update 更新数据
{
string Student_No = textBox1.Text;
string Student_Name = textBox2.Text;
string Student_Sex = textBox3.Text;
string Student_Age = textBox4.Text;
SqlConnection con = new SqlConnection("Data Source=USER-20190503GJ;Initial Catalog=Practice;Integrated Security=True");
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "update Student set Student_Name='" + Student_Name + "',Student_Sex='" + Student_Sex + "',Student_Age='" + Student_Age + "' where Student_No=" + Student_No;
int count = cmd.ExecuteNonQuery();
if (count > 0)
MessageBox.Show("Success!");
else
MessageBox.Show("Error!");
}
双击“Search”,对“查询操作”输入以下程序
private void button4_Click(object sender, EventArgs e)
{
string Student_No = textBox1.Text;
SqlConnection con = new SqlConnection("Data Source=USER-20190503GJ;Initial Catalog=Practice;Integrated Security=True");
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT Student_No,Student_Name,Student_Sex,Student_Age FROM Student WHERE Student_No='" + Student_No + "'";
Form1 r1 = new Form1();
DataTable count = new DataTable();//创建一个空表count
count = r1.ExecuteQuery(cmd.CommandText);//把数据库相应查询的结果放在count
if (count !=null)
{
dataGridView1.DataSource = count; //将查询结果放入到dataGridView;
}
else
MessageBox.Show("This student is not exists!");
}
并在类 Form1中新建
public DataTable ExecuteQuery(string sqlStr)
{
SqlConnection con = new SqlConnection("Data Source=USER-20190503GJ;Initial Catalog=Practice;Integrated Security=True");
con.Open();//打开数据库连接
SqlCommand cmd = new SqlCommand();//对数据库执行SQL语句或存储
cmd.Connection = con;//把con对象的引用赋值,即用SqlCommand的对象链接数据库
cmd.CommandType = CommandType.Text;//用于指定执行动作的形式,.Text表示SqlCommand的对象CommandType的执行形式为Text
cmd.CommandText = sqlStr;
DataTable dt = new DataTable();
SqlDataAdapter msda;
msda = new SqlDataAdapter(cmd);
msda.Fill(dt);
con.Close();
return dt;
}
并且手动导入命名空间
using System.Data.SqlClient;
需要注意,SqlConnection con = new SqlConnection(“”)中对数据库的链接需要手动添加,其方法为,打开“服务器资源管理器”,选择当前数据连接,复制连接字符串中的文本即可,如图
完整代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public DataTable ExecuteQuery(string sqlStr)
{
SqlConnection con = new SqlConnection("Data Source=USER-20190503GJ;Initial Catalog=Practice;Integrated Security=True");
con.Open();//打开数据库连接
SqlCommand cmd = new SqlCommand();//对数据库执行SQL语句或存储
cmd.Connection = con;//把con对象的引用赋值,即用SqlCommand的对象链接数据库
cmd.CommandType = CommandType.Text;//用于指定执行动作的形式,.Text表示SqlCommand的对象CommandType的执行形式为Text
cmd.CommandText = sqlStr;
DataTable dt = new DataTable();
SqlDataAdapter msda;
msda = new SqlDataAdapter(cmd);
msda.Fill(dt);
con.Close();
return dt;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“practiceDataSet.Student”中。您可以根据需要移动或删除它。
this.studentTableAdapter.Fill(this.practiceDataSet.Student);
}
private void button1_Click(object sender, EventArgs e)
{
string Student_No = textBox1.Text;
string Student_Name = textBox2.Text;
string Student_Sex = textBox3.Text;
string Student_Age = textBox4.Text;
SqlConnection con = new SqlConnection("Data Source=USER-20190503GJ;Initial Catalog=Practice;Integrated Security=True");
con.Open();
SqlCommand cmd = con.CreateCommand();//利用链接创建Command,执行SQL语句
cmd.CommandText = "insert into Student values('" + Student_No + "','" + Student_Name + "','" + Student_Sex + "','" + Student_Age + "')";
int count = cmd.ExecuteNonQuery();//将返回值赋值给count 判断执行是否成功,ExecuteNonQuery的返回值为受影响的行数
if (count > 0)
{
MessageBox.Show("Success!");
}
else
MessageBox.Show("Error!");
}
private void button2_Click(object sender, EventArgs e)
{
string Student_No = textBox1.Text;
SqlConnection con = new SqlConnection("Data Source=USER-20190503GJ;Initial Catalog=Practice;Integrated Security=True");
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "delete from Student where Student_No=" + Student_No;
int count = cmd.ExecuteNonQuery();
if (count > 0)
MessageBox.Show("Success!");
else
MessageBox.Show("Error!");
}
private void button3_Click(object sender, EventArgs e)
{
string Student_No = textBox1.Text;
string Student_Name = textBox2.Text;
string Student_Sex = textBox3.Text;
string Student_Age = textBox4.Text;
SqlConnection con = new SqlConnection("Data Source=USER-20190503GJ;Initial Catalog=Practice;Integrated Security=True");
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "update Student set Student_Name='" + Student_Name + "',Student_Sex='" + Student_Sex + "',Student_Age='" + Student_Age + "' where Student_No=" + Student_No;
int count = cmd.ExecuteNonQuery();
if (count > 0)
MessageBox.Show("Success!");
else
MessageBox.Show("Error!");
}
private void button4_Click(object sender, EventArgs e)
{
string Student_No = textBox1.Text;
SqlConnection con = new SqlConnection("Data Source=USER-20190503GJ;Initial Catalog=Practice;Integrated Security=True");
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT Student_No,Student_Name,Student_Sex,Student_Age FROM Student WHERE Student_No='" + Student_No + "'";
Form1 r1 = new Form1();
DataTable count = new DataTable();//创建一个空表count
count = r1.ExecuteQuery(cmd.CommandText);//把数据库相应查询的结果放在count
if (count !=null)
{
dataGridView1.DataSource = count; //将查询结果放入到dataGridView;
}
else
MessageBox.Show("This student is not exists!");
}
}
}
编者水平有限,如有错误,请读者见谅,欢迎骚扰:[email protected]