请帮帮忙,C#中添加、修改在一个界面有两处错误,下面是代码:
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace stu
{
public partial class user_info : Form
{
public user_info()
{
InitializeComponent();
}
SqlConnection conn;
SqlCommand cmd;
DataSet ds;
SqlDataAdapter sda;
public void user_info_Load(object sender, EventArgs e)
{
conn = stu.DBconn.stucon();
string str = "select * from [user]";
cmd = new SqlCommand(str, conn);
sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
ds = new DataSet();
sda.Fill(ds, "fs");
dataGridView1.DataSource = ds.Tables[0];
}
public void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();
textBox2.Text = dataGridView1.SelectedCells[1].Value.ToString();
textBox3.Text = dataGridView1.SelectedCells[2].Value.ToString();
}
public void user_info_Load_1(object sender, EventArgs e)
{
this.userTableAdapter.Fill(this.mydataDataSet.user);
}
//删除
private void bt_shanchu_Click(object sender, EventArgs e)
{
conn = stu.DBconn.stucon();
conn.Open();
cmd = new SqlCommand("select * from [user] where 用户名='" + textBox1.Text + "'",conn);
int i = Convert.ToInt32(cmd.ExecuteScalar());
if (textBox1.Text != "")
{
if (i != 0)
{
//string id = dataGridView1.SelectedCells[0].Value.ToString();
cmd = new SqlCommand("delete from [user] where 用户名='" + textBox1.Text + "'", conn);
int j = Convert.ToInt32(cmd.ExecuteScalar());
if (j > 0)
{
MessageBox.Show("用户信息删除失败!");
}
else
{
MessageBox.Show("用户信息删除成功!");
conn.Close();
}
}
else
{
MessageBox.Show("没有该用户记录,请重新输入!");
textBox1.Focus();
}
}
else
{
MessageBox.Show("请输入待删除用户信息!");
textBox1.Focus();
}
this.dataGridView1.DataSource = null;
string str = "select * from [user]";
cmd = new SqlCommand(str,conn);
sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
ds = new DataSet();
sda.Fill(ds,"fs");
dataGridView1.DataSource = ds.Tables[0];
textBox1.Text = "";
}
//添加
private void bt_tianjia_Click(object sender, EventArgs e)
{
conn = stu.DBconn.stucon();
conn.Open();
cmd = new SqlCommand("select count(*) from [user] where 用户名='" + textBox1.Text + "'", conn);
int i = Convert.ToInt32(cmd.ExecuteScalar());
if (i == 0)
{
sda = new SqlDataAdapter("select * from [user]", conn);
SqlCommandBuilder sbuildr = new SqlCommandBuilder(sda);
sda.Fill(ds, "[user]");
DataRow dr = ds.Tables["user"].NewRow();
dr["用户名"] = textBox1.Text.Trim();
dr["姓名"] = textBox2.Text.Trim();
dr["密码"] = textBox3.Text.Trim();
ds.Tables["user"].Rows.Add(dr);
sda.Update(ds, "[user]");
MessageBox.Show("记录添加成功!");
this.dataGridView1.DataSource = null;
SqlDataAdapter ssda = new SqlDataAdapter();
SqlCommand scmd = new SqlCommand("select * from [user]", conn);
ssda.SelectCommand = scmd;
ds = new DataSet();
ssda.Fill(ds, "fs");
dataGridView1.DataSource = ds.Tables[0];
}
else
{
MessageBox.Show("已存在该记录");
textBox1.Focus();
}
}
//修改
private void bt_xiugai_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
DataTable dt = ds.Tables["fs"];
sda = new SqlDataAdapter("select * from [user]", conn);
sda.FillSchema(dt, SchemaType.Mapped);
DataRow dr = dt.Rows.Find(textBox1.Text);
dr["用户名"] = textBox1.Text.Trim();
dr["姓名"] = textBox2.Text.Trim();
dr["密码"] = textBox3.Text.Trim();
SqlCommandBuilder sbuilder = new SqlCommandBuilder(sda);
sda.Update(dt);
MessageBox.Show("记录修改成功!");
this.dataGridView1.DataSource = null;
SqlDataAdapter ssda = new SqlDataAdapter();
SqlCommand scmd = new SqlCommand("select * from [user]", conn);
ssda.SelectCommand = scmd;
ds = new DataSet();
ssda.Fill(ds, "fs");
dataGridView1.DataSource = ds.Tables[0];
}
else
{
MessageBox.Show("请输入用户信息!");
textBox1.Focus();
}
}
}
}
|