怎样才能在运行之后,将数据库中的数据显示在窗体中,帮忙看一下哪里出错了
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace all
{
public partial class designer : Form
{
public designer()
{
InitializeComponent();
}
SqlDataAdapter sqlDataAdapter;
DataSet abc;
DataTable dt1 = new DataTable();
DataRow[] rows;
DataTable dtshow = new DataTable();
SqlCommand sqlCommand = new SqlCommand();
private void designer_Load(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "server=PC-20111126CNWU;uid=sa;pwd=123;database=store;Connect Timeout=30";
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = myConnection;
= CommandType.Text;
= "SELECT name as 姓名,experience as 工作经验, from worker order by name";
sqlDataAdapter.SelectCommand = sqlCommand;
SqlCommandBuilder builder = new SqlCommandBuilder(sqlDataAdapter);
abc = new DataSet();
sqlDataAdapter.Fill(abc, "worker");
sqlDataAdapter.Fill(dt1);
dataGridView1.DataSource = abc;
dataGridView1.DataMember = "worker";
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "添加")
{
button1.Text = "插入";
button2.Enabled = false;
button4.Enabled = false;
textBox1.Visible = true;
textBox2.Visible = true;
label1.Visible = true;
label2.Visible = true;
// textBox1.Focus();
//textBox1.Focus();
//textBox1.Focus();
}
else
{
if (textBox1.Text == "" || textBox2.Text == "")
MessageBox.Show("请输入插入的数据名称");
else
{
button1.Text = "添加";
button1.Enabled = false;
button6.Enabled = true;
textBox1.Visible = false;
textBox2.Visible = false;
label1.Visible = false;
label2.Visible = false;
button6.Focus();
DataRow myDR = abc.Tables["worker"].NewRow();
if (dataGridView1.RowCount > 1)
myDR[0] = int.Parse(abc.Tables["worker"].Rows[(dataGridView1.RowCount - 2)][0].ToString()) + 1;
else
myDR[0] = 1;
myDR[1] = textBox1.Text.Trim();
myDR[2] = textBox2.Text.Trim();
abc.Tables["worker"].Rows.Add(myDR);
/*datacon.Test.Tables[datacon.table_name].*/
//Rows.Add(myDR);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count == 1)//每次只能修改一个数据所以必须唯一
{
if (button2.Text == "修改")
{
button2.Text = "改正";
button1.Enabled = false;
button3.Enabled = false;
textBox1.Visible = true;
textBox1.Text = abc.Tables["worker"].Rows[dataGridView1.SelectedRows[0].Index][1].ToString().Trim();
textBox2.Text = abc.Tables["worker"].Rows[dataGridView1.SelectedRows[0].Index][2].ToString().Trim();
textBox1.Focus();
textBox2.Focus();
}
else
{
if (textBox1.Text == "" || textBox2.Text == "" )
MessageBox.Show("请输入工作经验");
else
{
button2.Text = "修改";
button2.Enabled = false;
button6.Enabled = true;
textBox1.Visible = true;
button6.Focus();
abc.Tables["worker"].Rows[dataGridView1.SelectedRows[0].Index][1] = textBox1.Text.Trim();
abc.Tables["worker"].Rows[dataGridView1.SelectedRows[0].Index][2] = textBox1.Text.Trim();
abc.Tables["worker"].Rows[dataGridView1.SelectedRows[0].Index][3] = textBox1.Text.Trim();
abc.Tables["worker"].AcceptChanges();
}
}
}
}
private void button3_Click(object sender, EventArgs e)
{
//abc.Update(abc.Test, abc.);
sqlDataAdapter.Update(abc, "worker");
dataGridView1.Refresh();
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = false;
button5.Enabled = false;
//this.Close();
}
private void button4_Click(object sender, EventArgs e)
{
//通过这种方式清除现有表中的数据,否则会有重复的数据
//datacon.Test.Clear();
this.Close();
}
private void button5_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count != 0) //判断选择删除的有几行
{
while (dataGridView1.SelectedRows.Count > 0)//每删除一行count减去一
{
abc.Tables["worker"].Rows[dataGridView1.SelectedRows[0].Index].Delete();
//不能修改SelectedRows[0]中的0,每删除一行,集合中就会减少一行。
}
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button5.Enabled = true;
}
else
MessageBox.Show("请选择要删除的数据");
}
private void button6_Click(object sender, EventArgs e)
{
//datacon.Test.Tables[datacon.table_name].Clear();//通过这种方式清除现有表中的数据,否则会有重复的数据
this.Close();
}
}
}