没有得到插入更新删除任何人告诉我
问题描述:
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 WindowsFormsApplication7
{
public partial class Form1 : Form
{
string strCon = "Data Source=CIODEV03\\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True";
string strSQL;
SqlConnection conn;
SqlCommand cmd;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string strSQL = "select * from gridview";
SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = bindingSource1;
}
private void btninsert_Click(object sender, EventArgs e)
{
strSQL = "insert into gridview values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
conn = new SqlConnection(strCon);
cmd = new SqlCommand(strSQL, conn);
conn.Open();
cmd.ExecuteNonQuery();
dataGridView1.DataSource = bindingSource1;
conn.Close();
}
private void btnupdate_Click(object sender, EventArgs e)
{
strSQL = "update gridview set ID='" + textBox1.Text + "',Name='" + textBox2.Text + "',Address='" + textBox3.Text + "'";
conn = new SqlConnection(strCon);
cmd = new SqlCommand(strSQL, conn);
conn.Open();
cmd.ExecuteNonQuery();
dataGridView1.DataSource = bindingSource1;
}
private void btndelete_Click(object sender, EventArgs e)
{
strSQL = "delete from gridview";
conn = new SqlConnection(strCon);
cmd = new SqlCommand(strSQL, conn);
conn.Open();
cmd.ExecuteNonQuery();
dataGridView1.DataSource = bindingSource1;
}
}
}
答
请参阅这些aticles
^ ]
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson01.aspx [ ^ ]
See these aticles
Using ADO.NET for beginners[^]
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson01.aspx[^]
Simple ADO.NET Database Read, Insert, Update and Delete using C#.[^]