using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication58_复习dgv
{
public partial class Form1 : Form
{
SqlDataAdapter adapter;
DataSet ds;
SqlConnection conn;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
loading();
}
public void loading() {
ds = new DataSet();
String sql = "select * from sanguo";
String connString = "Data Source=.;Initial Catalog=dbok;Persist Security Info=True;User ID=sa;Password=root123123";
conn = new SqlConnection(connString);
adapter = new SqlDataAdapter(sql,conn);
adapter.Fill(ds,"hero");
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = ds.Tables["hero"];
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void tsmiSubmit_Click(object sender, EventArgs e)
{
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(ds, "hero");
MessageBox.Show("更新成功");
}
private void tsmiDelete_Click(object sender, EventArgs e)
{
DataGridViewRow currentRow = dataGridView1.CurrentRow;
DataGridViewCell currentCell = currentRow.Cells[0];
String value = currentCell.Value.ToString();
String sql = "delete from sanguo where id = " + value;
SqlCommand cmd = new SqlCommand(sql,conn);
try
{
conn.Open();
int n = cmd.ExecuteNonQuery();
MessageBox.Show("删除成功,影响行数为:" + n);
}
catch
{
MessageBox.Show("error");
}
finally {
conn.Close();
}
}
}
}