新手遇到问题,现在已经可以在WinForm中的DataGridView中显示数据库的内容了,并且能实现对选定行的删除,但是研究了半天,怎么也实现不了对添加行的实现,故请大家帮帮忙,在此谢谢大家了!在下例中,数据库名为:manager 所用到的表名为:Info
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace INFO
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.DataBind();
} private void DataBind()
{
SqlConnection connection = new SqlConnection(@"Server=.;Integrated security=True;Database=manager");
SqlDataAdapter Adapter = new SqlDataAdapter(@"SELECT * FROM Info);
DataSet ds = new DataSet();
Adapter.Fill(ds);
this.dataGridView1.DataSource = ds.Tables[0];
this.dataGridView1.Refresh();
} private void Form1_Load(object sender, EventArgs e)
{ }
//问题就存在与以下代码 private void buttonInsert_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(@"Server=.;Integrated security=True;Database=manager");
connection.Open();//此处的代码应该怎样写呢?
//
//
//
//请教大家了! connection.Close();
this.DataBind(); }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace INFO
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.DataBind();
} private void DataBind()
{
SqlConnection connection = new SqlConnection(@"Server=.;Integrated security=True;Database=manager");
SqlDataAdapter Adapter = new SqlDataAdapter(@"SELECT * FROM Info);
DataSet ds = new DataSet();
Adapter.Fill(ds);
this.dataGridView1.DataSource = ds.Tables[0];
this.dataGridView1.Refresh();
} private void Form1_Load(object sender, EventArgs e)
{ }
//问题就存在与以下代码 private void buttonInsert_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(@"Server=.;Integrated security=True;Database=manager");
connection.Open();//此处的代码应该怎样写呢?
//
//
//
//请教大家了! connection.Close();
this.DataBind(); }
}
}
{
SqlConnection connection = new SqlConnection(@"Server=.;Integrated security=True;Database=manager");
connection.Open();
SqlDataAdapter Adapter = new SqlDataAdapter();
Adapter.InsertCommand = new SqlCommand(@"insert ......");
Adapter.Update((DataTable)this.dataGridView1.DataSource);
connection.Close();
this.DataBind(); }
cmd.ExecuteNoQuery();