标签:
一、windows窗体项目环境配置步骤
1.文件—>新建—>项目—>windows—>修改文件名/路径—>确定
2.右键添加sqlhelper.cs,再添加引用(.net):System.Configuration
3.右键添加—>新建项—>App.config(修改文件中数据库名、文件名)
4.在代码中添加using System.Data.SqlClient
二、添加DataGridView时,需要加bindingsource(绑定源)
//从数据库中读取数据
String sql = "select patient_id,patient_no,name,type,birth,id_no,dept,grade,major,glass,work,job,mobile,cornet,remark FROM Patient";
CommandType cmd = CommandType.Text;
String con = SqlHelper.connString;
bindingSource1.DataSource = SqlHelper.ExecuteReader(con, cmd, sql);
dgvPatient.DataSource = bindingSource1;
三、声明一个变量为学号/工号的长度,用于判断是否符合要求
int count1 = txtPatient_no.Text.Length;
else if (count1 == 5 || count1==9)
{
MessageBox.Show("插入成功");
show_message();
}
else
{
MessageBox.Show("学号/工号已存在,添加失败!","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
四、提示框规范性
MessageBox.Show("更新失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
五、DataGridView获取数据,注意列名要绑定数据源,即在DataGridView编辑列设置DataPropertyName为对应数据库字段名
private void CreateColumns()
{
dgvPatient.Columns.Clear();
Int32 colLenth = 10; //列数
DataGridViewTextBoxColumn[] columns = new DataGridViewTextBoxColumn[colLenth];
for (int j = 0; j < colLenth; j++)
columns[j] = new DataGridViewTextBoxColumn();
columns[0].HeaderText = "id";
columns[0].DataPropertyName = "patient_id";
columns[1].HeaderText = "学号/工号"; //列名
columns[1].DataPropertyName = "patient_no";//对应数据库字段
columns[2].HeaderText = "姓名";
columns[2].DataPropertyName = "name";
columns[3].HeaderText = "类型";
columns[3].DataPropertyName = "type";
columns[4].HeaderText = "出生年月";
columns[4].DataPropertyName = "birth";
columns[5].HeaderText = "身份证号";
columns[5].DataPropertyName = "id_no";
columns[6].HeaderText = "系别";
columns[6].DataPropertyName = "dept";
columns[7].HeaderText = "年级";
columns[7].DataPropertyName = "grade";
columns[8].HeaderText = "专业";
columns[8].DataPropertyName = "major";
columns[9].HeaderText = "班级";
columns[9].DataPropertyName = "glass";
for (int i = 0; i < colLenth; i++)
dgvPatient.Columns.Add(columns[i]);
}
六、点击一行,数据跳到上面编辑,即DataGridView的数据跳到文本框编辑
private void dgvPatient_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if (dgvPatient.Rows[e.RowIndex].Cells != null)
{
txtPatient_no.Text = Convert.ToString(dgvPatient.Rows[e.RowIndex].Cells[1].Value);
txtName.Text = Convert.ToString(dgvPatient.Rows[e.RowIndex].Cells[2].Value);
txtType.Text = Convert.ToString(dgvPatient.Rows[e.RowIndex].Cells[3].Value);
txtBirth.Text = Convert.ToString(dgvPatient.Rows[e.RowIndex].Cells[4].Value);
txtId_no.Text = Convert.ToString(dgvPatient.Rows[e.RowIndex].Cells[5].Value);
txtDept.Text = Convert.ToString(dgvPatient.Rows[e.RowIndex].Cells[6].Value);
txtGrade.Text = Convert.ToString(dgvPatient.Rows[e.RowIndex].Cells[7].Value);
txtMajor.Text = Convert.ToString(dgvPatient.Rows[e.RowIndex].Cells[8].Value);
txtGlass.Text = Convert.ToString(dgvPatient.Rows[e.RowIndex].Cells[9].Value);
//txtPatient_no.Enabled = false;//不可修改这个文本框
}
}
catch (Exception ee)
{
MessageBox.Show("操作失败:" + ee);
}
}
七、判断文本框不能为空
if (txtPatient_no.Text == "" || txtPatient_no.Text == string.Empty)
{
MessageBox.Show("请输入学号/工号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtPatient_no.Focus();
}
else if (txtName.Text == "" || txtName.Text == string.Empty)
{
MessageBox.Show("请输入名字!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtName.Focus();
}
八、存储过程的调用,这是一个查找存储
private void search_Click(object sender, EventArgs e)
{
try
{
string strSql = "patient_get";
CommandType cmdType = CommandType.StoredProcedure;
string connString = SqlHelper.connString;
SqlParameter[] paras = {//参数
new SqlParameter("@patient_no", SqlDbType.VarChar ,30),
new SqlParameter("@name", SqlDbType.VarChar ,30),
new SqlParameter("@type", SqlDbType.VarChar,20),
new SqlParameter("@dept", SqlDbType.VarChar,30),
new SqlParameter("@grade", SqlDbType.VarChar,30),
new SqlParameter("@major", SqlDbType.VarChar,30),
new SqlParameter("@glass", SqlDbType.VarChar,30)
};
paras[0].Value = txtPatient_no.Text;//获取参数
paras[1].Value = txtName.Text;
paras[2].Value = txtType.Text;
paras[3].Value = txtDept.Text;
paras[4].Value = txtGrade.Text;
paras[5].Value = txtMajor.Text;
paras[6].Value = txtGlass.Text;
SqlDataReader reader = SqlHelper.ExecuteReader(connString, cmdType, strSql, paras);
bindingSource1.DataSource = reader;
dgvPatient.DataSource = bindingSource1;
CreateColumns();
// CreateBtn();
int obj3 = SqlHelper.ExecuteNonQuery(connString, cmdType, strSql, paras);
if (obj3 != 1)
{
MessageBox.Show("查找成功!");
}
else
{
MessageBox.Show("不存在,查找失败!","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ee)
{
MessageBox.Show("操作失败:" + ee);
}
}
九、设置默认值
private void Form1_Load(object sender, EventArgs e)
{
show_message();
txtHeight.Text = "0";//默认为0
txtWeight.Text = "0.0";//默认为0.0
}
十、清空按钮-触发事件
private void clear_Click(object sender, EventArgs e)
{
txtPatient_no.Text = "";
txtName.Text = "";
txtType.Text = "";
txtBirth.Text = "";
txtId_no.Text = "";
}
十一、数据库:模糊查找存储过程
ALTER PROCEDURE [dbo].[patient_get](
@patient_no varchar(30),
@name varchar(30),
@type varchar(20),
@dept varchar(30),
@grade varchar(30),
@major varchar(30),
@glass varchar(30)
)
AS
BEGIN
SET NOCOUNT ON;
SELECT patient_id,patient_no,name,type,birth,id_no,dept,grade,major,glass,work,job,mobile,cornet,remark FROM Patient
WHERE patient_no like ‘%‘ + rtrim(@patient_no)+‘%‘ AND name like ‘%‘ + rtrim(@name)+‘%‘ AND
type like ‘%‘ + rtrim(@type)+‘%‘ AND dept like ‘%‘ + rtrim(@dept)+‘%‘ AND
grade like ‘%‘ + rtrim(@grade)+‘%‘ AND major like ‘%‘ + rtrim(@major)+‘%‘
AND glass like ‘%‘ + rtrim(@glass)+‘%‘
END
十二、
1.DataGridView属性中Anchor设置上下左右,此时最大化时会自适应大小
2.selectionMode 设置为FullRowSelect表示点击选全行
3.RowTemplate设置为DataGridViewRow { Index=-1 }、
在事件中设置cellclick为当前DataGridViewRow触发事件
表示点击一行中任意处都会把信息在文本框显示
4.在splitContainer属性中设置Dock为fill表示最大化时会自适应大小
(C#)一个项目的配置和增删改查
标签:
原文地址:http://www.cnblogs.com/ZJMcnblog-2015/p/4471300.html