c#访问数据库的问题 请问用c#访问sql数据库时候,adapter的update方法怎么用啊,我搞了好几遍都没搞懂,我是个初学者,还望各位大神不吝赐教啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private DataTable DT = new DataTable(); private SqlDataAdapter SDA = new SqlDataAdapter(); SqlConnection conn = new SqlConnection(DBClass.ConnStr); public datagreadForm() { InitializeComponent(); } private void datagreadForm_Load(object sender, EventArgs e) { button5.Enabled = false; } private void selectbutton_Click(object sender, EventArgs e) { DT.Clear(); string sqlstr = "select PlateNumber, goodsname,inputtime, grossweight, thetare, netweight, sunit, runit, biaoji from daoyuntable where biaoji=0 and PlateNumber='" + textBox1.Text + "' and goodsname='" + textBox2.Text + "' and sunit='" + textBox3.Text + "' and runit='"+textBox4.Text+"' and bfname='"+DBClass.bangfangStr+"'"; SqlCommand SCD = new SqlCommand(sqlstr, conn); SDA.SelectCommand = SCD; SDA.Fill(DT); dataGridView1.DataSource = DT; button5.Enabled = true; } private void button5_Click(object sender, EventArgs e) { int k = 0; float a = 0; if (dataGridView1.RowCount > 0) { for (int i = 0; i < dataGridView1.RowCount; i++) { if (bool.Parse(dataGridView1.Rows[i].Cells[8].Value.ToString())) { k = k + 1; a=a+float.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString()); } } textBox5.Text = dataGridView1.Rows[0].Cells[0].Value.ToString(); textBox6.Text = dataGridView1.Rows[0].Cells[2].Value.ToString(); textBox7.Text = a.ToString();//总的毛重数 textBox8.Text = k.ToString();//记录修改的记录数 //label2.Text = "你选择了" + k.ToString() + "条记录 总的毛重是:"+a.ToString(); //MessageBox.Show( "你选择了"+k.ToString()+"条记录"); } try { SqlCommandBuilder SCB = new SqlCommandBuilder(SDA); SDA.Update(DT); } catch (System.Exception ex) { MessageBox.Show(ex.ToString()); return; } MessageBox.Show("更新成功!"); button5.Enabled = false; } private void button1_Click(object sender, EventArgs e) { chehaoForm dlg = new chehaoForm(textBox1.Text); dlg.ShowDialog(); textBox1.Text = dlg.chehaoValue; } private void button2_Click(object sender, EventArgs e) { appClass.IDStr = 2; unitForm dlg = new unitForm(textBox2.Text); dlg.ShowDialog(); textBox2.Text = dlg.unitValue; } private void button3_Click(object sender, EventArgs e) { appClass.IDStr = 1; unitForm dlg = new unitForm(textBox3.Text); dlg.ShowDialog(); textBox3.Text = dlg.unitValue; } private void button4_Click(object sender, EventArgs e) { appClass.IDStr = 1; unitForm dlg = new unitForm(textBox4.Text); dlg.ShowDialog(); textBox4.Text = dlg.unitValue; } 简言之,为Adapter实现CUD操作所需的Command是先决条件。大致就是下面这个样子。不过可以用CommandBuilder省一些事。string sql = @".......";adapter = new adapter(sql, conn);adapter.updatecommand = new command();adapter.insertcommand = new command();adapter.deletecommand = new command()..... .....adapter.Update(); 偶还是觉得用Linq To SQL访问数据库比较简单... 请问一个http post的接口问题? DAL中开启ado事务的思路! VS版本与书不同的问题 如何判断ListView控件 右击了 Column bind9问题 求助,关于多线程委托执行方法的问题。 PropertyGrid的默认排序是怎么排的 是否能通过dropdownlist的value定位控件的selectedindex,在线等。谢谢!!! C#中怎样设置一个字符串的长度?string类型 md5加密问题 一个C#执行存储过程奇怪的问题! DataGridView用什么数据源较好?List? DataSet? entity Data Model?
private SqlDataAdapter SDA = new SqlDataAdapter();
SqlConnection conn = new SqlConnection(DBClass.ConnStr);
public datagreadForm()
{
InitializeComponent();
} private void datagreadForm_Load(object sender, EventArgs e)
{
button5.Enabled = false;
} private void selectbutton_Click(object sender, EventArgs e)
{
DT.Clear();
string sqlstr = "select PlateNumber, goodsname,inputtime, grossweight, thetare, netweight, sunit, runit, biaoji from daoyuntable where biaoji=0 and PlateNumber='" + textBox1.Text
+ "' and goodsname='" + textBox2.Text + "' and sunit='" + textBox3.Text + "' and runit='"+textBox4.Text+"' and bfname='"+DBClass.bangfangStr+"'";
SqlCommand SCD = new SqlCommand(sqlstr, conn);
SDA.SelectCommand = SCD;
SDA.Fill(DT);
dataGridView1.DataSource = DT;
button5.Enabled = true;
} private void button5_Click(object sender, EventArgs e)
{
int k = 0;
float a = 0;
if (dataGridView1.RowCount > 0)
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{ if (bool.Parse(dataGridView1.Rows[i].Cells[8].Value.ToString()))
{
k = k + 1;
a=a+float.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString());
}
}
textBox5.Text = dataGridView1.Rows[0].Cells[0].Value.ToString();
textBox6.Text = dataGridView1.Rows[0].Cells[2].Value.ToString();
textBox7.Text = a.ToString();//总的毛重数
textBox8.Text = k.ToString();//记录修改的记录数 //label2.Text = "你选择了" + k.ToString() + "条记录 总的毛重是:"+a.ToString();
//MessageBox.Show( "你选择了"+k.ToString()+"条记录");
}
try
{
SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
SDA.Update(DT);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
MessageBox.Show("更新成功!");
button5.Enabled = false; } private void button1_Click(object sender, EventArgs e)
{
chehaoForm dlg = new chehaoForm(textBox1.Text);
dlg.ShowDialog();
textBox1.Text = dlg.chehaoValue;
} private void button2_Click(object sender, EventArgs e)
{
appClass.IDStr = 2;
unitForm dlg = new unitForm(textBox2.Text);
dlg.ShowDialog(); textBox2.Text = dlg.unitValue;
} private void button3_Click(object sender, EventArgs e)
{
appClass.IDStr = 1;
unitForm dlg = new unitForm(textBox3.Text);
dlg.ShowDialog();
textBox3.Text = dlg.unitValue;
} private void button4_Click(object sender, EventArgs e)
{
appClass.IDStr = 1;
unitForm dlg = new unitForm(textBox4.Text);
dlg.ShowDialog();
textBox4.Text = dlg.unitValue;
}
string sql = @".......";
adapter = new adapter(sql, conn);
adapter.updatecommand = new command();
adapter.insertcommand = new command();
adapter.deletecommand = new command()..... .....adapter.Update();