在windows编程中加入新行
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 GridView_demo
{
public partial class Form1 : Form
{
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
SqlCommandBuilder cb;
DataSet ds;
public Form1()
{
InitializeComponent();
con = new SqlConnection();
con.ConnectionString = "Server = (local); DataBase = Northwind ; Uid = sa";
string selectCmd = "SELECT * FROM dbo.Employees";
cmd = new SqlCommand(selectCmd, con);
ds = new DataSet("Employees");
da = new SqlDataAdapter();
da.SelectCommand = cmd;
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“northwindDataSet.Employees”中。您可以根据需要移动或移除它。
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (con.State == ConnectionState.Closed)
con.Open();
cb = new SqlCommandBuilder(da);
da.Fill(ds, "Employees");
DataTable dt = ds.Tables["Employees"];
DataRow dr = dt.NewRow();
dr[1] = this.txtUserName.Text.Trim();
dr[2] = this.txtSex.Text.Trim();
dr[3] = int.Parse(this.txtHomePhone.Text.Trim());
ds.Tables[0].Rows.Add(dr);
da.Update(ds, "Employees");
this.dataGridView1.DataSource = ds.Tables["Employees"].DefaultView;
con.Close();
}
private void btnLook_Click(object sender, EventArgs e)
{
if (con.State == ConnectionState.Closed)
con.Open();
da.Fill(ds, "Employees");
this.dataGridView1.DataSource = ds.Tables["Employees"].DefaultView;
con.Close();
}
}
}
浙公网安备 33010602011771号