用dataGridView显示数据后,在它里面修改值 修改完以后通过一个按钮把修改后的值更新到数据库中  
应该怎么写呢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 datagridview
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private DataSet ds = new DataSet();
        private SqlDataAdapter adapter;
        private void Form1_Load(object sender, EventArgs e)
        {
            string sql = "select * from tur";
            string con = "Data Source=.;database=Sdyy;uid=sa";
            SqlConnection conn= new SqlConnection(con);
            conn.Open();
            adapter = new SqlDataAdapter(sql,conn);
            adapter.Fill(ds,"tur");
            this.dataGridView1.DataSource=ds.Tables[0];
            conn.Close();
        }        private void button1_Click(object sender, EventArgs e)
        {
            SqlCommandBuilder build = new SqlCommandBuilder(adapter);
            adapter.Update(ds,"tur");
        }        private void freash_Click(object sender, EventArgs e)
        {        }
    }
}我选中其中任意一个单元格的值然后点保存就会更新到数据库中去~  要怎么做呢?  现在我上面的代码报这个错:
请问要实现这样的功能应该怎么写呢 彻底崩溃了~被ADO给整晕了   在线等
 
 

解决方案 »

  1.   

    定义一个SqlCommand 试试
    SqlCommand myCmd = new SqlCommand("select * from tur",con);
    adapter.SelectCommand = myCmd;
      

  2.   

    连接字符串里缺少密码吧:string con = "Data Source=.;database=Sdyy;uid=sa;pwd=sa的密码";
      

  3.   


    难道你们都不知道如果sa用户的密码为空pwd=''是可以省略的么?
    居然还这么多人人云亦云
      

  4.   

    谁能告诉我怎么把图片弄到CSDN上贴出来啊 我弄不上来` 这样问题容易说清楚  谢谢了