完整示例代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.Data.SqlClient;
namespace WinformMessge
{
public partial class Form1 : Form
{
static int Num = 0;
int Count = 0;
public Form1()
{
InitializeComponent();
}
//根据自己情况填写
SqlConnection con = new SqlConnection("server=WinFormUse;pwd=sa;uid=sa;database=db_winform");
private void button6_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
Resultinfo(Num);
using (SqlCommand cmd = new SqlCommand("select Count(*) from 员工表", con))
{
con.Open();
Count = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
private DataSet DtReslut(int i)
{
using (SqlDataAdapter da = new SqlDataAdapter()) //实例化SqlDataAdapter类
{
da.SelectCommand = new SqlCommand("select * from 员工表", con); //建立SQL语句与数据库的连接
DataSet ds = new DataSet(); //实例化DataSet类
da.Fill(ds, i, i 1, "员工表"); //获取指定行的记录
return ds; //返回DataSet对象
}
}
private void Resultinfo(int j)
{
DataSet dsNew = DtReslut(j); //在数据表中获取指定行的信息
this.textBox1.Text = dsNew.Tables[0].Rows[0][0].ToString(); //显示相应的字段信息
this.textBox2.Text = dsNew.Tables[0].Rows[0][1].ToString();
this.textBox4.Text = dsNew.Tables[0].Rows[0][2].ToString();
this.textBox5.Text = dsNew.Tables[0].Rows[0][3].ToString();
}
private void button1_Click(object sender, EventArgs e)
{
Num = 0; //行号
Resultinfo(Num); //显示记录信息
}
private void button2_Click(object sender, EventArgs e)
{
Num -= 1; //行号减1
if (Num >= 0) //如果当前不是首记录
{
Resultinfo(Num); //显示上一条记录
}
else
{
MessageBox.Show("现已是首条记录");
Num = 0; //设置记录号为首记录
return; ;
}
}
private void button3_Click(object sender, EventArgs e)
{
Num = 1; //行号加1
if (Num < Count) //如果没有超出记录尾
{
Resultinfo(Num); //显示下一条记录
}
else
{
MessageBox.Show("现已是末条记录");
Num = Count - 1; //将行号设置为最后一个
return;
}
}
private void button4_Click(object sender, EventArgs e)
{
Num = Count; //尾记录号
Resultinfo(Num - 1); //显示最后一条记录
}
private void label4_Click(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
}
}
设计图
报错的话请检查用户名、密码、地址、数据库信息
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved