📄 search.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace graduationNET
{
public partial class search : Form
{
SqlConnection myconnection;
SqlDataAdapter sa;
SqlCommand sqlcmd;
DataSet ds;
private void GetData(string s)
{
string constr = "Data Source=localhost;Initial Catalog=.NET_51671;Integrated Security= true;";
myconnection = new SqlConnection(constr);
sa = new SqlDataAdapter();
sqlcmd = new SqlCommand();
sqlcmd.CommandText =s;
sqlcmd.Connection = myconnection;
sa.SelectCommand = sqlcmd;
ds = new DataSet();
sa.Fill(ds, "graduation_51671");
dataGridView1.DataSource = ds.Tables["graduation_51671"];
dataGridView1.CurrentCell = this.dataGridView1[0, 0];
showdata();
}
private void showdata()
{
string sqlstatement = "select * from graduation_51671 where sno=@sno";
sqlcmd = new SqlCommand(sqlstatement, myconnection);
string selectsno = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
sqlcmd.Parameters.AddWithValue("@sno", selectsno);
SqlDataReader sr;
try
{
myconnection.Open();
sr = sqlcmd.ExecuteReader();
while (sr.Read())
{
snotxt.Text = sr[0].ToString();
snametxt.Text = sr[1].ToString();
timetxt.Text = sr[2].ToString();
worktxt.Text = sr[3].ToString();
interntxt.Text = sr[4].ToString();
skilltxt.Text = sr[5].ToString();
}
}
catch (SqlException ex)
{
MessageBox.Show("数据读取失败:" + ex.Message, "出现错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
myconnection.Close();
}
}
public search()
{
InitializeComponent();
GetData("select * from graduation_51671");
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
showdata();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
myconnection.Open();
if (data.Text.Length != 0)
{
string m = "select * from graduation_51671 where gradata='" + data.Text + "'";
GetData(m);
data.Text = "";
}
else
MessageBox.Show("查询出错:没有输入日期!", "出现错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message, "出现错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
myconnection.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
myconnection.Open();
if (sno.Text.Length != 0)
{
string m = "select * from graduation_51671 where sno='" + sno.Text + "'";
GetData(m);
sno.Text = "";
}
else
MessageBox.Show("查询出错:没有输入日期!", "出现错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message, "出现错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
myconnection.Close();
}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
myconnection.Open();
if (sname.Text.Length != 0)
{
string m = "select * from graduation_51671 where sname='" + sname.Text + "'";
GetData(m);
sname.Text = "";
}
else
MessageBox.Show("查询出错:没有输入日期!", "出现错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message, "出现错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
myconnection.Close();
}
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -