老师来帮下忙 C#从入门到精通第3版 15.6.3 实例位置\TM\sl\15\8
发表在C#图书答疑
2015-05-23
是否精华
是
否
版块置顶:
是
否
我把你们的代码稍微改动了一下 但是出错了 下面是代码
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Test08
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn;
DataSet ds;
SqlDataAdapter sda;
private void Form1_Load(object sender, EventArgs e)
{
conn = new SqlConnection("server=P5ZT4CYV41AC7L8;database=test_db;Trusted_Connection=SSPI");
SqlCommand cmd = new SqlCommand("select * from TABLE1", conn);
sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds, "cs");
dataGridView1.DataSource = ds.Tables[0];
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables["cs"]; //这里出错了
sda.FillSchema(dt, SchemaType.Mapped);
DataRow dr = dt.Rows.Find(txtNo.Text);
dr["name"] = txtName.Text.Trim();
SqlCommandBuilder cmdbuider = new SqlCommandBuilder(sda);
sda.Update(dt);
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
txtNo.Text = dataGridView1.SelectedCells[0].Value.ToString();
txtName.Text = dataGridView1.SelectedCells[1].Value.ToString();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
[img src=null/img][img src=null/img]
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Test08
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn;
DataSet ds;
SqlDataAdapter sda;
private void Form1_Load(object sender, EventArgs e)
{
conn = new SqlConnection("server=P5ZT4CYV41AC7L8;database=test_db;Trusted_Connection=SSPI");
SqlCommand cmd = new SqlCommand("select * from TABLE1", conn);
sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds, "cs");
dataGridView1.DataSource = ds.Tables[0];
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables["cs"]; //这里出错了
sda.FillSchema(dt, SchemaType.Mapped);
DataRow dr = dt.Rows.Find(txtNo.Text);
dr["name"] = txtName.Text.Trim();
SqlCommandBuilder cmdbuider = new SqlCommandBuilder(sda);
sda.Update(dt);
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
txtNo.Text = dataGridView1.SelectedCells[0].Value.ToString();
txtName.Text = dataGridView1.SelectedCells[1].Value.ToString();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
[img src=null/img][img src=null/img]